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
Rajesh T 19Rajesh T 19 

i have opportunity and custom object(lookup realationship) ...but when two fields in custom object met the condition then opportunity object field want to update ='true' ....to done by trigger?

i have opportunity and custom object(lookup realationship) ...but when two fields in custom object met the condition then opportunity object field want to update ='true' ....to done by trigger
Best Answer chosen by Rajesh T 19
Rishabh Agrawal 18Rishabh Agrawal 18
Sure Please find the code sample below--
 
trigger childTrigger on ChildObject(after update){
Set<Id> oppIdSet = new Set<Id>();      
for (childObject c:trigger.new){
             if(c.firstfield != null && csecondField != null){
                  oppIdSet.add(c.Opportunity__c);      
             }
      }
List<Opportunity> oppListToUpdate = new List<Opportunity>();
for(Opportunity opp: [Select id,FieldToUpdate from Opportunity where Id in:oppIdSet]){
          opp.fieldToUpdate = Value You want to Update;
          oppListToUpdate.add(opp);
}
Update oppListToUpdate;
}

Use this code replace your field names and condition you want to test and then try. Let me know if you have any queries.

Please mark this answer as best answer if it solves your query

Thanks,
Rishabh
  

All Answers

Rishabh Agrawal 18Rishabh Agrawal 18
You can use process builder as well, by putting the check if condition met the criteria(two fields of custom object met the condition) then update related opportunity field to true.
Rajesh T 19Rajesh T 19
@Rishabh Agarwal thanks....but i new to developer...so i am zeal to do it in Trigger....can you help me???
Rishabh Agrawal 18Rishabh Agrawal 18
Sure Please find the code sample below--
 
trigger childTrigger on ChildObject(after update){
Set<Id> oppIdSet = new Set<Id>();      
for (childObject c:trigger.new){
             if(c.firstfield != null && csecondField != null){
                  oppIdSet.add(c.Opportunity__c);      
             }
      }
List<Opportunity> oppListToUpdate = new List<Opportunity>();
for(Opportunity opp: [Select id,FieldToUpdate from Opportunity where Id in:oppIdSet]){
          opp.fieldToUpdate = Value You want to Update;
          oppListToUpdate.add(opp);
}
Update oppListToUpdate;
}

Use this code replace your field names and condition you want to test and then try. Let me know if you have any queries.

Please mark this answer as best answer if it solves your query

Thanks,
Rishabh
  
This was selected as the best answer
Rajesh T 19Rajesh T 19
@Rishabh Agarwal thanks........it's working good.....
Rajesh T 19Rajesh T 19
@Rishabh Agarwal......Can you help me how to write a Testclass for above trigger????