+ Start a Discussion
Runishkannaa GounderRunishkannaa Gounder 

I am creating a checkbox named 'Primary contact' for every contact. The logic is only one contact can be a primary contact in an account. If a new contact has updated as primary contact, then previous contacts should be unchecked from primary contact.how?

Best Answer chosen by Runishkannaa Gounder
Runishkannaa GounderRunishkannaa Gounder
here is the solution .This is how I solved it.


trigger validationPrimarycontact1 on Contact (after insert,after update) {
List<Contact> acclist = new List<Contact>();
set<Id> accIdSet = new set<Id>();
Set<Id> ContactIds = new Set<Id>();
if(Trigger.IsUpdate){
for ( Contact s : trigger.new ){
        if(s.AccountId != null)
        accIdSet.add(s.AccountId);   
        Contact oldcon = Trigger.oldMap.get(s.Id);       
        if(oldcon.id != null)
        ContactIds.add(oldcon.id);
       
                                }

acclist=[select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN : accIdSet AND Id NOT IN : ContactIds];

system.debug('*******Runish'+acclist);

List<Contact> conlist=new List<Contact>();
Contact c1=new Contact();
/*for ( Contact s : trigger.new ){
c1=Trigger.newMap.get(s.id);
system.debug('*******Runish'+c1.Name);
conlist.add(c1);
}*/
 if(checkRecursive.runOnce()){
        for ( Contact s : Trigger.new)

             {         
            if(s.Primary_Contact__c ==true)
                {
                  for(Contact a :acclist)
                      {
                         a.Primary_Contact__c =false;
                         c1=a;            
                      }
          
                    update c1; 
                }  
              }
           update acclist;
                       }
     }
        
        
      ////next is trigger for after insert
        
        
        if(Trigger.isInsert){
                for ( Contact s : trigger.new ){
                        if(s.AccountId != null)
                        accIdSet.add(s.AccountId);   
                        Contact oldcon = Trigger.newMap.get(s.Id);       
                        if(oldcon.id != null)
                        ContactIds.add(oldcon.id);
       
                                                }

acclist=[select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN : accIdSet AND Id NOT IN : ContactIds];

system.debug('*******Runish'+acclist);

List<Contact> conlist=new List<Contact>();
Contact c1=new Contact();

 if(checkRecursive.runOnce()){
for ( Contact s : Trigger.new)
         {      
            if(s.Primary_Contact__c ==true)
                {
                  for(Contact a :acclist)
                          {
                             a.Primary_Contact__c =false;
                              c1=a;            
                          }
                     update c1; 
        
        }  
        
       }
       update acclist;
       }
        
        }
         }
 

All Answers

Rahul Sangwan7341Rahul Sangwan7341
Hi,

For this you can write a trigger on Contact and check before insert or update if Primary checkbox is checked in new value than find all the contacts related to corresponding account to that contact and check if particular contact is true then uncheck that contact record Primary contact field and update the record.

Please Select this as Best Answer if it helps you.
Runishkannaa GounderRunishkannaa Gounder
Hi,
I tried this code.But its not working.

trigger validationPrimarycontact on Contact (after insert,after update) {
List<Contact> acclist = new List<Contact>();
acclist=[select id, name,Account.name,Primary_Contact__c from Contact where Account.name='Burlington Textiles Corp of America'];
for ( Contact s : trigger.new )

         {
       // ContactIds.add(s.id);
         
            if(s.Primary_Contact__c ==true)
        {
          for(Contact a :acclist)
          {
                 a.Primary_Contact__c = false;
                 acclist.add(a); 
                              
          }
          update acclist;
         s.Primary_Contact__c=true;
          update s; 
        
        }  
        
       }
         }
Rahul Sangwan7341Rahul Sangwan7341
run it before update.
Kunal01Kunal01
Try out below code 

trigger validationPrimarycontact on Contact (before insert,before update) {
List<Contact> acclist = new List<Contact>();
set<Id> accIdSet = new set<Id>();
for ( Contact s : trigger.new ){
    if(s.AccountId != null)
    accIdSet.add(s.AccountId);
}

for(Contact con : [select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN accIdSet AND Primary_Contact__c=: true]){
    con.Primary_Contact__c = false;
    acclist.add(con);
    
}
update acclist;

}

Thanks,
~KR
Runishkannaa GounderRunishkannaa Gounder
Hi kunal,
thanks for ur suggestion.I solved it through trigger(after insert,after update).Though your code was helpful to some extect.
Thanks,
Runish
Kunal01Kunal01
Hi Runish,

Please mark this as solved. 

Thanks,
~KR
 
Runishkannaa GounderRunishkannaa Gounder
here is the solution .This is how I solved it.


trigger validationPrimarycontact1 on Contact (after insert,after update) {
List<Contact> acclist = new List<Contact>();
set<Id> accIdSet = new set<Id>();
Set<Id> ContactIds = new Set<Id>();
if(Trigger.IsUpdate){
for ( Contact s : trigger.new ){
        if(s.AccountId != null)
        accIdSet.add(s.AccountId);   
        Contact oldcon = Trigger.oldMap.get(s.Id);       
        if(oldcon.id != null)
        ContactIds.add(oldcon.id);
       
                                }

acclist=[select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN : accIdSet AND Id NOT IN : ContactIds];

system.debug('*******Runish'+acclist);

List<Contact> conlist=new List<Contact>();
Contact c1=new Contact();
/*for ( Contact s : trigger.new ){
c1=Trigger.newMap.get(s.id);
system.debug('*******Runish'+c1.Name);
conlist.add(c1);
}*/
 if(checkRecursive.runOnce()){
        for ( Contact s : Trigger.new)

             {         
            if(s.Primary_Contact__c ==true)
                {
                  for(Contact a :acclist)
                      {
                         a.Primary_Contact__c =false;
                         c1=a;            
                      }
          
                    update c1; 
                }  
              }
           update acclist;
                       }
     }
        
        
      ////next is trigger for after insert
        
        
        if(Trigger.isInsert){
                for ( Contact s : trigger.new ){
                        if(s.AccountId != null)
                        accIdSet.add(s.AccountId);   
                        Contact oldcon = Trigger.newMap.get(s.Id);       
                        if(oldcon.id != null)
                        ContactIds.add(oldcon.id);
       
                                                }

acclist=[select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN : accIdSet AND Id NOT IN : ContactIds];

system.debug('*******Runish'+acclist);

List<Contact> conlist=new List<Contact>();
Contact c1=new Contact();

 if(checkRecursive.runOnce()){
for ( Contact s : Trigger.new)
         {      
            if(s.Primary_Contact__c ==true)
                {
                  for(Contact a :acclist)
                          {
                             a.Primary_Contact__c =false;
                              c1=a;            
                          }
                     update c1; 
        
        }  
        
       }
       update acclist;
       }
        
        }
         }
 
This was selected as the best answer
vishal reddy 20vishal reddy 20
This code works for me,

trigger primCon on Contact ( before update, before insert) {
   List<Id> accounts=new List<Id>();
   
    for(contact c: Trigger.New){
        if( c.AccountId!=Null)
            accounts.add(c.AccountId);
            
    }
        List<contact> contacts= [select id, phone from contact WHERE Primary_contact__c= true AND AccountId IN:accounts];
  
         
    for(contact c:trigger.New){
        if(c.Primary_contact__c==true){
             for(contact con: contacts){
                con.Primary_contact__c=false;
                
               
            }
            update contacts;
          
        }
       
    }
       }