+ Start a Discussion
uHaveOptionsuHaveOptions 

Primary Contact ID to populate in Account Object Primary_Contact__c lookup field Apex Trigger

Using this Apex Trigger, how can I add or show the primary contact in the Account object.  I know it can be shown using the contact related list but I would like it to show on the detail page instead as a lookup so users can just click on the contact link instead of looking for the primary contact in the related list.

Here's the code 
trigger PrimaryContact on Contact (before insert, before update) {
  
   set<id> getid = new set<id>();
    string contactId;
    List<Contact> conList = new List<Contact>();
  
    // Trigger Functionality
    if(Trigger.isInsert || Trigger.isUpdate) {
      
        for(Contact cont: Trigger.New) {
          
            if(cont.Primary_Contact__c == true) {
              
                getid.add(cont.AccountId);
                contactId = cont.id;
            }
        }
    }
  
    // Fetching the other Contact which has primary contact checked
    List<contact> cList = [select id, Primary_Contact__c from contact where accountid IN: getid AND Primary_Contact__c = true];
  
    // Unchecking the already checked primary contact
    if(cList.size() > 0) {
      
        for(Contact newClst: cList) {
          
            if(newClst.id != contactId) {
              
                newClst.Primary_Contact__c = false;
                conList .add(newClst);
            }
        }
    } 
    update conList; 
  }
Thanks in advance
 
Best Answer chosen by uHaveOptions
HARSHIL U PARIKHHARSHIL U PARIKH
It's because we need to query Phone and Email from Contact Object as well..

Try below code:
Trigger PrimaryContact on Contact (Before Insert, Before Update, 
                                   After Insert, After Update, After Delete, After UnDelete) {
    
     List<Id> actIds = New List<Id>();
     List<Contact> comingCons = New List<Contact>();
     List<Id> conIds = New List<Id>();
     
     If(Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate))
     {
         For(Contact Con : Trigger.New)
         {
             If(Con.Primary_Contact__c == TRUE)
             {
                actIds.add(Con.AccountId);
                conIds.add(Con.Id);
                comingCons.add(Con);
             }
             
         }
     }
     
     List<Account> allRelatedAccounts = [Select Id, (Select Id, Primary_Contact__c 
                                                     FROM Contacts WHERE Primary_Contact__c = TRUE AND Id !=: conIds)
                                                        FROM Account WHERE Id =: actIds];
     
     
     For(Contact EveryCon : comingCons)
     {
         For(Account EveryAccount : allRelatedAccounts){
             If(EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0){
                 EveryCon.addError('There is already a primary contact for this account');
             }
         }
     }                                  
                                       
     
                                   
    List<Id> accountIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Contact Con : Trigger.New){
            If(Con.AccountId != NULL){
                accountIds.add(Con.AccountId);
            }
        }
    }
    If(Trigger.IsDelete){
        For(Contact Con : Trigger.Old){
            If(Con.AccountId != NULL){
                accountIds.add(Con.AccountId);
            }
        }
    }
    
    List<Account> actFinalListToUpdte = New List<Account>();
    
    
    For(Account act : [Select ID, Primary_Contact_Id__c, Primary_Contact_First_and_Last_Name__c, Primary_Contact_Email__c, Primary_Contact_Phone__c,
                            (Select Id, FirstName, LastName, Phone, Email,
                                    Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE LIMIT 1)
                                FROM Account WHERE Id =: accountIds])
    {
        If(act.Contacts.size() > 0)
        {
            act.Primary_Contact_Id__c = act.Contacts[0].Id;
            act.Primary_Contact_First_and_Last_Name__c = act.Contacts[0].FirstName + ' ' + act.Contacts[0].LastName;
            act.Primary_Contact_Email__c = act.Contacts[0].Email;
            act.Primary_Contact_Phone__c = act.Contacts[0].Phone;
            actFinalListToUpdte.add(act);
        }
            
    }
    
    try{
        If(!actFinalListToUpdte.isEmpty()){
            update actFinalListToUpdte;
        }
    }Catch(Exception e){
        system.debug('Thrown Exception for PrimaryContact is: ' + e.getMessage());
    }
}

This should work!
 

All Answers

uHaveOptionsuHaveOptions
I tried this code and it doesn't work.  Also, I need to see the name and phone of the primary contact.  When I click on the primary contact check box I can ad multiple primary account becomes an issue to which primary contact info it will choose. Any ideas?
HARSHIL U PARIKHHARSHIL U PARIKH
Below trigger would prevent two primary contact being added to an account record. It will also populate the name of Primary contact with a HYPERLINK function.

Trigger Code:
 
Trigger PrimaryContact on Contact (Before Insert, Before Update, 
                                   After Insert, After Update, After Delete, After UnDelete) {
    
     List<Id> actIds = New List<Id>();
     List<Contact> comingCons = New List<Contact>();
     List<Id> conIds = New List<Id>();
     
     If(Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate))
     {
         For(Contact Con : Trigger.New)
         {
             If(Con.Primary_Contact__c == TRUE)
             {
                actIds.add(Con.AccountId);
             	conIds.add(Con.Id);
             	comingCons.add(Con);
             }
             
         }
     }
     
     List<Account> allRelatedAccounts = [Select Id, (Select Id, Primary_Contact__c 
                                                     FROM Contacts WHERE Primary_Contact__c = TRUE AND Id !=: conIds)
                                         				FROM Account WHERE Id =: actIds];
     
     
     For(Contact EveryCon : comingCons)
     {
         For(Account EveryAccount : allRelatedAccounts){
             If(EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0){
                 EveryCon.addError('There is already a primary contact for this account');
             }
         }
     }                                  
                                       
     
                                   
    List<Id> accountIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Contact Con : Trigger.New){
            If(Con.AccountId != NULL){
                accountIds.add(Con.AccountId);
            }
        }
    }
    If(Trigger.IsDelete){
        For(Contact Con : Trigger.Old){
            If(Con.AccountId != NULL){
                accountIds.add(Con.AccountId);
            }
        }
    }
    
    List<Account> actFinalListToUpdte = New List<Account>();
    
    
    For(Account act : [Select ID, Primary_Contact_Id__c, Primary_Contact_First_and_Last_Name__c,
                            (Select Id, FirstName, LastName,
                             		Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE LIMIT 1)
                                FROM Account WHERE Id =: accountIds])
    {
        If(act.Contacts.size() > 0)
        {
            act.Primary_Contact_Id__c = act.Contacts[0].Id;
            act.Primary_Contact_First_and_Last_Name__c = act.Contacts[0].FirstName + ' ' + act.Contacts[0].LastName;
            actFinalListToUpdte.add(act);
        }
            
    }
    
    try{
        If(!actFinalListToUpdte.isEmpty()){
            update actFinalListToUpdte;
        }
    }Catch(Exception e){
        system.debug('Thrown Exception for PrimaryContact is: ' + e.getMessage());
    }
}

Here how it looks,

User-added image

Now, if i try to Select primary conatct checkbox for Josh Davis then it would throw an error mesage. Here we go,
User-added image


Hope it helps! If it solves the query then kindly mark it Best Answer!
uHaveOptionsuHaveOptions

Thanks Govind!

I tried adding email and phone.  How can I achieve that?

I added

If(act.Contacts.size() > 0)
        {
            act.Primary_Contact_Id__c = act.Contacts[0].Id;
            act.Primary_Contact_First_and_Last_Name__c = act.Contacts[0].FirstName + ' ' + act.Contacts[0].LastName;
            act.Primary_Contact_Email__c = act.Contacts[0].Email;
            act.Primary_Contact_Phone__c = act.Contacts[0].Phone;
            actFinalListToUpdte.add(act);
        }
 

But it's giving an error of invalid data
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger PrimaryContact caused an unexpected exception, contact your administrator: PrimaryContact: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Phone: ()

Can't it just be a lookup value?  So I can build and add what ever field I need?

I'm going to try to build the test class as well.

Thanks again.

HARSHIL U PARIKHHARSHIL U PARIKH
It's because we need to query those data on SOQL as well.

Try this:
 
Trigger PrimaryContact on Contact (Before Insert, Before Update, 
                                   After Insert, After Update, After Delete, After UnDelete) {
    
     List<Id> actIds = New List<Id>();
     List<Contact> comingCons = New List<Contact>();
     List<Id> conIds = New List<Id>();
     
     If(Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate))
     {
         For(Contact Con : Trigger.New)
         {
             If(Con.Primary_Contact__c == TRUE)
             {
                actIds.add(Con.AccountId);
                conIds.add(Con.Id);
                comingCons.add(Con);
             }
             
         }
     }
     
     List<Account> allRelatedAccounts = [Select Id, (Select Id, Primary_Contact__c 
                                                     FROM Contacts WHERE Primary_Contact__c = TRUE AND Id !=: conIds)
                                                        FROM Account WHERE Id =: actIds];
     
     
     For(Contact EveryCon : comingCons)
     {
         For(Account EveryAccount : allRelatedAccounts){
             If(EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0){
                 EveryCon.addError('There is already a primary contact for this account');
             }
         }
     }                                  
                                       
     
                                   
    List<Id> accountIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Contact Con : Trigger.New){
            If(Con.AccountId != NULL){
                accountIds.add(Con.AccountId);
            }
        }
    }
    If(Trigger.IsDelete){
        For(Contact Con : Trigger.Old){
            If(Con.AccountId != NULL){
                accountIds.add(Con.AccountId);
            }
        }
    }
    
    List<Account> actFinalListToUpdte = New List<Account>();
    
    
    For(Account act : [Select ID, Primary_Contact_Id__c, Primary_Contact_First_and_Last_Name__c, act.Primary_Contact_Email__c, act.Primary_Contact_Phone__c,
                            (Select Id, FirstName, LastName,
                                    Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE LIMIT 1)
                                FROM Account WHERE Id =: accountIds])
    {
        If(act.Contacts.size() > 0)
        {
            act.Primary_Contact_Id__c = act.Contacts[0].Id;
            act.Primary_Contact_First_and_Last_Name__c = act.Contacts[0].FirstName + ' ' + act.Contacts[0].LastName;
            act.Primary_Contact_Email__c = act.Contacts[0].Email;
            act.Primary_Contact_Phone__c = act.Contacts[0].Phone;
            actFinalListToUpdte.add(act);
        }
            
    }
    
    try{
        If(!actFinalListToUpdte.isEmpty()){
            update actFinalListToUpdte;
        }
    }Catch(Exception e){
        system.debug('Thrown Exception for PrimaryContact is: ' + e.getMessage());
    }
}

Hope this helps!
uHaveOptionsuHaveOptions

I updated your code with the addition of the...
 

For(Account act : [Select ID, Primary_Contact_Id__c, Primary_Contact_First_and_Last_Name__c, act.Primary_Contact_Email__c, act.Primary_Contact_Phone__c,
                            (Select Id, FirstName, LastName,
                                    Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE LIMIT 1)
                                FROM Account WHERE Id =: accountIds])
    {
        If(act.Contacts.size() > 0)
        {
            act.Primary_Contact_Id__c = act.Contacts[0].Id;
            act.Primary_Contact_First_and_Last_Name__c = act.Contacts[0].FirstName + ' ' + act.Contacts[0].LastName;
            act.Primary_Contact_Email__c = act.Contacts[0].Email;
            act.Primary_Contact_Phone__c = act.Contacts[0].Phone;
            actFinalListToUpdte.add(act);
        }
I got an error...

Error: Compile Error:
Primary_Contact_First_and_Last_Name__c, act.Primary_Contact_Email__c, act.Primary_Contact_Phone__c
^
ERROR at Row:1:Column:75
Didn't understand relationship 'act' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 58 column 23

So I updated and removed "act." on act.Primary_Contact_Email__c, act.Primary_Contact_Phone__c,
 
For(Account act : [Select ID, Primary_Contact_Id__c, Primary_Contact_First_and_Last_Name__c, Primary_Contact_Email__c, Primary_Contact_Phone__c,
                            (Select Id, FirstName, LastName,
                                    Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE LIMIT 1)
                                FROM Account WHERE Id =: accountIds])
    {
        If(act.Contacts.size() > 0)
        {
            act.Primary_Contact_Id__c = act.Contacts[0].Id;
            act.Primary_Contact_First_and_Last_Name__c = act.Contacts[0].FirstName + ' ' + act.Contacts[0].LastName;
            act.Primary_Contact_Email__c = act.Contacts[0].Email;
            act.Primary_Contact_Phone__c = act.Contacts[0].Phone;
            actFinalListToUpdte.add(act);
        }


That passed but updating a record gives me an error. 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger PrimaryContact caused an unexpected exception, contact your administrator: PrimaryContact: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Email: ()

 

 

uHaveOptionsuHaveOptions
I updated the code and removed phone, email, and first/last, and Primary contact ID.  Instead I queried a different field (lookup) and took the contact.id to the lookup field.  Works well without adding additional lines of code for the extra fields.  Now I can just create extra fields without adding them to the trigger.  I will keep you posted.
HARSHIL U PARIKHHARSHIL U PARIKH
It's because we need to query Phone and Email from Contact Object as well..

Try below code:
Trigger PrimaryContact on Contact (Before Insert, Before Update, 
                                   After Insert, After Update, After Delete, After UnDelete) {
    
     List<Id> actIds = New List<Id>();
     List<Contact> comingCons = New List<Contact>();
     List<Id> conIds = New List<Id>();
     
     If(Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate))
     {
         For(Contact Con : Trigger.New)
         {
             If(Con.Primary_Contact__c == TRUE)
             {
                actIds.add(Con.AccountId);
                conIds.add(Con.Id);
                comingCons.add(Con);
             }
             
         }
     }
     
     List<Account> allRelatedAccounts = [Select Id, (Select Id, Primary_Contact__c 
                                                     FROM Contacts WHERE Primary_Contact__c = TRUE AND Id !=: conIds)
                                                        FROM Account WHERE Id =: actIds];
     
     
     For(Contact EveryCon : comingCons)
     {
         For(Account EveryAccount : allRelatedAccounts){
             If(EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0){
                 EveryCon.addError('There is already a primary contact for this account');
             }
         }
     }                                  
                                       
     
                                   
    List<Id> accountIds = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Contact Con : Trigger.New){
            If(Con.AccountId != NULL){
                accountIds.add(Con.AccountId);
            }
        }
    }
    If(Trigger.IsDelete){
        For(Contact Con : Trigger.Old){
            If(Con.AccountId != NULL){
                accountIds.add(Con.AccountId);
            }
        }
    }
    
    List<Account> actFinalListToUpdte = New List<Account>();
    
    
    For(Account act : [Select ID, Primary_Contact_Id__c, Primary_Contact_First_and_Last_Name__c, Primary_Contact_Email__c, Primary_Contact_Phone__c,
                            (Select Id, FirstName, LastName, Phone, Email,
                                    Primary_Contact__c FROM Contacts WHERE Primary_Contact__c = TRUE LIMIT 1)
                                FROM Account WHERE Id =: accountIds])
    {
        If(act.Contacts.size() > 0)
        {
            act.Primary_Contact_Id__c = act.Contacts[0].Id;
            act.Primary_Contact_First_and_Last_Name__c = act.Contacts[0].FirstName + ' ' + act.Contacts[0].LastName;
            act.Primary_Contact_Email__c = act.Contacts[0].Email;
            act.Primary_Contact_Phone__c = act.Contacts[0].Phone;
            actFinalListToUpdte.add(act);
        }
            
    }
    
    try{
        If(!actFinalListToUpdte.isEmpty()){
            update actFinalListToUpdte;
        }
    }Catch(Exception e){
        system.debug('Thrown Exception for PrimaryContact is: ' + e.getMessage());
    }
}

This should work!
 
This was selected as the best answer