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 help

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 error written so far. I am getting lot of errors while creating or saving any contact record.

Could you please help me as I am pretty new to Apex.

trigger contactcheckbox on Contact ( before insert,after update) {

        Map<Id, List<Contact>> AcctContactList = new Map<Id, List<Contact>>();
        Set<Id> AcctIds = new Set<Id>(); 

        List<Account> AcctList = new List<Account>();
        List<Contact> ConList = new List<Contact>();
   
    if(trigger.isInsert || trigger.isUPdate) {
        for(Contact Con : trigger.New) {
            AcctIds.add(Con.AccountId);    
        } 
    }
   
   ConList = [SELECT Id, AccountId FROM Contact WHERE Potential_Contact__c=true and AccountId IN : AcctIds];
   
    for(Contact Con : ConList) {
        if(!ConList== Null){
            con.adderror('there is an existing record');
        }
  
}
}
Best Answer chosen by Deek
Chidambar ReddyChidambar Reddy
Hi Deek,

Try the following

//put before update
trigger contactcheckbox on Contact ( before insert, before update) {

       Set<Id> accountIds = new Set<Id>();
for(Contact c : trigger.new){
if((c.AccountId!=null)&&(!accountIds.Contains(c.AccountId))){
accountIds.add(c.AccountId); //getting unique account Ids
}
}

Map<Id,Account> accountMap =new Map<Id,Account>( [Select Id, (select Id,Name,Potential_Contact__c from Contacts ) from Account Where Id IN : accountIds]) ;

for(Contact con : trigger.new){ //iterate through inserting or updating contacts
if(con.AccountId!=null){
if(accountMap.get(con.AccountId).Contacts.size()>0){
//if there are contacts for the account selected

for(Contact others : accountMap.get(con.AccountId).Contacts){
//all contacts of selected account
if((con.Id!=others.Id)&&(others.Potential_Contact__c==true)){
con.addError('There exist one more Potential Contact for this Account.  Contact Name : \' '+others.Name+'\'');

}
}
}

}
}

}


Thank you
Choose it as Best Answer if it resolved your issue.

All Answers

Ramu_SFDCRamu_SFDC
Here is the code you can try. Checkbox__c is the checkbox field on contacts.

trigger Contactcheckbox_check_trigger 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 checkbox__c=true]; 
        if(cons.size()>0){
          relcontactsmap.put(accid,cons);
        }
    }
   
    for(contact con1:trigger.new){
        if(con1.checkbox__c==true){
            if(relcontactsmap.containsKey(con1.AccountId)){
               con1.Checkbox__c.adderror('checkbox field already exist on another contact related to same account');
            }  
        }
    }
}

Let me know if this worked for you. Mark this solution as the best answer if it resolved your requirement for the benefit of others.
Chidambar ReddyChidambar Reddy
Hi Deek,

Try the following

//put before update
trigger contactcheckbox on Contact ( before insert, before update) {

       Set<Id> accountIds = new Set<Id>();
for(Contact c : trigger.new){
if((c.AccountId!=null)&&(!accountIds.Contains(c.AccountId))){
accountIds.add(c.AccountId); //getting unique account Ids
}
}

Map<Id,Account> accountMap =new Map<Id,Account>( [Select Id, (select Id,Name,Potential_Contact__c from Contacts ) from Account Where Id IN : accountIds]) ;

for(Contact con : trigger.new){ //iterate through inserting or updating contacts
if(con.AccountId!=null){
if(accountMap.get(con.AccountId).Contacts.size()>0){
//if there are contacts for the account selected

for(Contact others : accountMap.get(con.AccountId).Contacts){
//all contacts of selected account
if((con.Id!=others.Id)&&(others.Potential_Contact__c==true)){
con.addError('There exist one more Potential Contact for this Account.  Contact Name : \' '+others.Name+'\'');

}
}
}

}
}

}


Thank you
Choose it as Best Answer if it resolved your issue.
This was selected as the best answer
DeekDeek
Hi Chidambar and Ramu,

That worked perfectly fine. You guys are genius. Kudos to both. Cheers!
DeekDeek
Hi Chidambar and Ramu,

I have just noticed a new fallout in the code.

When I go to the Contacts page, and do an edit for an existing contact and save the record, still the customized error is thrown even though this contact is the only one present for an Account with the "potential contact" check box ticked.


Please help.
*****************************************************************************************************************************************

trigger Contactcheckbox_check_trigger 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 checkbox__c=true];
        if(cons.size()>0){
          relcontactsmap.put(accid,cons);
        }
    }
  
    for(contact con1:trigger.new){
        if(con1.checkbox__c==true){
            if(relcontactsmap.containsKey(con1.AccountId)){
               con1.Checkbox__c.adderror('checkbox field already exist on another contact related to same account');
            } 
        }
    }
}