+ Start a Discussion
Stancioiu Stefan 1Stancioiu Stefan 1 

Hi guys. Anybody help me to write this trigger? I'm new on salesforce

1. Create a custom field on the Contact object: Is Primary Contact (checkbox)
2. Create a custom field on the Contact object: Primary Contact Phone (Phone);
3. A validation should be added so that if the Account already has a Primary Contact set a new one cannot be added;​
4. When a contact is set as primary, the Primary Contact Phone should be updated to all Contacts related to the same account. This should be an asynchronous process. Make sure that if one Contact update fails, it doesn’t rollback the changes for the others.
Best Answer chosen by Stancioiu Stefan 1
Abhishek BansalAbhishek Bansal
Hi Stancioiu,

Please find your reuiqred beow:
trigger primaryContact on Contact(before insert, after update){
	Set<Id> setOfAccIds = new Set<Id>();
	for(Contact con : trigger.new){
		if(con.AccountId != null){
			setOfAccIds.add(con.AccountId);
		}
	}
	
	Map<Id,Account> mapOfAccounts = new Map<Id,Account>([select (select id,Primary_Contact_Phone__c,Is_Primary_Contact__c from Contacts) where Id IN :setOfAccIds]);
	
	if(trigger.isBefore && trigger.isInsert){
		for(Contact con : trigger.new){
			if(con.AccountId != null && mapOfAccounts.containsKey(con.AccountId)){
				for(Contact childCon : mapOfAccounts.get(con.AccountId).Contacts){
					if(childCon.Is_Primary_Contact__c == true){
						con.addError('Contact cannot be created since Primary Contact already exist in the Account');
					}
				}
			}
		}
	}
	
	if(trigger.isAfter && trigger.isUpdate){
		List<Contact> listOfContactsToUpdate = new List<Contact>();
		for(Contact con : trigger.new){
			if(con.Is_Primary_Contact__c == true && con.Is_Primary_Contact__c != trigger.oldMap.get(con.id).Is_Primary_Contact__c && mapOfAccounts.containsKey(con.AccountId)){
				for(Contact childCon : mapOfAccounts.get(con.AccountId).Contacts){
					childCon.Primary_Contact_Phone__c = con.Primary_Contact_Phone__c;
					listOfContactsToUpdate.add(childCon);
				}
			}
		}
		if(listOfContactsToUpdate.size() > 0){
			update listOfContactsToUpdate;
		}
	}
}
Please let me know if you have any concern.

Thanks,
Abhishek Bansal

All Answers

Abhishek BansalAbhishek Bansal
Hi Stancioiu,

Please find your reuiqred beow:
trigger primaryContact on Contact(before insert, after update){
	Set<Id> setOfAccIds = new Set<Id>();
	for(Contact con : trigger.new){
		if(con.AccountId != null){
			setOfAccIds.add(con.AccountId);
		}
	}
	
	Map<Id,Account> mapOfAccounts = new Map<Id,Account>([select (select id,Primary_Contact_Phone__c,Is_Primary_Contact__c from Contacts) where Id IN :setOfAccIds]);
	
	if(trigger.isBefore && trigger.isInsert){
		for(Contact con : trigger.new){
			if(con.AccountId != null && mapOfAccounts.containsKey(con.AccountId)){
				for(Contact childCon : mapOfAccounts.get(con.AccountId).Contacts){
					if(childCon.Is_Primary_Contact__c == true){
						con.addError('Contact cannot be created since Primary Contact already exist in the Account');
					}
				}
			}
		}
	}
	
	if(trigger.isAfter && trigger.isUpdate){
		List<Contact> listOfContactsToUpdate = new List<Contact>();
		for(Contact con : trigger.new){
			if(con.Is_Primary_Contact__c == true && con.Is_Primary_Contact__c != trigger.oldMap.get(con.id).Is_Primary_Contact__c && mapOfAccounts.containsKey(con.AccountId)){
				for(Contact childCon : mapOfAccounts.get(con.AccountId).Contacts){
					childCon.Primary_Contact_Phone__c = con.Primary_Contact_Phone__c;
					listOfContactsToUpdate.add(childCon);
				}
			}
		}
		if(listOfContactsToUpdate.size() > 0){
			update listOfContactsToUpdate;
		}
	}
}
Please let me know if you have any concern.

Thanks,
Abhishek Bansal
This was selected as the best answer
Stancioiu Stefan 1Stancioiu Stefan 1
Thanks Buddy, its working fine !