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
DeekDeek 

Contact trigger issue

Hi All,

We have a custom check box field in contact "Potential_Contact__c".

The requirement is when we add or update a contact for an account and if there is an existing contact with the above check box field checked, it should throw error or otherwise continue saving the contact record.

Below is the apex written so far which was shared by one of the group member.

Issue: I am getting the below custom error message during update operation while updating the contact which has the checked box ticked and the only contact associated to this account.

Please advise where I am doing wrong.



trigger CampaignContactCheckbox on Contact (before insert,before update) {
    set<id> accids=new set<id>();
    map<id,List<contact>> relcontactsmap=new map<id,List<contact>>();
    List<contact> cons=new List<contact>();
    List<Account> accts=new List<Account>();
    for(contact con:trigger.new){
        accids.add(con.AccountId);
    }
accts=[select id,name from Account where id=:accids];  
    for(Account accts1:accts){
        id accid=accts1.id;
        cons=[select id from contact where Accountid=:accid AND Potential_Contact__c=true];
        if(cons.size()>0){
          relcontactsmap.put(accid,cons);
        }
    }
  
    for(contact con1:trigger.new){
        if(Trigger.isInsert) {
         if(con1.Potential_Contact__c==true){
            if(relcontactsmap.containsKey(con1.AccountId)){
               con1.Potential_Contact__c.adderror('There is already a Potential Contact associated to this Account.');
            } 
         }
       }
   
    else if(Trigger.isUpdate) {
      if(con1.Potential_Contact__c==true){
               if(relcontactsmap.containsKey(con1.AccountId)){
                 con1.Potential_Contact__c.adderror('There is already a Potential Contact associated to this Account.');
                } 
            }

    }


}
}
bob_buzzardbob_buzzard
When updating, you need to exclude the contacts that the trigger is processing from the query for existing contacts, otherwise you will double count:

    accts=[select id,name from Account where id=:accids]; 
    for(Account accts1:accts){
        id accid=accts1.id;
        if (Trigger.isUpdate)
        {
            cons=[select id from contact where Accountid=:accid AND Potential_Contact__c=true AND id not in :trigger.old];
        }
        else
        {
           cons=[select id from contact where Accountid=:accid AND Potential_Contact__c=true]; 
        }
        if(cons.size()>0){
          relcontactsmap.put(accid,cons);
        }
    }

I'm also duty bound to point out that you have a SOQL query nested inside a for loop, which may cause problems with governor limits going forward - you should consider bulkifying this : http://wiki.developerforce.com/page/Best_Practice%3A_Bulkify_Your_Code
DeekDeek
Hi Bob,

I am sorry. I didnt understand your code due to lack of my knowledge in apex. I didnt get whether I should modify my code or use yours completely.


Where do I put the error message? Could you please advise? 
bob_buzzardbob_buzzard
The code that I pasted is a copy of a section of yours with a couple of additiona lines.  Find the following section:

accts=[select id,name from Account where id=:accids]; 
    for(Account accts1:accts){
        id accid=accts1.id;
        cons=[select id from contact where Accountid=:accid AND Potential_Contact__c=true];
        if(cons.size()>0){
          relcontactsmap.put(accid,cons);
        }
    }

and replace it with the code from above.
DeekDeek
Hi Bob,

Now I hope i got your point. Below is the final code which now works fine for update and not allowing to create duplicate potential contacts.

Pls let me know if there is some kind of refinement needs to be done.

Cheers!



trigger CampaignContactCheckbox on Contact (before insert,before update) {
    set<id> accids=new set<id>();
    map<id,List<contact>> relcontactsmap=new map<id,List<contact>>();
    List<contact> cons=new List<contact>();
    List<Account> accts=new List<Account>();
    for(contact con:trigger.new){
        accids.add(con.AccountId);
    }
accts=[select id,name from Account where id=:accids];
    for(Account accts1:accts){
        id accid=accts1.id;
        if (Trigger.isUpdate)
        {
            cons=[select id from contact where Accountid=:accid AND Potential_Contact__c=true AND id not in :trigger.old];
        }
        else
        {
           cons=[select id from contact where Accountid=:accid AND Potential_Contact__c=true];
        }
        if(cons.size()>0){
          relcontactsmap.put(accid,cons);
        }
    }
  
    for(contact con1:trigger.new){
        if(Trigger.isInsert) {
         if(con1.Potential_Contact__c==true){
            if(relcontactsmap.containsKey(con1.AccountId)){
               con1.Potential_Contact__c.adderror('There is already a Potential Contact associated to this Account.');
            } 
         }
       }
   
          else if(Trigger.isUpdate) {
            if(con1.Potential_Contact__c==true){
               if(relcontactsmap.containsKey(con1.AccountId)){
                 con1.Potential_Contact__c.adderror('There is already a Potential Contact associated to this Account.');
                } 
            }
   
          }
   
   
    }
}