+ Start a Discussion
sfdcChi2sfdcChi2 

how to prevent duplicate primary contact based on an Associated Account in the Contact Object

I have a checkbox field named primary_contact__c in the Contact object. I want an individual contact to be the only primary contact for any given account. hence once the checkbox field is checked for an individual account i want to prevent any other individual contact from having the same field checked for the same account. an attempt to do this should result in an error. 
Can somebody please help with this. I'M thinking the only way to do this is with a trigger
Best Answer chosen by sfdcChi2
sfdcChi2sfdcChi2
trigger RVMember_Primary_Contact on rvpe__RVMember__c (before insert,before update) {
   
set<id>Accountid = new set <id>();
set<string>Memberid = new set <string>();
for (Member__c RVAcc: trigger.new)
{
   if(RVAcc.Account__c!=null)
   {
     Accountid.add(RVAcc.Account__c); 
  }
   if(RVAcc.Primary_Contact__c == 'Yes')
   {
     Memberid.add(RVAcc.Primary_Contact__c); 
   }
   
list<rvpe__RVMember__c>rvm5 = [select id,Primary_Contact__c,Account__c from Member__c
                                where Primary_Contact__c=:Memberid and Account__c=:Accountid ];
    
     if (rvm5.size()>0)
        
         for (Member__c RVMem: trigger.new)
     {
         if(RVAcc.Primary_Contact__c=='Yes'){
             RVMem.addError('Primary Contact already exist');
         }
     }
}
}


This worked for me. thanks for the input guys!! (I used a picklist:yes/no for the primary contact field)

All Answers

AshlekhAshlekh
Hi 

Below code he;lps you to do this complete
trigger PrimartyContactTest on Contact (before insert,before update)
{
      Set<Id> conAccId = new set<Id>();
       for(contact con: trigger.new)
       {
         if(con.AccountId!=null)
			conAccId.add(con.AccountId);
       }
		Map<Id Account> accountMap = new Map<Id Account>([select id ,YOURFIELDACCONT(select id,YOURFIELD from contacts where primary_contact__c = true ) 
										from account where id in : conAccId]);
 	
		if(accountMap!= null && accountMap.size()>0)
		{
			for(Contact c : Trigger.new)
			{
				if(c.accountID !=null)
				{
					if(accountMap.containsKey(c.AccountID) && accountMap.get(c.AccountID).contacts.size()>0)
					{
						c.addError('This account has already primary contact');
					}
				}
			}

		}	
}
IF it helps you than please mark it as a solution and ENJOY APEX
Suneel#8Suneel#8
Gera,I guess your code doesn't let changes on primary contact to save in case if primary contact is getting updated.
sfdcChi2sfdcChi2
Gera, i get an error on the map line. it says unexpected token: 'select' - can you please take a look at it. i believe theres some syntax error somewhere in the line of code. thanks
sfdcChi2sfdcChi2
trigger RVMember_Primary_Contact on rvpe__RVMember__c (before insert,before update) {
   
set<id>Accountid = new set <id>();
set<string>Memberid = new set <string>();
for (Member__c RVAcc: trigger.new)
{
   if(RVAcc.Account__c!=null)
   {
     Accountid.add(RVAcc.Account__c); 
  }
   if(RVAcc.Primary_Contact__c == 'Yes')
   {
     Memberid.add(RVAcc.Primary_Contact__c); 
   }
   
list<rvpe__RVMember__c>rvm5 = [select id,Primary_Contact__c,Account__c from Member__c
                                where Primary_Contact__c=:Memberid and Account__c=:Accountid ];
    
     if (rvm5.size()>0)
        
         for (Member__c RVMem: trigger.new)
     {
         if(RVAcc.Primary_Contact__c=='Yes'){
             RVMem.addError('Primary Contact already exist');
         }
     }
}
}


This worked for me. thanks for the input guys!! (I used a picklist:yes/no for the primary contact field)
This was selected as the best answer
Suneel#8Suneel#8
sfdcChi2,you are using SOQL in for loop which is against APEX best practice.It will hit governor limits if you update more than 100 contacts at a time.You might have to reconsider before deploying it