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
eddieeddie 

Can upserts with external id foreign keys be done using the Partner WSDL?

I'm trying to figure out how to do an upsert to an object setting it's parent using a foreign key to it's parent's external ID using the partner WSDL.  The example in the 8.0 docs only shows this being done using the enterprise WSDL, is this possible?
SuperfellSuperfell
Yes, this is doable from partner WSDL. It works exactly the same way, except you need to manually build the nested element structure.
eddieeddie

Can you please provide a snippet of code (preferably c#) showing how this is done?

 

Thanks

DevAngelDevAngel

An 8.0 Sample can be found here.

 

https://wiki.apexdevnet.com/index.php/API#Java

eddieeddie
I dug through all the java samples and can't find an example of setting a relationship on upsert using an external id foreign key, using the partner WSDL. Can you be more specific about where I can find the example?
DevAngelDevAngel
I guess I'm not sure exactly what you are trying to do.  You approach upsert as you would create or update.  You will need to the parent's external id value to set the childs "parent".  You will also need the parent's id to establish the relationship if the upsert ends up being a create.
eddieeddie

Here is your example from your API docs (page 168) of what I am trying to do using using the partner WSDL, but it uses the enterprise WSDL:

You can use external ID fields as a foreign key, allowing you to access records in a single step instead of querying a record to get the ID first.To do this, specify the foreign key name and the external ID field value. For example:

Opportunity upsertOpportunity = new Opportunity();

upsertOpportunity.setStageName("Prospecting");

// Standard object ref

Account upsertParentAccountRef = new Account();

upsertParentAccountRef.setExternal_SAP1_ACCTID__c("SAP111111");

upsertOpportunity.setAccount(upsertParentAccount);

// Opportunity ref

upsertOpportunity.set_SAP1_OPPID__c("SAP222222");

UpsertResult[] upsertResults = binding.upsert("SAP1_OPPID__c",

new SObject[] {upsertOpportunity});

 

SimonF said it could be done, but I needed to "manually build the nested element structure". I'm looking for an example of how this is done.

Thanks.

 

SuperfellSuperfell
static void Main(string args[]) {
sf.SforceService s = new sf.SforceService();
login(s);
XmlDocument doc = new XmlDocument();
sf.sObject contact = new sf.sObject();
contact.type = "Contact";
XmlElement lastName = Element(doc, "LastName", "Flintstone");
XmlElement firstName = Element(doc, "FirstName", "Fred");
XmlElement contactExt = Element(doc, "Ext__c", "123123321");
XmlElement account = Element(doc, "Account", null);
account.AppendChild(Element(doc, "type", "Account"));
account.AppendChild(Element(doc, "SystemId__c", "BEDROCK_01"));

contact.Any = new XmlElement[] { contactExt, lastName, firstName, account} ;
sf.UpsertResult sr = s.upsert("Ext__c", new sf.sObject[] { contact })[0];
if (sr.success)
        Console.WriteLine("Success! id of contact is {0}", sr.id);
else
         Console.WriteLine("{0} {1}", sr.errors[0].statusCode, sr.errors[0].message);
}


static XmlElement Element(XmlDocument doc, String name, String val)
{
    XmlElement e = doc.CreateElement(name);
    if (val != null) e.InnerText = val;
    return e;
}
cgoncorpcgoncorp

Can this be done with the Enterprise WSDL using C# ?

I don't see a way of calling this line: upsertOpportunity.setAccount(upsertParentAccount);

In fact, it doesn't look like this variable (upsertParentAccount) is declared in the java example, so I don't see how this even compiles.

 

I only get the option to set the AccountId in C#, so how do I set the parent account to take advantage of the single step access?

 

Carlos

You can use external ID fields as a foreign key, allowing you to access records in a single step instead of querying a record to get the ID first.To do this, specify the foreign key name and the external ID field value. For example:

Opportunity upsertOpportunity = new Opportunity();

upsertOpportunity.setStageName("Prospecting");

// Standard object ref

Account upsertParentAccountRef = new Account();

upsertParentAccountRef.setExternal_SAP1_ACCTID__c("SAP111111");

upsertOpportunity.setAccount(upsertParentAccount);

// Opportunity ref

upsertOpportunity.set_SAP1_OPPID__c("SAP222222");

UpsertResult[] upsertResults = binding.upsert("SAP1_OPPID__c",

new SObject[] {upsertOpportunity});

SuperfellSuperfell
You need to download the 8.0 Enterprise WSDL.
NS_ForceNS_Force

Hi,

Can this whole process be done from the Apex code rather than javascript or C#.In C# we are adding a web reference.In javascript again we have the connection to WSDL.But how to go about if we want to implement it through Apex code.Say reset Password in User Object.If we are going for javascript then it is not at all secure.

Please help.

Regards

SuperfellSuperfell
There are direct equivalents in Apex to many of the API verbs, including upsert, check the Apex docs for your specifics.
GL_NSGL_NS
Thanks for prompt reply.
 
I had gone through the doc.But everything there is mentioned with the Java and C# code.Now if I try to login say using the login() method, its giving
 
Compile Error: Invalid type: LoginResult  
 
Please suggest whats the process to call these methods from Apex code.
 
Regards,
NS
SuperfellSuperfell
You probably want to read the Apex language docs, and not the API docs. There's no login in Apex because it makes no sense, you have to already be authenticated to be able to run it.
NS_ForceNS_Force

Hi,

That was just an example.

Actually I want to change the password of a specific User.How to go about it?

If you can provide some sample that would be of great help.

Thanks in Advance....

 

GL_NSGL_NS

Hi Simon,

I am stuck at this point.Please help.

For security reasons, you cannot query User passwords via the API or the Salesforce user interface. However, the API allows

you to set and resetUser passwords using the setPassword and resetPassword calls.

Requirement is to add a new user to Salesforce (not using Salesforce UI) through code.

Adding a user to Salesforce is followed by the setPassword call.We dont have any idea where exactly the password of the Users are saved.Please provide some pointers on how to set the Password of User.

Regards,

SuperfellSuperfell
You can't call setPassword from Apex, so you'll need to do it from some external code via the API.
GL_NSGL_NS

Should I go for javascript to set the password by calling

sforce.connection.setPassword(id, password);

What about the security of password if i use javascript?

Regards.