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
Ritesh001Ritesh001 

Trigger to restrict user from creating not more than 1 contact per account using controller

Can someone suggest a trigger using the controller? So user cant create more than 1 contacts per account using apex controller 
Best Answer chosen by Ritesh001
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ritesh,

Try this:

Handler Class:
public class OneContactHandler {
    
    public void onBeforeInsert(List<Contact> lstContact){
        Set<id> accId = new Set<id>();
        for(Contact cc : lstContact) {
            if(cc.AccountId != null) {
                accId.add(cc.AccountId);    
            }
        }
        
        Map<Id,Account> mapAccount = new Map<Id,Account> ([SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN : accId]);
        for(Contact oCon:lstContact) {	
            if( oCon.AccountId != null && mapAccount.containsKey(oCon.AccountId) ) {
                Account acc = mapAccount.get(oCon.AccountId);
                if(acc.Contacts.size() > 0 ) {
                    oCon.addError('You cant add duplicate contact for this account'); 
                }
            }
        }
    }
}

Trigger:
trigger OneContactPerAcc on Contact (before insert) {
    
    OneContactHandler handler = new OneContactHandler();
	
	if(Trigger.isBefore) {
		if(Trigger.isInsert)
			handler.onBeforeInsert(Trigger.new);		
	}
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Ritesh,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger OneContactPerAcc on Contact (before insert) {
    Set<id> accId = new Set<id>();
    for(Contact cc : trigger.new) {
        if(cc.AccountId != null) {
            accId.add(cc.AccountId);    
        }
    }
    
    Map<Id,Account> mapAccount = new Map<Id,Account> ([SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN : accId]);
    for(Contact oCon:trigger.new) {	
        if( oCon.AccountId != null && mapAccount.containsKey(oCon.AccountId) ) {
            Account acc = mapAccount.get(oCon.AccountId);
            if(acc.Contacts.size() > 0 ) {
                oCon.addError('You cant add more then one contact for this account'); 
            }
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ritesh001Ritesh001
Thanks for your Help,
I had done with a trigger. But I was wanted to do the same using the Apex controller and trigger. 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ritesh,

Try this:

Handler Class:
public class OneContactHandler {
    
    public void onBeforeInsert(List<Contact> lstContact){
        Set<id> accId = new Set<id>();
        for(Contact cc : lstContact) {
            if(cc.AccountId != null) {
                accId.add(cc.AccountId);    
            }
        }
        
        Map<Id,Account> mapAccount = new Map<Id,Account> ([SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN : accId]);
        for(Contact oCon:lstContact) {	
            if( oCon.AccountId != null && mapAccount.containsKey(oCon.AccountId) ) {
                Account acc = mapAccount.get(oCon.AccountId);
                if(acc.Contacts.size() > 0 ) {
                    oCon.addError('You cant add duplicate contact for this account'); 
                }
            }
        }
    }
}

Trigger:
trigger OneContactPerAcc on Contact (before insert) {
    
    OneContactHandler handler = new OneContactHandler();
	
	if(Trigger.isBefore) {
		if(Trigger.isInsert)
			handler.onBeforeInsert(Trigger.new);		
	}
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Regards,
Khan Anas
This was selected as the best answer