4

I am using the Java API and trying to create an Attachment with a reference to the Parent external Id field. The setParent() method takes a Name object as the parameter.

For what I read, the Name object is to allow references to another object that can be of different types (Account, Campaign, Opportunity, ...), but I have no idea on how to use it.

In any other object that has a relationship to, let's say, Account (account__c) I can setAccount__c(theAccountId) or setAccount__r(accountWithOnlyExternalIdField).

Any info on how to use this Name object?

Edit to add example with Task object in case it was not clear:

//Create Task related to Account by Id
Task t = new Task();
t.setAccountId("001O000000SQ1xD");
//we can insert task and it will link to the account with id 001O000000SQ1xD

//Create Task related to Account by externalId field
Task t = new Task();
Account a = new Account();
a.setMyCustomExternalId__c("12345")
t.setAccount(a);
//we can insert task and it will link to the account which has MyCustomExternalId__c 12345

Edit 2: I begin to suspect that this may not be possible. The Name object reference says:

This object is used to retrieve information from related records where the related record may be from more than one object type (a polymorphic foreign key). For example, the owner of a case can be either a user or a group (queue). This object allows retrieval of the owner name, whether the owner is a user or a group (queue). You can use a describe call to access the information about parents for an object, or you can use the who, what, or owner fields (depending on the object) in SOQL queries. This object cannot be directly accessed. Source: Name object referece

If this is accurate and the Attachment.setParent() can not be used despite the fact that it is exposed, then maybe the question title should be rephrased to something like "How to set a polymorphic foreign key by its external id instead of its id".

2 Answers 2

1

Currently it is not possible to upsert using an external id for polymorphic fields. It would require you to pull a list of all ids and external ids, then match and then insert your record. Sorry for the bad news.

1
  • This is really bad news but at least there is some workaround. Unfortunately this increases complexity in my server because I will need to take into account these relationships and the order in which I submit the different objects to Salesforce.
    – momo
    Commented Sep 22, 2014 at 2:20
0

As I understand, the problem is, that Attachment has a polymorphic key to its parent objects i.e. Attachment can be a child of any standard/ custom object (which allows attachments). Hence, the auto-resolution of parent record via external id would not be fruitful.

So, I would suggest you to use the ID of account (example code, not executed):-

Attachment a = new Attachment();
/*
 Set attachment body/name etc.
*/
a.setParentId("001O000000SQ1xD");

To read more about polymorphic keys, refer to this

3
  • Thanks for answering. I would use the account Id if it was available to me. This is a server that keeps in sync with Salesforce via triggers from sf and a pi calls to sf every minute. It is possible to add an attachment to a newly created account not yet synced to sf. So because I'd may not be know yet I need to rely on the external id.
    – momo
    Commented Aug 18, 2014 at 5:53
  • 1
    You may need to have two calls then: 1. Query for Salesforce ID using external ID 2. Create attachment. Commented Sep 16, 2014 at 14:45
  • @momo: Regarding your query, in Salesforce every attachement has to be associated with a parent. When you say account is not synched with Salesforce, I presume it is not created in SF. If so, then you cannot create an attachment in SF without first syncing Account. Only after Account is created in SF you can create an attachment under it.
    – Anshul
    Commented Oct 16, 2014 at 3:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .