function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Glenn.ax34Glenn.ax34 

Creation of objects using Partner API

I am attempting create/modifify a task using the sForce 2.0 Partner version.  I am having a difficult time understanding the sObject that is used in creation objects in your system.  I understand that an sObject array is neccessary, but not sure how the sObjects in that array are setup.  Any help would be greatly appreciated...  An example would be nice.

-- Glenn

fouloxfoulox
Glen I'm not a salesforce employee so I might be wrong, and I haven't tackled this problem yet but I spent sometime looking into it because I know I'm going to be hitting the same issue within the next couple of days. Here's what I figured out:

I'm using java, and for me it's pretty easy to initiate an SObject, and set the type (type is the type of SObject i.e. Account, Opportunity, etc.) and ID, the tricky part is creating the MessageElement[] which contains everything else. To construct this you basically need to create an MessageElement Factory class that will create a MessageElement based upon the field name and soapType which you get from the describe call, and the value you would like to set that field to.

I tried just changing the textNode of each MessageElement that I wanted to update but I wasn't successful in doing that, and then I checked on the Axis message boards and it doesn't look like it's possible to alter the text nodes anyway.

Good luck, and if you figure out anything please post it.

Message Edited by foulox on 12-04-2003 10:51 AM

Glenn.ax34Glenn.ax34

Thanks for the help, but is there a MessageElement in API 2.5 for Partners? I am unable to find it in any of the namespaces. 

-- Glenn

DevAngelDevAngel

Here is a quick sample in C# of creating an object using the partner API.  Totally off the cuff, first time I tried it, not optimized, but works.  This code uses a proxy to TCPMon (from Axis).

private void button1_Click(object sender, System.EventArgs e)
{
 sforce.SforceService binding = new sforce.SforceService();
 binding.Proxy = new System.Net.WebProxy("localhost:8082");
 sforce.LoginResult lr = binding.login("username", "password");
 binding.SessionHeaderValue = new sforce.SessionHeader();
 binding.SessionHeaderValue.sessionId = lr.sessionId;
  System.Xml.XmlDocument xFrag = new System.Xml.XmlDocument();
   
 sforce.sObject[] sOb = new sforce.sObject[2];

 sOb[0] = new sforce.sObject();
 sOb[0].type = "Account";
 System.Xml.XmlNode xnode;
 xnode = xFrag.CreateNode(System.Xml.XmlNodeType.Element, "Name", "");
 xnode.InnerText = "Partner Test 1";
 sOb[0].Any = new System.Xml.XmlElement[] {(System.Xml.XmlElement)xnode};

 sOb[1] = new sforce.sObject();
 sOb[1].type = "Account";
 System.Xml.XmlNode xnode1  = xFrag.CreateNode(System.Xml.XmlNodeType.Element, "Name", "");
 xnode1.InnerText = "Partner Test 2";
 sOb[1].Any = new System.Xml.XmlElement[] {(System.Xml.XmlElement) xnode1};

 sforce.SaveResult[] sr = binding.create(sOb);
 
}

Glenn.ax34Glenn.ax34

I have tried doing this with no success.  My code is as follows:

objSFObject = new sObject();
objSFObject.type = "Task";

objXmlNode = objXmlDoc.CreateNode(System.Xml.XmlNodeType.Element, "WhoId", "");
objXmlNode.InnerText = validID;
objSFObject.Any = new System.Xml.XmlElement[] {(System.Xml.XmlElement)objXmlNode };

arrSaveResults = ((SalesForceUser)objApiUser).SFConn.create(
new sObject[]{objSFObject});

This throws the error:

"org.xml.sax.SAXException: Could not find deserializer for field: WhoId of type {urn:enterprise.soap.sforce.com}ID"

I have tried a number of fields, but keep getting the same error.  Please help me...

--Glenn

 

 

 

 

 

Glenn.ax34Glenn.ax34

Nevermind... I saw another message that you cannot right to the whoid.  The old API allowed it, but no big deal.

-- Glenn

DevAngelDevAngel

Hi Glen,

We are working on the problem.  You code looks fine, this is definitely a problem on the service.

 

GaganGagan

Hi Dave,

I am facing the problem while inserting records while inserting records in Account. I am using VB.net and partners 2.5 API

The problem occurs while try to create sObject, thought c# sample from your site works fine. I tried the code in your sample in my VB.net application when this problem occured.

I a pasting the final code below and look forward for the help, I have tried diffrent tricks but no solution. If u can send some Sample in VB.Net that will be highly appriciated.

+----------------------------Code begins--------------------------

 

Private Function MakeFieldElement(ByVal fieldName As String, ByVal fieldValue As String) As System.Xml.XmlElement

Dim frag As System.Xml.XmlDocument = New System.Xml.XmlDocument

Dim el As System.Xml.XmlNode = frag.CreateNode(Xml.XmlNodeType.Element, fieldName, "")

el.InnerText = fieldValue

Return el

End Function

 

 

Public Function InsertRecordsToSFDC(ByVal RacordTable As Hashtable, ByVal entity As String, ByVal errmsg As String)

Dim newAccount As New sObject

Dim Key, Value As String

newAccount.type = "Account"

Dim record() As System.Xml.XmlElement

Try

Dim myEnumerator As IDictionaryEnumerator = RacordTable.GetEnumerator()

Dim i As Integer = 0

While myEnumerator.MoveNext

record(i) = MakeFieldElement(myEnumerator.Key, myEnumerator.Value)
' Here i geet this exception .... "Object reference not set to an instance of an object." 

i = i + 1

End While

newAccount.Any = record

Dim saveResult As SaveResult() = Binding.create(New sObject() {newAccount})

 

 

Catch ex As Exception

errmsg = ex.ToString

End Try

End Function

+--------------------------------- code ends----------------------------------

 

 

DevAngelDevAngel

Hi Gagan,

Seems there might be one or two problems with the code.  I think you need to dimension your array like:

Dim record(RacordTable.Count - 1) As System.Xml.XmlElement

Try that and see if the error persists.

Cheers

 

Dave ConorozzoDave Conorozzo

It does not look like you allocated memory for your records:
Dim record() As System.Xml.XmlElement

You need to either ReDim that array before each:
record(i) = MakeFieldElement(myEnumerator.Key, myEnumerator.Value)

or you need to Dim it initially to RacordTable.Count - 1, or RacordTable.Count depending on whether you use 0-based or 1-based indexing:
I use 0-based:

Dim record(RacordTable.Count - 1) As System.Xml.XmlElement