What I do is create a class with some helper methods for each object I need to insert.
public static NikuDataBus NewNikuBus(String objectCode)
{
var xog = new NikuDataBus
{
Header = new Header
{
externalSource = "NIKU",
version = "13.1.0.0248",
},
customObjectInstances = new List<CustomObjectInstancesType>
{
new CustomObjectInstancesType {objectCode = objectCode}
}
};
return xog;
}
private static InstanceType NewStrip(String userName, String name, String desc, Int32 processing)
{
var strip = new InstanceType
{
instanceCode = "-1",
parentInstanceCode = "-1",
objectCode = "action_sub",
parentObjectCode = "financialtask"
};
strip.OBSAssocs.complete = false;
var security = new UserSecurityType
{
rightCode = "odf_cst_action_sub_edit",
userName = userName
};
strip.Security.UserSecurity.Add(security);
strip.CustomInformation.ColumnValue.Add(CaHelper.BuildCustomValue("desc_long", desc));
strip.CustomInformation.ColumnValue.Add(CaHelper.BuildCustomValue("code", "-1"));
if (processing == 1) name = String.Format("{0} - I", name);
if (processing == 2) name = String.Format("{0} - II", name);
if (processing == 3) name = String.Format("{0} - III", name);
if (processing == 4) name = String.Format("{0} - IV", name);
name = CheckStringLenght(name, 80);
strip.CustomInformation.ColumnValue.Add(CaHelper.BuildCustomValue("name", name));
return strip;
}
public static CustomInformationColumnValue BuildCustomValue(String name, String value)
{
return new CustomInformationColumnValue
{
name = name,
Value = value
};
}
I then a insert method which takes the populated object and then I just serialize the object to xml and past that to WriteCustomObjectInstance soap method.
public Boolean InsertNewStripObject(InstanceType instanceType, Checkbook_SpisLookupQueryRecord spisLookupRecord)
{
var name = instanceType.CustomInformation.ColumnValue.Find(x => x.name.IsEqualTo("name", true));
var amount = instanceType.CustomInformation.ColumnValue.Find(x => x.name.IsEqualTo("amount", true));
if (!Commit)
{
NLogger.Info("Commit false=>Would have created: Strip {0} with Amount = {1}", name.Value, amount.Value);
return false;
}
var xog = NewNikuBus(instanceType.objectCode);
try
{
var stripObject = QueryStripCode(instanceType, spisLookupRecord);
if (stripObject == null)
{
xog.Header.args = new List<args> { new args { name = "overrideAutoNumbering", value = "false" } };
}
else
{
instanceType.instanceCode = stripObject.code;
var code = instanceType.CustomInformation.ColumnValue.Find(x => x.name.IsEqualTo("code", true));
if (code != null) code.Value = stripObject.code;
}
xog.customObjectInstances[0].instance.Add(instanceType);
xog.XOGOutput = null;
var xogString = xog.SerializeObject();
var writeCustomObject = GetElement(xogString);
var responseElement = ObjectsService.WriteCustomObjectInstance(writeCustomObject);
var responseCustom = GetCustomInstanceResponse(responseElement);
if (responseCustom.Statistics.failureRecords != 0)
{
NLogger.Error(responseElement.OuterXml);
return false;
}
if (stripObject != null)
{
NLogger.Info("Updated: Strip {0} with {1} on row {2}", name.Value, String.Format("Amount = {0} with code = {1}", amount.Value, stripObject.code), SheetRow);
return true;
}
stripObject = QueryStripCode(instanceType, spisLookupRecord);
NLogger.Info("Created: Strip {0} with {1} on row {2}", name.Value, String.Format("Amount = {0} with code = {1}", amount.Value, stripObject.code), SheetRow);
return true;
}
catch (Exception ex)
{
NLogger.Error(ex.Message);
CaLogout();
throw;
}
}
V/r,
Gene