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
Alex MakkAlex Makk 

Trigger to update one lookup field from another lookup field on same Object

Hi all! Tried to use workflows, but it does not work with lookup fields. So I assume I need to use trigger. I need to take field data from Lookup_Field_1__c to Lookup_Field_2__c and add extra word. So if first field has "Example", second field will have "Example-Scrap" whenever I create new record.

Here is what I found in other topics but it did not work. I think I have to query somehow fields before operating them.

 

Trigger scrap on PPE_Metrics__c (before update){
  for (PPE_Metrics__c obj: trigger.new){
    Scrap__c = Produced__c+"-Scrap";
  }
}


Maharajan CMaharajan C
Hi Ryan,

Thats not possible to add the text to the Lookup because it holds the Id's not the text.

If you try this with the apex then you will get an foriegn key error.

So simply create a formula field instead of that Lookup field with the return type text then you can use the below formula.

Produced__r.Name   +  "-Scrab"

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
​Raj
Alex MakkAlex Makk
Hi Maharaja! Thanks for reply. In this case, is it possible for trigger to search for products that have same product name as source product + "-Scrap"? If there is one - populate into lookup field its ID. If not - give an error.

Formula field seem to be easy, but not sure it will work for me. Its because we process one product and get two. We will then need to use 2  products. But with formula field I'll have 1 actual product field and 1 non-product field. 
Maharajan CMaharajan C
Thanks for clearing the requirement:

I have some questions:

You want to find the Duplicate the right?

In which object you want to populate this ID from the Product? i.e. Where the lookup field is presented same objecct or some other object? 

If the source product + "-Scrap" is not presented then you want to throw an error while saving the record?

Thanks,
Raj