You need to sign in to do that
Don't have an account?

Contact Trigger
Hi everyone,
There is a custom field of type 'checkbox' called "Primary" in Contact object. If this Primary is checked, then the value of the contact's Email field has to be updated in the custom email field named "PrimaryContactEmail" in the Account Standard object. How can this be done in Trigger. Please do suggest a solution
There is a custom field of type 'checkbox' called "Primary" in Contact object. If this Primary is checked, then the value of the contact's Email field has to be updated in the custom email field named "PrimaryContactEmail" in the Account Standard object. How can this be done in Trigger. Please do suggest a solution
You can refer below code:
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal
All Answers
You can refer below code:
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal
<pre>
trigger UpdatePrimaryContactEmail on Contact (after insert, after update) {
//This will contain a list of all accounts related to contacts where primary has been checked.
List<Account> accountsToUpdate = new List<Account>();
for (Contact c : trigger.new) {
//Only update the account if the Contact Primary field is True.
if (c.Primary__c == true) {
//Since we are specifying the ID field, this will allow us to update the existing account with the new email field.
Account acct = new Account(Id=c.AccountId, PrimaryContactEmail__c=c.Email);
accountsToUpdate.add(acct);
}
}
update accountsToUpdate;
}
</pre>