You need to sign in to do that
Don't have an account?

ForeignKey relation using ExternalId with Partner WSDL
Hello All,
I am trying to use upsert() with Partner WSDL and set reference object while upserting. Ex. I would like to add Account reference to Contact using Partner WSDL. I know how it works with Enterprise WSDL. But if any of you can help me getting this done with Partner WSDL, your help is greately appreciated.
With Enterprise WSDL the code looks like this:
Contact upsertContact = new Contact();
upsertContact.setName("Ramana");
//To indicate the reference, attach an Account object, which has only the external ID field specified.
Account upsertParentAccountRef = new Account();
upsertParentAccountRef.setExternal_SAP1_ACCTID__c("SAP111111");
upsertContact.setAccount(upsertParentAccount);
With Partner WSDL, I am trying to do this:
SObject sfdcObj = new SObject();
sfdcObj.setType("Contact");
MessageElement[] sfdcFieldsArray = new MessageElement[2];
sfdcFieldsArray[0] = new MessageElement(new QName("Name"), "Ramana");
sfdcFieldsArray[1] = new MessageElement(new QName("Phone"), "111.111.1111");
sfdcObj.set_any(sfdcFieldsArray);
SObject sfdcAccObj = new SObject();
sfdcAccObj.setType("Account");
MessageElement[] sfdcFieldsArray1 = new MessageElement[1];
sfdcFieldsArray1[0] = new MessageElement(new QName("ExternalID__c"), "Nimaya Inc");
sfdcAccObj.set_any(sfdcFieldsArray1);
And here I want to add the sfdcAccObj as parent to sfdcObj.
Please advice how to proceed, as soon as possible.
Thanks in advance.
Regards,
Ramana
All Answers
how about
sfdcFieldsArray[2] = new MessageElement(new QName("Parent"), sfdcAcObj);
Simon, thanks a lot for the reply. Out of curiosity (even before I try this out), does this work for the Custom objects as well? i.e, In my example above if I have a Custom object in place of Cotact?
Or, do I have to use the relation here, in case of custom object?
Regards,
Ramana Maddikunta
Simon,
It resulted in error:
No such column 'Parent' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name
Please advice what I could do, to get this working.
Regards,
Ramana Maddikutna
Simon,
Thanks for the quick response, I got it working for the Standard objects. I am trying to do the same on Custom objects and I am struck, becuase I am not able to get the relationship name.
I am using describeObject to get the fields and corresponding relationship names. It works fine in case of Standard objects. But, in case of Custom objects, I am not able to get the relationship names. Please provide any hints on that.
My objects and corresponding relations look like this:
ObjectName: Object1__c
Fields: External_ID__c, Field1, Field2, Contact__c. Here Contact__c is a look up field reference to Contact object.
Now I am trying to get the relationship name for Contact__c field in Object1, so that while creating a Object1 record I can add refernce to Contact using External Id of Contact using Partner WSDL.
This is similar to what I saw in standard objects, Contact and Account. Where Contact.AccountId has reference to Account object and corresponding relationship name on Contact.AccountId is Account.
I was wondering if we need to enable something like field-level security etc on the relationships on Custom objects!
Please advice.
Thanks in advance.
Regards,
Ramana Maddikunta
Simon,
For some reason I do not get the Relationship Name correponding to a Field in a Custom object referencing to another Custom object, from the DescribeSObjectResult.
Regards,
Ramana Maddikunta
Simon,
Thanks for the response. I understand what you explained, but I am not able to get the relationship name using field.getRelationshipName(). Following is the piece of code I am trying:
fields = sObjResult.getFields();
for (int fldInd = 0; fldInd < fields.length; fldInd++)
{
field = fields[fldInd];
String fieldName = field.getName();
String relationshipName = field.getRelationshipName();
System.out.println("Field Name and relationshipname:: " + fieldName + "-" + relationshipName );
}
Here I am expecting to get the ouput as foo__c - foo__r. But I only get foo__c.
I get the output correct in case of Standard objects. I am really stuck with Custom objects..!!
Regards,
Ramana Maddikunta
Simon,
the field type shows correctly as reference.
Another interesting thing I noticed is, I am able to see the relationship name in the Enterprise WSDL. But I a using Partner WSDL and I am able to get the relationship name for Standard objects.
Regards,
Ramana Maddikunta
There seem to be something special (to be done) with Custom objects. I tried it by hard coding the relationship as I see it in the Enterprise WSDL, and I got the following error:
faultString: INVALID_FIELD: No such column 'Contact__r' on entity 'Holdings__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
But I have Contact__c as a custom field in Holdings__c object which is a look up field referencing to Contact object.
Regards,
Ramana Maddikunta
I am able to see them in Apex Explorer. I am using API version 7.0.
BTW, How do I get the latest API jar? Do I need to bulid them from Partner WSDL?
Regards,
Ramana Maddikunta
Simon,
Thanks a lot for all the advice. Everything works now with the latest version of the API.
Regards,
Ramana Maddikunta.