In previous blog, i took on an example of importing into SBO document.

Our aim in this blog is to have a flexible program that assigns the SBO Document object properties without having to harcode each of the properties.

Our first step was to have a list of properties exposed by DI API, which we have done that in the previous blog.

In this blog will show you how to dynamically assign all the values without harcoding the properties.

In this example, i assume I have a System.Data.DataTable object which is declared below :

'Create the Data Table + Columns Dim oDT As New System.Data.DataTable oDT.Columns.Add("CustomerCode", Type.GetType("System.String")) oDT.Columns.Add("ExternalDate", Type.GetType("System.DateTime")) oDT.Columns.Add("DeliveryDate", Type.GetType("System.DateTime"))

In this example I will store the SBO Object properties mapping into the column’s Extended Properties.

You would keep this mapping some where and you can build this data table dynamically using this mapping.

Thus, if your client ask for a change of mapping, you can do so in the mapping table without affecting your code.

oDT.Columns("CustomeCode").ExtendedProperties.Add("SBOProp", "CardCode") oDT.Columns("ExternalDate").ExtendedProperties.Add("SBOProp", "DocDate") oDT.Columns("DeliveryDate").ExtendedProperties.Add("SBOProp", "DocDueDate")

Read the Data from external source and store in the DataTable

For this example, I am hardcoding the data

oDT.Rows.Add("Customer1", DateTime.Now, DateTime.Now) oDT.Rows.Add("Customer2", DateTime.Now, DateTime.Now) oDT.Rows.Add("Customer3", DateTime.Now, DateTime.Now)

I am also declaring a target object type to create. in this case i am using Orders

Dim iObjType As Integer = 17 'Orders    -- You can also store this doc type some where and use it here instead of hardcoding.

Now that we have all the data and the mapping.

we need to assign each of the document property.

Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom(My.Application.Info.DirectoryPath & "Interop.SAPbobsCOM.dll") For Each row As System.Data.DataRow In oDT.Rows     Dim oDoc As SAPbobsCOM.Documents = oCompany.GetBusinessObject(iObjType)     'Assign the Values     For Each column As System.Data.DataColumn In oDT.Columns         Dim DESTF As String = column.ExtendedProperties("SBOProp")         If Not DESTF.StartsWith("U_") Then             asm.GetType("SAPbobsCOM.IDocuments").GetProperty(DESTF).SetValue(oDoc, row(column.ColumnName), Nothing)         Else             'We can assign UDF directly             oDoc.UserFields.Fields.Item(DESTF).Value = row(column.ColumnName)         End If     Next     'You will need to also handle the Document Lines here the same way we assign the header above.     'Add the object as usual.     Dim lErr As Integer = oDoc.Add Next

With the above code, we do not need to hardcode any of the DI object properties.

Hence any changes on the data/mapping is only a change on our mapping data, we would not need to modify the coding just to cater for additional/removal/changes of the field in external data.

Last thing to note :

In the SetValue method, we would need to pass the exact data type of the field.

ie. if the DI Api property is of string, we need to pass the string object. if it is int, we need to pass in an int object.

You can check the target object type using :

Dim TargetType As String = asm.GetType("SAPbobsCOM.IDocuments).GetProperty(DESTF).PropertyType.FullName

Then cast your object to this target type.

Thats it.

Happy Coding.

Edy

New NetWeaver Information at SAP.com

Very Helpfull

User Rating: Be the first one !