You need to sign in to do that
Don't have an account?
Deek
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');
}
}
}
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');
}
}
}
Try the following
//put before update
trigger contactcheckbox on Contact ( before insert, before update) {
Set<Id> accountIds = new Set<Id>();
for(Contact con : trigger.new){ //iterate through inserting or updating contacts
if(accountMap.get(con.AccountId).Contacts.size()>0){
//all contacts of selected account
}
}
Thank you
Choose it as Best Answer if it resolved your issue.
All Answers
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.
Try the following
//put before update
trigger contactcheckbox on Contact ( before insert, before update) {
Set<Id> accountIds = new Set<Id>();
for(Contact con : trigger.new){ //iterate through inserting or updating contacts
if(accountMap.get(con.AccountId).Contacts.size()>0){
//all contacts of selected account
}
}
Thank you
Choose it as Best Answer if it resolved your issue.
That worked perfectly fine. You guys are genius. Kudos to both. Cheers!
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');
}
}
}
}