Programmatic SmartObject Inserts using C#

Last post 04-24-2008, 4:39 PM by endpointsys. 0 replies.
Sort Posts: Previous Next
  •  04-24-2008, 4:39 PM 23381

    Programmatic SmartObject Inserts using C#

    What I'm doing 

    I have a project where we need to upload data into a collection of SmartObjects (SmartBox) before starting a workflow process. The data is passed into a web service via serialized Xml from another system. Using the SourceCode.Hosting.Client.BaseAPI namespace, I initiate objects like so:

                _k2Server = new SmartObjectClientServer();
                _k2Server.CreateConnection();
                _k2Server.Connection.Open(scb.ToString());
                SmartObject aH = _k2Server.GetSmartObject("MySO");

    Because i have an array of objects in my serialized Xml object, i have to loop through and enter each one into the SmartObject:

                    foreach (RequisitionApproverItem1 approver in _pr.Approvers)
                    {
                        aH.Properties["Prop1"].Value = serializedXml.prop1;
                        aH.Properties["Prop2"].Value = serializedXml.prop2;
                        aH.Properties["Prop3"].Value = serializedXml.prop3;
                        aH.Properties["Prop4"].Value = _serializedXml.prop4;

                        aH.MethodToExecute = aH.MethodToExecute = "Create";
                        _k2Server.ExecuteScalar(aH);
                    }
     

    The Behavior
    What the code doesn't show is that there is a key value on my SmartObject that is of type autonumber that isn't set - it's auto-generated by the corresponding datatype in SQL Server. What would happen is the first iteration would insert correctly, but when the second iteration went to insert, a SmartObjectException was thrown, with the exception data seeming to indicate that the same autonumber was being re-inserted into the new record.

    The Solution

    The following code change to my loop fixed this:

                    foreach (RequisitionApproverItem1 approver in _pr.Approvers)
                    {
                        aH = _k2Server.GetSmartObject("MySO");
                        aH.Properties["Prop1"].Value = serializedXml.prop1;
                        aH.Properties["Prop2"].Value = serializedXml.prop2;
                        aH.Properties["Prop3"].Value = serializedXml.prop3;
                        aH.Properties["Prop4"].Value = _serializedXml.prop4;

                        aH.MethodToExecute = aH.MethodToExecute = "Create";
                        _k2Server.ExecuteScalar(aH);
                    }


    Lucas Vogel
    Senior Managing Consultant
    Endpoint Systems
    http://endpoint-systems.net
    Filed under: ,
View as RSS news feed in XML