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?

Sergej Utko
  • 22,020
  • 11
  • 59
  • 88
Yash Mehta
  • 1,515
  • 4
  • 24
  • 42

1 Answers1

17
//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;
techtrekker
  • 18,348
  • 1
  • 43
  • 50
  • 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 '13 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. – techtrekker Jun 10 '13 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 '13 at 13:54
  • @YashMehta is this functionality documented somewhere? – Matt and Neil Oct 09 '15 at 09:54