13

I have an lookup field in my opportunity object which is pointing to a custom object called Code__c.

Code__c has an external Id field called Code_Source__c.

When creating an opportunity once way of linking the opportunity to Code__c is by passing Id.

Opportunity op = new Opportunity(Name = 'Test Op', StageName = 'Open', ... , Oppoortunity_Code__c =code.Id );

Is it possible to use external ID (Code_Source__c) instead of the code.Id?

1 Answer 1

16
//create an in-memory instance of code, with the external id set
Code__c code = new Code__c(Code_Source__c = 'EXTERNAL_ID_HERE');

//create an instance of Opportunity, and point to the instance of code created above
Opportunity op = new Opportunity(Name = 'Test Op', StageName = 'Open', ... , Oppoortunity_Code__r = code );

upsert op;
4
  • Can I apply the same concept if I want to use external Id of a standard object. For instance when I am creating Opportunity Team members?
    – Yash Mehta
    Jun 10, 2013 at 19:52
  • 1
    Yes you can use this for any relationships where there is an External Id, eg relating Contacts to Accounts using an External Id field on Account. Jun 10, 2013 at 20:08
  • I tried using this relationship to pull Users and Opportunity while creating Opportunity Team members. But it is returning null. Following are the two statements: User teamUser = new User(PeopleSoft_Empl_ID__c = 'Temp12345'); and Opportunity req = new Opportunity(Requisition_Number__c = 'Req1234');
    – Yash Mehta
    Jun 12, 2013 at 13:54
  • @YashMehta is this functionality documented somewhere? Oct 9, 2015 at 9:54

You must log in to answer this question.

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