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
PeterUchytil.ax224PeterUchytil.ax224 

Upsert with External ID question

I have read through the Apex language guide and this post discussing the upsert feature, but I'm still confused as to whether I can do what I want.

I have a sign-up process that I'm integrating with Salesforce. First in the process a survey is given. I will store this a custom object. Later in the process the Account is created and I want to link it to the Survey, but due to some timing issues, I can't guarantee that the Survey object will actually get created before I try to create the Account. I'm guessing most of the time it will, but I can't be sure.

What I want to do is create the Survey object and give it an ID that I create. This is set as an External ID. Then I have an External ID in the Account object. When I create the Account, I wanted to do it with an upsert command to link together the Survey and the Account by the External ID. I tried this, but I the relationship never gets made.

I set my objects up like this:

Survey
    Text SurveyID (External ID)

Account
    Text SurveyID (External ID)
    Related Field: Survey

As a test I created a Survey and set the SurveyID to '1234' then I did this upsert:

Account a;
// set up the account
a.SurveyID = '1234';
upsert a Survey__c.SurveyID;

I was hoping that would fill in the Survey related field in the Account.

I'm obviously doing something wrong. Is what I'm describing even possible? The blog post seemed to indicate it was.

Thanks for any help you can give.

Peter
BoxBox

Hi Peter,

I'm afraid you would have to do the following (or similar) process:

I am assuming that the Survey Object exists before an account object (similar to a Lead type processes in the SFA?)

1) Upsert the Survey record with your external ID, this as you know will either create the Record if that Survey ID does not exist or update an existing Survey record if the ID is found.

2) Upsert the Account record with the Survey ID, again creating the Account if the external ID is not found.

3) Create an Account level trigger that is before insert will search for the Survey ID in the Survey table and insert the salesforce 15 (or 18) character ID into the lookup field on the account.

4) Depending on your business process, if an account can also exist before a survey, or subsequent surveys can be done for an account, also create a Survey before insert trigger that looks up the Survey ID in the account table and links in this direction to.



RedtagRedtag
Peter,
 
Did you work out how to do this? I've got a similar problem with a custom object where I have to return the value of the custom object id to the lookup field on the Account Object based on two external IDs matching.
 
In Box's reply it's point 3 that's causing the problem - how do you code to do this?
 
Steve