I don't know a lot about Apex, and I'm trying to figure out how to basically copy the value of a lookup field to another lookup field. If I were to use my own logic it would look something like this:

Custom_Object1__c.Lookup_c = Custom_Object2__c.Lookup__c;

This obviously doesn't work, but I don't know how to go about it. Any help is appreciated!

  • 1
    does it have to be a trigger, why not use a process builder? In a trigger you may have to query the value of custom_object2__c.lookup__c to associate to the custom object lookup1
    – Rao
    Jul 18, 2018 at 20:46
  • If I were just trying to do that one update, then I would definitely not be trying to write Apex lol I would just use the process builder. It's a part of a much bigger process that I'm working on though, but I'm stuck on this one part right now. Sorry I should have been more clear. Based on other threads, I figured that I would have to do a query of some kind. I just don't know how to do that.
    – ad_rey
    Jul 18, 2018 at 20:53
  • right before the line just throw custom_object2__c custom_obj_2 = [select id,lookup__c from custom_object2__c where id=: 'XXXX'] fill in the XXXX and rewrite the next line to Custom_Object1__c.Lookup_c = custom_obj_2 .Lookup__c;
    – Rao
    Jul 18, 2018 at 21:30
  • Since you're new to Apex, you should definitely complete the Apex tutorial modules on [Salesforce Trailhead(trailhead.salesforce.com/en/home) and also make use of various Apex tutorials on Youtube and on the web. Jul 18, 2018 at 23:33

1 Answer 1


Here is a sample Apex code that does the logic you looking for. Of course there are many variations to this logic. Hope this helps.

MyObject__c myobject = [SELECT id, Account__c FROM MyObject__c WHERE name = 'Replicate Lookup Id Test'];

TestObject__c testobject = New TestObject__c();
testobject.name = 'Replicate Lookup from MyObject';
testobject.account__c = myobject.account__c;

Insert testobject;
  • This is awesome! What if I wanted to do WHERE "id =" instead of "name = "?
    – ad_rey
    Jul 19, 2018 at 15:49
  • That will work too. just replace (WHERE id = 'a0R1J000008Du2n') Jul 19, 2018 at 16:37

You must log in to answer this question.

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