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
THUNDER CLOUDTHUNDER CLOUD 

How to make one contact as primary ?

Account has multiple contacts associated with it. I want to check one contact as primary and if new contact is added to account then that contact will be checked as primary.

How to achieve this ?
Best Answer chosen by THUNDER CLOUD
SandhyaSandhya (Salesforce Developers) 
Hi,

To achieve this, you need to create one custom checkbox field in contact as a "primary" so when you click that check box that contact must be the primary contact others not.And then you can write a trigger to check if primary contact is true then throw error else allow to add a contact.
 
trigger ContactMap on Contact (before Insert,After Update){ Set accountIdSet = new Set(); Set contactIdSet = new Set(); Map> accIdAndContactListMap = new Map>(); Map accIdAndContactCountMap = new Map();
List oldContactList = new list(); Integer Count =0;
if(trigger.IsInsert && trigger.IsBefore){

    for (Contact newcontact : trigger.new){

        if(newContact.Primary__c == true && newcontact.AccountId!=NULL ){

            if(!accountIdSet.Contains(newContact.AccountId)){            
                accountIdSet.add(newcontact.AccountId);                    
            }else{                   
                newContact.addError('Only one contact can be the primary.Must Not Add all contacts as primary contact');
            }                
        }
    }    

    if((accountIdSet !=NULL) && (accountIdSet.size()>0)){        
        OldContactList=    [SELECT Name,Primary__c,AccountId 
                            FROM Contact 
                            WHERE AccountId IN :accountIdSet AND Primary__c=TRUE];                                              

        for(Contact cnt : OldContactList){                                                 
            accIdAndContactCountMap.put(cnt.AccountId,1);                                                                                                                                                                      
        }
        system.debug('::::++++++'+accIdAndContactCountMap);                       
        for(Contact newcontact : trigger.new){

            if(newcontact.AccountId!=NULL && accIdAndContactCountMap.containsKey(newcontact.AccountId)){
            Count = accIdAndContactCountMap.get(newcontact.AccountId);
            if(Count==1){
                system.debug('+++++count------'+Count);                        
                newcontact.addError('One primary contact is already Available,Inserting another primary contact is not Possible.');
            }
            } 
        }
    }        
}

if(trigger.IsUpdate && trigger.IsAfter){
    for(Contact newupContact : Trigger.new){// validation for a particular contact whether it is already primary or not.

        if(newupContact.Primary__c == true && newupContact.AccountId!=NULL &&
         (Trigger.oldmap.get(newupContact.Id).Primary__c == FALSE || 
            Trigger.oldmap.get(newupContact.Id).Primary__c == TRUE )){ //unchecked primary, to check the new value

                if(!accountIdSet.Contains(newupContact.AccountId)){
                    accountIdSet.add(newupContact.AccountId);//primary checkbox newly checked value Id will be here.                                                                                                              
                    system.debug('++++++++'+accountIdSet);
                    contactIdSet.add(newupContact.Id);
                }else if(accountIdSet.Contains(newupContact.AccountId)){//If two contacts of same id is not allowed                
                    newupContact.addError('When updating or inserting, Only 1 contact can be the primary. uncheck all other Primary custom field');
                }
        }                                              
    }   
    if((accountIdSet !=NULL) && (accountIdSet.size()>0))
    {    
        OldContactList=    [SELECT Id,Name,Primary__c,AccountId 
                            FROM Contact 
                            WHERE AccountId IN :accountIdSet AND Primary__c=TRUE AND Id NOT IN :contactIdSet];               
        //system.debug('********'+OldContactList[0].AccountId);
        for(Contact cnt : OldContactList){                                
            accIdAndContactCountMap.put(cnt.AccountId,1);  
            system.debug('********'+accIdAndContactCountMap);
        }
        for(Contact newcontact : trigger.new){

            if(newcontact.AccountId!=NULL && accIdAndContactCountMap.containsKey(newcontact.AccountId)){
            Count = accIdAndContactCountMap.get(newcontact.AccountId);                                
                if((Count==1)&&(newcontact.Primary__c==TRUE)){
                  system.debug('+*-+*-+*-+*+-'+Count);  
                  newcontact.addError('update is not Possible. one primary contact is allowed for a Account and it is already Available');
                }
            }

        }            
    }        
}
}
I also suggest you refer below link for more information.

http://salesforce.stackexchange.com/questions/93001/only-one-primary-contact-for-an-account


Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 

All Answers

Suraj PSuraj P
Use Process Builder
SandhyaSandhya (Salesforce Developers) 
Hi,

To achieve this, you need to create one custom checkbox field in contact as a "primary" so when you click that check box that contact must be the primary contact others not.And then you can write a trigger to check if primary contact is true then throw error else allow to add a contact.
 
trigger ContactMap on Contact (before Insert,After Update){ Set accountIdSet = new Set(); Set contactIdSet = new Set(); Map> accIdAndContactListMap = new Map>(); Map accIdAndContactCountMap = new Map();
List oldContactList = new list(); Integer Count =0;
if(trigger.IsInsert && trigger.IsBefore){

    for (Contact newcontact : trigger.new){

        if(newContact.Primary__c == true && newcontact.AccountId!=NULL ){

            if(!accountIdSet.Contains(newContact.AccountId)){            
                accountIdSet.add(newcontact.AccountId);                    
            }else{                   
                newContact.addError('Only one contact can be the primary.Must Not Add all contacts as primary contact');
            }                
        }
    }    

    if((accountIdSet !=NULL) && (accountIdSet.size()>0)){        
        OldContactList=    [SELECT Name,Primary__c,AccountId 
                            FROM Contact 
                            WHERE AccountId IN :accountIdSet AND Primary__c=TRUE];                                              

        for(Contact cnt : OldContactList){                                                 
            accIdAndContactCountMap.put(cnt.AccountId,1);                                                                                                                                                                      
        }
        system.debug('::::++++++'+accIdAndContactCountMap);                       
        for(Contact newcontact : trigger.new){

            if(newcontact.AccountId!=NULL && accIdAndContactCountMap.containsKey(newcontact.AccountId)){
            Count = accIdAndContactCountMap.get(newcontact.AccountId);
            if(Count==1){
                system.debug('+++++count------'+Count);                        
                newcontact.addError('One primary contact is already Available,Inserting another primary contact is not Possible.');
            }
            } 
        }
    }        
}

if(trigger.IsUpdate && trigger.IsAfter){
    for(Contact newupContact : Trigger.new){// validation for a particular contact whether it is already primary or not.

        if(newupContact.Primary__c == true && newupContact.AccountId!=NULL &&
         (Trigger.oldmap.get(newupContact.Id).Primary__c == FALSE || 
            Trigger.oldmap.get(newupContact.Id).Primary__c == TRUE )){ //unchecked primary, to check the new value

                if(!accountIdSet.Contains(newupContact.AccountId)){
                    accountIdSet.add(newupContact.AccountId);//primary checkbox newly checked value Id will be here.                                                                                                              
                    system.debug('++++++++'+accountIdSet);
                    contactIdSet.add(newupContact.Id);
                }else if(accountIdSet.Contains(newupContact.AccountId)){//If two contacts of same id is not allowed                
                    newupContact.addError('When updating or inserting, Only 1 contact can be the primary. uncheck all other Primary custom field');
                }
        }                                              
    }   
    if((accountIdSet !=NULL) && (accountIdSet.size()>0))
    {    
        OldContactList=    [SELECT Id,Name,Primary__c,AccountId 
                            FROM Contact 
                            WHERE AccountId IN :accountIdSet AND Primary__c=TRUE AND Id NOT IN :contactIdSet];               
        //system.debug('********'+OldContactList[0].AccountId);
        for(Contact cnt : OldContactList){                                
            accIdAndContactCountMap.put(cnt.AccountId,1);  
            system.debug('********'+accIdAndContactCountMap);
        }
        for(Contact newcontact : trigger.new){

            if(newcontact.AccountId!=NULL && accIdAndContactCountMap.containsKey(newcontact.AccountId)){
            Count = accIdAndContactCountMap.get(newcontact.AccountId);                                
                if((Count==1)&&(newcontact.Primary__c==TRUE)){
                  system.debug('+*-+*-+*-+*+-'+Count);  
                  newcontact.addError('update is not Possible. one primary contact is allowed for a Account and it is already Available');
                }
            }

        }            
    }        
}
}
I also suggest you refer below link for more information.

http://salesforce.stackexchange.com/questions/93001/only-one-primary-contact-for-an-account


Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
This was selected as the best answer
UC InnovationUC Innovation
Just to add, typically, defining which contact is the primary contact is done using the Account Contact Role, which is a related list to the Account.
THUNDER CLOUDTHUNDER CLOUD
Hi Sandhya,

Your code is working with little modification.

Modified code :
 
trigger CheckOnePrimaryContact on Contact (before insert,after update) 
{
	Set<ID> accountIdSet = new Set<ID>(); 
    Set<ID> contactIdSet = new Set<ID>();
 	Map<ID,List<Contact>> accIdAndContactListMap = new Map<ID,List<Contact>>(); 
 	Map<ID,integer> accIdAndContactCountMap = new Map<ID,Integer>();
	List<Contact> oldContactList = new list<Contact>(); 
    Integer Count =0;

	if(trigger.IsInsert && trigger.IsBefore)
	{	
    for (Contact newcontact : trigger.new)
    {
       if(newContact.Primary__c == true && newcontact.AccountId!=NULL )
       {
            if(!accountIdSet.Contains(newContact.AccountId))
            {           
                accountIdSet.add(newcontact.AccountId);                   
            }
           else
           {                  
                newContact.addError('Only one contact can be the primary.Must Not Add all contacts as primary contact');
            }               
        }
    }   
    if((accountIdSet !=NULL) && (accountIdSet.size()>0))
    {       
        OldContactList=    [SELECT Name,Primary__c,AccountId FROM Contact WHERE AccountId IN :accountIdSet AND Primary__c=TRUE];                                             
        for(Contact cnt : OldContactList)
        {                                                
            accIdAndContactCountMap.put(cnt.AccountId,1);                                                                                                                                                                     
        }
        system.debug('::::++++++'+accIdAndContactCountMap);                      
        for(Contact newcontact : trigger.new)
        {
            if(newcontact.AccountId!=NULL && accIdAndContactCountMap.containsKey(newcontact.AccountId))
            {
            Count = accIdAndContactCountMap.get(newcontact.AccountId);
                if(Count==1)
                {
                    system.debug('+++++count------'+Count);                       
                    newcontact.addError('One primary contact is already Available,Inserting another primary contact is not Possible.');
                }
            }
        }
    }       
}

if(trigger.IsUpdate && trigger.IsAfter)
{
   for(Contact newupContact : Trigger.new)
   {
       // validation for a particular contact whether it is already primary or not.
        if(newupContact.Primary__c == true && newupContact.AccountId!=NULL &&  
           (Trigger.oldmap.get(newupContact.Id).Primary__c == FALSE ||
            Trigger.oldmap.get(newupContact.Id).Primary__c == TRUE ))
        { 
            //unchecked primary, to check the new value

                if(!accountIdSet.Contains(newupContact.AccountId))
                {
                    accountIdSet.add(newupContact.AccountId);//primary checkbox newly checked value Id will be here.                                                                                                             

                    system.debug('++++++++'+accountIdSet);

                    contactIdSet.add(newupContact.Id);

                }else if(accountIdSet.Contains(newupContact.AccountId))
                {
                    //If two contacts of same id is not allowed               

                    newupContact.addError('When updating or inserting, Only 1 contact can be the primary. uncheck all other Primary custom field');
                }
        }                                             
    }  

    if((accountIdSet !=NULL) && (accountIdSet.size()>0))
    {   
        OldContactList=    [SELECT Id,Name,Primary__c,AccountId FROM Contact
                            WHERE AccountId IN :accountIdSet AND Primary__c=TRUE AND Id NOT IN :contactIdSet];              

        //system.debug('********'+OldContactList[0].AccountId);

        for(Contact cnt : OldContactList)
        {                               
            accIdAndContactCountMap.put(cnt.AccountId,1); 
            system.debug('********'+accIdAndContactCountMap);
        }
        for(Contact newcontact : trigger.new)
        {
            if(newcontact.AccountId!=NULL && accIdAndContactCountMap.containsKey(newcontact.AccountId))
            {
            Count = accIdAndContactCountMap.get(newcontact.AccountId);                               
                if((Count==1)&&(newcontact.Primary__c==TRUE))
                {
                  system.debug('+*-+*-+*-+*+-'+Count); 
                  newcontact.addError('update is not Possible. one primary contact is allowed for a Account and it is already Available');
                }
            }
        }           
    }       
}
}

Thanks & Regards,

THUNDER CLOUD