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
Shyam Sundar 84Shyam Sundar 84 

how to update a lookup field in an custom object based on the picklist field on opportunity both are related??

how to update a lookup field in an custom object based on the picklist field on opportunity both are related??
@anilbathula@@anilbathula@
Hi Shyam,

You can write a trigger or set up a process builder on opportunity picklist update.
check this link for setting process builder: https://automationchampion.com/2015/03/04/getting-started-with-process-builder-part-6-working-with-related-records/

Thanks
Anil.B
Deepali KulshresthaDeepali Kulshrestha
Hi Shyam,

Greetings to you!

First, you never want to put a query inside of a For loop. As it stands right now, you're querying for each record when you could have made a collection first and then run one single query for all of the related records in obect2__c.

That said, since the record Id's in object2__c are the same as the Case Id's (from your query, object2.Id =: cs.id), you don't even need to run a query at all. Instead, all you need to do is determine whether or not your picklist values have changed, then update the related records using the same Id's.

Finally, this is an After Update trigger, not both a Before and an After update. a Before Update runs before the updates on Case are committed to the database. You don't want to update the related object until the updates have been written to the Case Object and you only want to write them once.

Here's your revised trigger:

trigger casetoupdate on Case (after update) {

   List<object2__c> toUpdate = new list<object2__c>();

   for(Case cs : Trigger.New) {     

      // compare new and prev picklist values to create a list of Object__2 records to update

      if(cs.picklist__c != trigger.oldMap.get(cs.Id).picklist__c) {

         object2__c toUp = new object2__c(Id = cs.Id, picklist__c = cs.picklist__c); 

         toUpdate.add(toUp);

      } // end of if statement

   } // end of for loop

   if(toUpdate.isEmpty() == false) Update toUpdate;

} // end of trigger


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
shyam sundar 112shyam sundar 112

Can you help me just opposite to this.

Lookup to picklist value updation using trigger