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
sooria narayanan Balasubramaniamsooria narayanan Balasubramaniam 

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
Best Answer chosen by sooria narayanan Balasubramaniam
Vatsal KothariVatsal Kothari
Hi,

You can refer below code:
trigger updateAccountPrimaryEmail on Contact (after insert, after update){
	
	Set<Id> accIdSet = new Set<Id>();
	
	for(Contact con: trigger.new){
		if(con.Primary__c == true && con.AccountId != null){
			accIdSet.add(con.AccountId);
		}
	}
	if(!accIdSet.isEmpty()){	
		Map<Id,Account> accMap = new Map<Id,Account>([Select Id,PrimaryContactEmail__c from Account where Id IN: accIdSet]);
		
		if(!accMap.isEmpty()){
			for(Contact con: trigger.new){
				if(con.Primary__c == true && con.AccountId != null){
					accMap.get(con.AccountId).PrimaryContactEmail__c = con.Email;
				}
			}
			
			if(!accMap.isEmpty()){
				update accMap.values();
			}
		}
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

Vatsal KothariVatsal Kothari
Hi,

You can refer below code:
trigger updateAccountPrimaryEmail on Contact (after insert, after update){
	
	Set<Id> accIdSet = new Set<Id>();
	
	for(Contact con: trigger.new){
		if(con.Primary__c == true && con.AccountId != null){
			accIdSet.add(con.AccountId);
		}
	}
	if(!accIdSet.isEmpty()){	
		Map<Id,Account> accMap = new Map<Id,Account>([Select Id,PrimaryContactEmail__c from Account where Id IN: accIdSet]);
		
		if(!accMap.isEmpty()){
			for(Contact con: trigger.new){
				if(con.Primary__c == true && con.AccountId != null){
					accMap.get(con.AccountId).PrimaryContactEmail__c = con.Email;
				}
			}
			
			if(!accMap.isEmpty()){
				update accMap.values();
			}
		}
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
This was selected as the best answer
CheyneCheyne
This should do it.

<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>
sooria narayanan Balasubramaniamsooria narayanan Balasubramaniam
Thank you for your valuable tips cheyne and Vatsal..  Both are working