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
Mario Conteddu 1Mario Conteddu 1 

Cross Object field update - Checkbox Sync on Custom to Standard object

Hi All,

I am very new to programming and I am trying to takle my first challenge...

I need to create a trigger which check/uncheck a checkbox on the contact record when a checkbox is checked/unchecked on a custom object. 

The custom object has a child relationship with Contact object. 

I have Ok_to_Welcome_Email__c on both custom object and Contact object - they should basically get synced, the custom object will be the master for the syncronization.

following anothe post, I've written the following code which clearly does not work:
 
trigger updateOkToWelcomeEmail on CustomObject__c (before insert, before update) {   
    List<Contact> ContactList = new List<Contact>();
    List<Id> contactId        = new List<Id>();
        For(CustomObject__c fa : Trigger.new) {
           if(fa.ok_to_welcome_email__c = True) {
               contactId.add(fa.Contact__c);
                }
                }
contactList = [SELECT Id, ok_to_welcome_email__c FROM Contact WHERE Contact__c IN : contactId];
    for(Contact l : contactList) {
        l.ok_to_welcome_email__c = true;
        }
        update contactList;
}





 
Best Answer chosen by Mario Conteddu 1
Amit GhadageAmit Ghadage
Hi Mario,
use this code.
trigger updateOkToWelcomeEmail on CustomObject__c (before insert, before update) { 

Map<Id,CustomObject__c > contactIdToCustomObjectMap = new Map<Id,CustomObject__c >();

 For(CustomObject__c fa : Trigger.new) { 

           contactIdToCustomObjectMap.put(fa.Contact__c , fa);
    }

 List<Contact> updatedContactList = new List<Contact>();
 Set<Id> ContactIdSet = contactIdToCustomObjectMap.keySet();

 for(Contact l : [SELECT Id, ok_to_welcome_email__c FROM Contact WHERE Id IN :ContactIdSet])
 {
             if(contactIdToCustomObjectMap.get(l.Id).ok_to_welcome_email__c != null)
             {
             	  l.ok_to_welcome_email__c = contactIdToCustomObjectMap.get(l.Id).ok_to_welcome_email__c;
              	  updatedContactList .add(l); 
               }
    } 
update updatedContactList;

 }

Best Regards,
Amit Ghadage.

All Answers

VineetKumarVineetKumar
Hi Mario,

Your code is totally correct, nothing wrong with it.
The only things I can see, is that you have put a conditional check to only work when the checkbox on you custom object is checked, not when it is unchecked.
if(fa.ok_to_welcome_email__c = True) {
    contactId.add(fa.Contact__c);
}

If you remove this condition, your code will run for both the cases check and un-check.
Also, I would recomment running this piece of login in After context rather than in a Before context.

Let me know if you need anymore help.
Amit GhadageAmit Ghadage
Hi Mario,
use this code.
trigger updateOkToWelcomeEmail on CustomObject__c (before insert, before update) { 

Map<Id,CustomObject__c > contactIdToCustomObjectMap = new Map<Id,CustomObject__c >();

 For(CustomObject__c fa : Trigger.new) { 

           contactIdToCustomObjectMap.put(fa.Contact__c , fa);
    }

 List<Contact> updatedContactList = new List<Contact>();
 Set<Id> ContactIdSet = contactIdToCustomObjectMap.keySet();

 for(Contact l : [SELECT Id, ok_to_welcome_email__c FROM Contact WHERE Id IN :ContactIdSet])
 {
             if(contactIdToCustomObjectMap.get(l.Id).ok_to_welcome_email__c != null)
             {
             	  l.ok_to_welcome_email__c = contactIdToCustomObjectMap.get(l.Id).ok_to_welcome_email__c;
              	  updatedContactList .add(l); 
               }
    } 
update updatedContactList;

 }

Best Regards,
Amit Ghadage.
This was selected as the best answer
Mario Conteddu 1Mario Conteddu 1
Amit, it worked! Thanks guys!
Amit GhadageAmit Ghadage
Great Mario, If it worked you can mark it as best answer.