You need to sign in to do that
Don't have an account?
sfdcChi2
how to prevent duplicate primary contact based on an Associated Account in the Contact Object
I have a checkbox field named primary_contact__c in the Contact object. I want an individual contact to be the only primary contact for any given account. hence once the checkbox field is checked for an individual account i want to prevent any other individual contact from having the same field checked for the same account. an attempt to do this should result in an error.
Can somebody please help with this. I'M thinking the only way to do this is with a trigger
Can somebody please help with this. I'M thinking the only way to do this is with a trigger
set<id>Accountid = new set <id>();
set<string>Memberid = new set <string>();
for (Member__c RVAcc: trigger.new)
{
if(RVAcc.Account__c!=null)
{
Accountid.add(RVAcc.Account__c);
}
if(RVAcc.Primary_Contact__c == 'Yes')
{
Memberid.add(RVAcc.Primary_Contact__c);
}
list<rvpe__RVMember__c>rvm5 = [select id,Primary_Contact__c,Account__c from Member__c
where Primary_Contact__c=:Memberid and Account__c=:Accountid ];
if (rvm5.size()>0)
for (Member__c RVMem: trigger.new)
{
if(RVAcc.Primary_Contact__c=='Yes'){
RVMem.addError('Primary Contact already exist');
}
}
}
}
This worked for me. thanks for the input guys!! (I used a picklist:yes/no for the primary contact field)
All Answers
Below code he;lps you to do this complete IF it helps you than please mark it as a solution and ENJOY APEX
set<id>Accountid = new set <id>();
set<string>Memberid = new set <string>();
for (Member__c RVAcc: trigger.new)
{
if(RVAcc.Account__c!=null)
{
Accountid.add(RVAcc.Account__c);
}
if(RVAcc.Primary_Contact__c == 'Yes')
{
Memberid.add(RVAcc.Primary_Contact__c);
}
list<rvpe__RVMember__c>rvm5 = [select id,Primary_Contact__c,Account__c from Member__c
where Primary_Contact__c=:Memberid and Account__c=:Accountid ];
if (rvm5.size()>0)
for (Member__c RVMem: trigger.new)
{
if(RVAcc.Primary_Contact__c=='Yes'){
RVMem.addError('Primary Contact already exist');
}
}
}
}
This worked for me. thanks for the input guys!! (I used a picklist:yes/no for the primary contact field)