You need to sign in to do that
Don't have an account?
Mario 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:
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; }
use this code.
Best Regards,
Amit Ghadage.
All Answers
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 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.
use this code.
Best Regards,
Amit Ghadage.