Currently Being Moderated
Janos Nagy

Importing Alternative items into SAP B1

Posted by Janos Nagy in janos.nagy on Mar 3, 2010 6:50:37 AM

In my Exporting - Importing Authorization using MS Excel in SAP B1 I have shown how to import / export the Auhorizations using MS Excel and VBA. In this blog I would like to give some tips to handle a missing functionality of DTW: Migrate/Import Alternative Items into SAP B1. This sample using code examples from SDK Help Center to build up a VBA application for importing the Alternative items. 

Data structure in Excel

 In Excel the list of items are available in the following format:

  • Column A will be mapped to Item number of Original Item
  • Column B will be mapped to Item number of Alternative Item
  • Column C will be mapped to Matching factor
  • Column D will be mapped to Remark

 

 

Background - DI API and AlternativeItemsService Object

SAP B1 2007 DI API has an interface for Alternative items. The interface can be reached by AlternativeItemsService, and this service can manage the following operations:

  • Adding a new record by calling AddItem method
  • Deleting records by calling DeleteItem method
  • Receiving records by calling GetItem method
  • Updating records (after received called GetItem) by UpdateItem method

Common property of these methods the data structure, which called  OriginalItem.

Public Function AddItem( _
ByVal pIOriginalItem As OriginalItem _
) As OriginalItemParams

The OriginalItem data structure practically holds the Item Number of Original item, and the collection of alternative items. The data collection called AlternavtiveItems, which holds the Alternative Item number, remarks, and matching factor.

'Original Item number
OriItem.ItemCode = "A00003"
' Adding a Line to Alternative Item
OriItem.AlternativeItems.Add()
AltItem = OriItem.AlternativeItems.Item(0)
AltItem.AlternativeItemCode = "A00001"
AltItem.MatchFactor = 200
AltItem.Remaks = "Test"

Based on SDK help, to add a new Altertanive item, the following code can be used:

Dim oCompanyService As SAPbobsCOM.CompanyService
Dim AltItemsService As SAPbobsCOM.AlternativeItemsService
Dim OriItem As SAPbobsCOM.OriginalItem
Dim OriItemParams As SAPbobsCOM.OriginalItemParams
Dim AltItem As SAPbobsCOM.AlternativeItem
oCompanyService = oCompany.GetCompanyService
AltItemsService = 
oCompanyService.GetBusinessService(ServiceTypes.AlternativeItemsService)
OriItem =
AltItemsService.GetDataInterface(AlternativeItemsServiceDataInterfaces.aisOriginalItem)
OriItemParams =
AltItemsService.GetDataInterface(AlternativeItemsServiceDataInterfaces.aisOriginalItemParams)

OriItem.ItemCode = "A00003"

' Adding a Line to Alternative Item
OriItem.AlternativeItems.Add()
AltItem = OriItem.AlternativeItems.Item(0)
AltItem.AlternativeItemCode = "A00001"
AltItem.MatchFactor = 200
' Adding the new Alternative Items
OriItemParams = AltItemsService.AddItem(OriItem)

Steps

In MS Excel, the VBA (Visual Basic for Applications) can be used for creating simple "Addons" like applications with SDK UI API and DI API. (simiral to excel macros). The language is similar to VB 6 version. To write the code, open a new workbook and launch VBA ( ALT+F11), and Add the SAP Business One UI API and SAP B1 DI API into the list of references from ToolsRefernces menu.

Steps to be completed to add the List of Alternative items  for each user in VBA:

1. Connect to company with Single-sing on

Dim SboGuiApi As New SAPbouiCOM.SboGuiApi
Dim SBO_App As SAPbouiCOM.Application
Dim oCompany As New SAPbobsCOM.Company
SboGuiApi.Connect
("0030002C0030002C00530041005000420044005F0044
0061007400650076002C0050004C006F006D0056004900490056")
Set SBO_App = SboGuiApi.GetApplication()
Set oCompany = SBO_App.Company.GetDICompany()

2. Get the Original Item and Alternative item data structure from Services of Company object

Dim oCompanyService As SAPbobsCOM.CompanyService
Dim AltItemsService As SAPbobsCOM.AlternativeItemsService
Dim OriItem As SAPbobsCOM.OriginalItem
Dim OriItemParams As SAPbobsCOM.OriginalItemParams
Dim AltItem As SAPbobsCOM.AlternativeItem
Set oCompanyService = oCompany.GetCompanyService
Set AltItemsService = 
oCompanyService.GetBusinessService(ServiceTypes.AlternativeItemsService)
Set OriItem =
AltItemsService.GetDataInterface(AlternativeItemsServiceDataInterfaces.aisOriginalItem)
Set OriItemParams =
AltItemsService.GetDataInterface(AlternativeItemsServiceDataInterfaces.aisOriginalItemParams)

3. Loop in the excel sheet, and address the data into the OriginalItem and AlternativeItem data structures represented by OriItem and AltItem variables. The table must be sorted by Original Item numbers. The loop cycle will consider changes of Column "A" to add the Collection of Alternative items into the database.

'First row includes header
row = 2
Do While ActiveSheet.Cells(row, 1) <> ""

OriItem.ItemCode = ActiveSheet.Cells(row, 1)
Call OriItem.AlternativeItems.Add
Set AltItem = OriItem.AlternativeItems.Item(OriItem.AlternativeItems.Count - 1)
AltItem.AlternativeItemCode = ActiveSheet.Cells(row, 2)
AltItem.MatchFactor = ActiveSheet.Cells(row, 3)
AltItem.Remarks = ActiveSheet.Cells(row, 4)
If ActiveSheet.Cells(row + 1, 1) <> ActiveSheet.Cells(row, 1) Then
Set OriItemParams = AltItemsService.AddItem(OriItem)
End If
row = row + 1
Loop

Conclusion

Ms Excel and VBA is offers a good environment to write and small usefull applications related to SAP B1 using SDK components, by methods/features provided by SDK.

Sample source can be found here

Comments