[SalesForce] how to update picklist field according to another object

I have two objects:

  • object1__c, which has a text field called sds__c and a picklist field called sds_chek__c (values "Yes"/"No")
  • object2__c contain a text field called sds__c

Here, I want to compare both objects' sdc_c values. If any records match, then in the object1__c picklist field populate with "Yes". If not match even one record also, the picklist value should populate with "No".

Please any one can help me!

Best Answer

How about trigger compare? You must create 2 triggers make it work.

on object 2 you must have after insert/update (and before delete but I will skip it) actions in method:

Set<String> matchedFields = new Set<String>();
object_1__c[] obj1List = new object_1__c[]{};

for(object_2__c obj : Trigger.new){
    matchedFields.add(sds__c);
}

for(object_1__c obj : [SELECT Id, sds_chek__c
                       FROM object_1__c
                       WHERE sds__c IN : matchedFields]){

   if(obj.sds_chek__c == 'No'){
       obj.sds_chek__c = 'Yes';
       obj1List.add(obj)
   }
}

update obj1List;

You should also have trigger on before insert object_1__c that will check if in system already is some matching record

If you have any other question please let me know

Related Topic