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
SV 9SV 9 

Need to write a trigger to update the second object

There are 2 objects
Object 1: Contact - Field Contact Opt Out : Check box
Object 2 : Survey - Field Survey Optout : CheckBox

When Object 2: Surveyoptout is true, then Object 1 : Contact Opt out checkbox should be true
Best Answer chosen by SV 9
VinayVinay (Salesforce Developers) 
Hi Sravanthi,

Try below snippet.
trigger Survey on Survey__c(after insert, after update)
{
   List<Contact> cnList = new List<Contact>();
    for(Survey__c sy : Trigger.new)
   {
       if(sy.Contact__c != null)
      {
         Contact cn = new Contact();
         cn.Id = sy.Contact__c;
         cn.Field_Survey_Optout__c = sy.Field_Survey_Optout__c ;
         cnList.add(cn);

       }
   }
update cnList;
}

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar

All Answers

VinayVinay (Salesforce Developers) 
Hi Sravanthi,

Try below snippet.
trigger Survey on Survey__c(after insert, after update)
{
   List<Contact> cnList = new List<Contact>();
    for(Survey__c sy : Trigger.new)
   {
       if(sy.Contact__c != null)
      {
         Contact cn = new Contact();
         cn.Id = sy.Contact__c;
         cn.Field_Survey_Optout__c = sy.Field_Survey_Optout__c ;
         cnList.add(cn);

       }
   }
update cnList;
}

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
This was selected as the best answer
SV 9SV 9
Thank You, this worked.