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
AlwaysConfusedAlwaysConfused 

Using the Partner WSDL / API to "upsert" ... Accounts

Ok so I have some data in a .Net DataTable object and I want to put this on a customers Salesforce.

 

I have the login details and I have my reference to the SF Partner API all setup.

 

I make my call to "DescribesObjects" so I know what fields are available on the account sObject type.

 

now what?

 

In the documentation it says its really easy ... all i need to do is declare an array of sforce.Account objects ... erm big flaw there ... the partner API does not have any of those ... only the base sObject type.

 

So lets start there ...

 

sObject obj = new sObject();

 

hmmm ... nothing to let me assign to fields?

 

So what now?

Best Answer chosen by Admin (Salesforce Developers) 
kxa422kxa422

For partners, something like that should work

 

public sObject[] BuildObjectArray(DataTable Data, System.Xml.XmlElement[] ObjectDesc)
{
 sObject[] oReturn = new sObject[Data.Rows.Count];
 int i = 0;
 int index = 0;
 apex.sObject account;
 
 foreach (DataRow dr in Data.Rows)
 {
  account = new apex.sObject();
  ObjectDesc[index++] = GetNewXmlElement("ID", dr["ID"]);

  account.type = "Account";
  account.Any = ObjectDesc;
  
  oReturn[i] = account;
  i++;
 }

 return oReturn;

}
private System.Xml.XmlElement GetNewXmlElement(string Name, string nodeValue)
{
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    System.Xml.XmlElement xmlel = doc.CreateElement(Name);
    xmlel.InnerText = nodeValue;
    return xmlel;
}

All Answers

AlwaysConfusedAlwaysConfused

 

Lets assume I'm making a method that looks something like this ...

 

public sObject[] BuildObjectArray(DataTable Data, DescribeSObjectResult ObjectDesc)
{

        // Answer in here

}

kxa422kxa422

Hello, 

 

 

May be something like that:

 

 

public sObject[] BuildObjectArray(DataTable Data, Account ObjectDesc)
{

 

 

 

 

            sObject[] oReturn = new sObject[Data.Rows.Count];
            int i = 0;

 

            foreach (DataRow dr in Data.Rows)
            {
                Account a = new Account();
                a.Id = dr["ID"];
                oReturn[i] = a;
                i++;
            }

 

 

 

           return oReturn;

 

 }

 

 

 

 

 

 

 

 

 

 

 

and somewhere else

 

UpsertResult[] uResult = sForce.upsert("Id", sReturn);

 

 

 

 Where sReturn is the return of BuildObjectArray and id the field used to match the PK for the upsert operation.

 

 

 

 

 

 

AlwaysConfusedAlwaysConfused

The Type definition "Account" does not exist in the Partner API.

 

Which is why I have passed in a Describe result.

 

So ... from a description of an object and a datatable how do I get an array of sobjects that contain my data?

SuperfellSuperfell

See the .NET sample code, it includes samples for the partner API.

AlwaysConfusedAlwaysConfused

Do you by any chance have a link?

 

I have only managed to find samples that involve using the "Account" object in this situation which of course does not exist in my case because i am not using the enterprise API.

AlwaysConfusedAlwaysConfused

Never mind .... i finally found it buried away in some other section of the usual documentation :)

 

thanks Simon.

kxa422kxa422

For partners, something like that should work

 

public sObject[] BuildObjectArray(DataTable Data, System.Xml.XmlElement[] ObjectDesc)
{
 sObject[] oReturn = new sObject[Data.Rows.Count];
 int i = 0;
 int index = 0;
 apex.sObject account;
 
 foreach (DataRow dr in Data.Rows)
 {
  account = new apex.sObject();
  ObjectDesc[index++] = GetNewXmlElement("ID", dr["ID"]);

  account.type = "Account";
  account.Any = ObjectDesc;
  
  oReturn[i] = account;
  i++;
 }

 return oReturn;

}
private System.Xml.XmlElement GetNewXmlElement(string Name, string nodeValue)
{
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    System.Xml.XmlElement xmlel = doc.CreateElement(Name);
    xmlel.InnerText = nodeValue;
    return xmlel;
}

This was selected as the best answer