You need to sign in to do that
Don't have an account?
Deek
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.');
}
}
}
}
}
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.');
}
}
}
}
}
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
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?
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.
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.');
}
}
}
}
}