In this sample, I will show how to import from an XML document to a UDO of type Document.
I assume that you have prior knowledge of the SAP SDK in this sample. We do not offer support on this code. If you get issues you will need to look at the SDK help for SAP.
If you need to make it work with a Master Data UDO please consult the SAP SDK help.
Setup:
You will need to first create the metadata and register the UDO:
User-defined tables:
User-defined fields:
Next, register the UDO and you can then use the code below to import the XML file attached.
Code:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using Boyum.Utilities.FileHandler.Model;
using SAPbobsCOM;
using SBO.DI;
namespace MyNameSpace
{
public class CustomClass : UniversalFunctions.Public.FileImporter.IImporter
{
public FileHandlerResultEntry Import(FileHandlerParameters parameters)
{
try
{
var company = SBO.DI.Connection.SboCompany;
GeneralService udoGeneralService = company.GetCompanyService().GetGeneralService("BOYX_UDO");
var generalData = (GeneralData) udoGeneralService.GetDataInterface(GeneralServiceDataInterfaces.gsGeneralData);
var objectToImportFromXml = CreateObjectToImportFromXml(parameters);
generalData.SetProperty("U_BOYX_INVDATE", objectToImportFromXml.InvoiceDate);
generalData.SetProperty("U_BOYX_CARDCODE", objectToImportFromXml.CardCode);
var udoLines = generalData.Child("BOYX_UDO_LINE");
for (var index = 0; index < objectToImportFromXml.Lines.Count; index++)
{
var line = objectToImportFromXml.Lines[index];
udoLines.Add();
udoLines.Item(index).SetProperty("U_BOYX_ITEMCODE", line.ItemCode);
udoLines.Item(index).SetProperty("U_BOYX_QTY", line.Quantity);
}
udoGeneralService.Add(generalData);
return new FileHandlerResultEntry(FileHandlerResultStatus.Success);
}
catch (Exception e)
{
return new FileHandlerResultEntry(FileHandlerResultStatus.Error, "Exception caught", e);
}
}
private ObjectToImport CreateObjectToImportFromXml(FileHandlerParameters parameters)
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(parameters.File);
var order = new ObjectToImport();
order.CardCode = xmlDoc.GetElementsByTagName("CardCode")[0].InnerText;
order.InvoiceDate = DateTime.Today;
XmlNodeList items = xmlDoc.GetElementsByTagName("Item");
foreach (XmlNode item in items)
{
var lines = new Line();
foreach (XmlNode attributes in item.ChildNodes)
{
switch (attributes.Name)
{
case "ItemCode":
lines.ItemCode = attributes.InnerText;
break;
case "Quantity":
lines.Quantity = Convert.ToInt32(attributes.InnerText);
break;
}
}
order.Lines.Add(lines);
}
return order;
}
private class ObjectToImport
{
public DateTime InvoiceDate { get; set; }
public string CardCode { get; set; }
public List<Line> Lines { get; private set; }
public ObjectToImport()
{
Lines = new List<Line>();
}
}
private class Line
{
public string ItemCode { get; set; }
public double Quantity { get; set; }
}
}
}
Comments
1 comment
I created the UDO exactly as the instructions. Seems the code does not accept the UDO name as valid.
I get this error:
Exception: System.Runtime.InteropServices.COMException (0xFFFFFC16): Service Not Found
at SAPbobsCOM.CompanyServiceClass.GetGeneralService(String sServiceCode)
at MyNameSpace.CustomClass.Import(FileHandlerParameters parameters)
any ideas?
regards Sotos Soteriou
Please sign in to leave a comment.