I am creating a checkbox named 'Primary contact' for every contact. The logic is only one contact can be a primary contact in an account. If a new contact has updated as primary contact, then previous contacts should be unchecked from primary contact.how?
For this you can write a trigger on Contact and check before insert or update if Primary checkbox is checked in new value than find all the contacts related to corresponding account to that contact and check if particular contact is true then uncheck that contact record Primary contact field and update the record.
Please Select this as Best Answer if it helps you.
trigger validationPrimarycontact on Contact (after insert,after update) { List<Contact> acclist = new List<Contact>(); acclist=[select id, name,Account.name,Primary_Contact__c from Contact where Account.name='Burlington Textiles Corp of America']; for ( Contact s : trigger.new )
trigger validationPrimarycontact on Contact (before insert,before update) { List<Contact> acclist = new List<Contact>(); set<Id> accIdSet = new set<Id>(); for ( Contact s : trigger.new ){ if(s.AccountId != null) accIdSet.add(s.AccountId); }
for(Contact con : [select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN accIdSet AND Primary_Contact__c=: true]){ con.Primary_Contact__c = false; acclist.add(con);
In this code one thing is missing if i delete the primary contact than which contact id go to account primary contact field ... so after the delete the primary contact object you have to make another primary contact object
//TRIGGERS trigger makeprimary on Contact (after insert , after Update , before delete ) if(trigger.isAfter){ if(trigger.isInsert){ primarycontact.checkAfterInsert(trigger.new); } else if(trigger.isUpdate){ primarycontact.checkAfterUpdate(trigger.new); } } else if(trigger.isbefore){ if(trigger.isDelete){ primarycontact.checkBeforeDelete(Trigger.Old); } } }
//HANDLER CLASS public class primaryContact { public static void checkAfterInsert(List<Contact> clist){ Set<Id> sid = new Set<Id>(); String s = ''; List<Account> ac = new List<Account>(); for(Contact c : clist){ if(c.AccountId!= null){ if(c.KaranJain__Is_Primary__c == true && c.AccountId != null){ sid.add(c.AccountId); s = c.Id; } } } List<Contact>containsIsPrimary = [select name ,id , accountId , KaranJain__Is_Primary__c from contact where KaranJain__Is_Primary__c = true AND AccountId IN:sid];
} System.debug(containsIsPrimary.size()); update containsIsPrimary ; update ac; } public static void checkAfterUpdate(List<Contact> clist){ string s = ''; Set<id> sid = new Set<Id>(); for(Contact sc : clist ){ s = sc.Id; sid.add(sc.AccountId); } List<Contact>containsIsPrimary = [select name ,id , accountId , KaranJain__Is_Primary__c from contact where KaranJain__Is_Primary__c = true AND AccountId IN:sid]; List<Account> ac = new List<Account>(); for(integer i = 0; i< containsIsPrimary.size() ; i++){ if(containsIsPrimary[i].Id != s){ containsIsPrimary[i].KaranJain__Is_Primary__c = false ; } else if(containsIsPrimary[i].Id== s ){ containsIsPrimary[i].KaranJain__Is_Primary__c = true; ac.add(new Account(id = containsIsPrimary[i].accountId , KaranJain__Contact_Primary_Id__c = containsIsPrimary[i].id)); } } update ac; } public static void checkBeforeDelete(List<Contact> delclist){ list<Account> ac = new List<Account>(); Set<id> sid = new Set<Id>();
for(Contact c : delclist){ if(c.KaranJain__Is_Primary__c == true ){ sid.add(c.AccountId); } } List<Contact>delIsPrimary = [select name ,id , accountId , KaranJain__Is_Primary__c from contact where AccountId IN:sid AND KaranJain__Is_Primary__c = false limit 1 ]; for(contact c : delclist){ if(delIsPrimary.size() > 0 ){ for(contact con : delIsPrimary){ con.KaranJain__Is_Primary__c = true ; ac.add(new Account(id = con.accountId , KaranJain__Contact_Primary_Id__c = con.id)); } } else { c.addError('Cannot delete becouse this is a last contact that is primary first you have to uncheck the primary contact field '); } }
trigger validationPrimarycontact1 on Contact (after insert,after update) {
List<Contact> acclist = new List<Contact>();
set<Id> accIdSet = new set<Id>();
Set<Id> ContactIds = new Set<Id>();
if(Trigger.IsUpdate){
for ( Contact s : trigger.new ){
if(s.AccountId != null)
accIdSet.add(s.AccountId);
Contact oldcon = Trigger.oldMap.get(s.Id);
if(oldcon.id != null)
ContactIds.add(oldcon.id);
}
acclist=[select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN : accIdSet AND Id NOT IN : ContactIds];
system.debug('*******Runish'+acclist);
List<Contact> conlist=new List<Contact>();
Contact c1=new Contact();
/*for ( Contact s : trigger.new ){
c1=Trigger.newMap.get(s.id);
system.debug('*******Runish'+c1.Name);
conlist.add(c1);
}*/
if(checkRecursive.runOnce()){
for ( Contact s : Trigger.new)
{
if(s.Primary_Contact__c ==true)
{
for(Contact a :acclist)
{
a.Primary_Contact__c =false;
c1=a;
}
update c1;
}
}
update acclist;
}
}
////next is trigger for after insert
if(Trigger.isInsert){
for ( Contact s : trigger.new ){
if(s.AccountId != null)
accIdSet.add(s.AccountId);
Contact oldcon = Trigger.newMap.get(s.Id);
if(oldcon.id != null)
ContactIds.add(oldcon.id);
}
acclist=[select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN : accIdSet AND Id NOT IN : ContactIds];
system.debug('*******Runish'+acclist);
List<Contact> conlist=new List<Contact>();
Contact c1=new Contact();
if(checkRecursive.runOnce()){
for ( Contact s : Trigger.new)
{
if(s.Primary_Contact__c ==true)
{
for(Contact a :acclist)
{
a.Primary_Contact__c =false;
c1=a;
}
update c1;
}
}
update acclist;
}
}
}
All Answers
For this you can write a trigger on Contact and check before insert or update if Primary checkbox is checked in new value than find all the contacts related to corresponding account to that contact and check if particular contact is true then uncheck that contact record Primary contact field and update the record.
Please Select this as Best Answer if it helps you.
I tried this code.But its not working.
trigger validationPrimarycontact on Contact (after insert,after update) {
List<Contact> acclist = new List<Contact>();
acclist=[select id, name,Account.name,Primary_Contact__c from Contact where Account.name='Burlington Textiles Corp of America'];
for ( Contact s : trigger.new )
{
// ContactIds.add(s.id);
if(s.Primary_Contact__c ==true)
{
for(Contact a :acclist)
{
a.Primary_Contact__c = false;
acclist.add(a);
}
update acclist;
s.Primary_Contact__c=true;
update s;
}
}
}
trigger validationPrimarycontact on Contact (before insert,before update) {
List<Contact> acclist = new List<Contact>();
set<Id> accIdSet = new set<Id>();
for ( Contact s : trigger.new ){
if(s.AccountId != null)
accIdSet.add(s.AccountId);
}
for(Contact con : [select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN accIdSet AND Primary_Contact__c=: true]){
con.Primary_Contact__c = false;
acclist.add(con);
}
update acclist;
}
Thanks,
~KR
thanks for ur suggestion.I solved it through trigger(after insert,after update).Though your code was helpful to some extect.
Thanks,
Runish
Please mark this as solved.
Thanks,
~KR
trigger validationPrimarycontact1 on Contact (after insert,after update) {
List<Contact> acclist = new List<Contact>();
set<Id> accIdSet = new set<Id>();
Set<Id> ContactIds = new Set<Id>();
if(Trigger.IsUpdate){
for ( Contact s : trigger.new ){
if(s.AccountId != null)
accIdSet.add(s.AccountId);
Contact oldcon = Trigger.oldMap.get(s.Id);
if(oldcon.id != null)
ContactIds.add(oldcon.id);
}
acclist=[select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN : accIdSet AND Id NOT IN : ContactIds];
system.debug('*******Runish'+acclist);
List<Contact> conlist=new List<Contact>();
Contact c1=new Contact();
/*for ( Contact s : trigger.new ){
c1=Trigger.newMap.get(s.id);
system.debug('*******Runish'+c1.Name);
conlist.add(c1);
}*/
if(checkRecursive.runOnce()){
for ( Contact s : Trigger.new)
{
if(s.Primary_Contact__c ==true)
{
for(Contact a :acclist)
{
a.Primary_Contact__c =false;
c1=a;
}
update c1;
}
}
update acclist;
}
}
////next is trigger for after insert
if(Trigger.isInsert){
for ( Contact s : trigger.new ){
if(s.AccountId != null)
accIdSet.add(s.AccountId);
Contact oldcon = Trigger.newMap.get(s.Id);
if(oldcon.id != null)
ContactIds.add(oldcon.id);
}
acclist=[select id, name,Account.name,Primary_Contact__c from Contact where AccountId IN : accIdSet AND Id NOT IN : ContactIds];
system.debug('*******Runish'+acclist);
List<Contact> conlist=new List<Contact>();
Contact c1=new Contact();
if(checkRecursive.runOnce()){
for ( Contact s : Trigger.new)
{
if(s.Primary_Contact__c ==true)
{
for(Contact a :acclist)
{
a.Primary_Contact__c =false;
c1=a;
}
update c1;
}
}
update acclist;
}
}
}
trigger primCon on Contact ( before update, before insert) {
List<Id> accounts=new List<Id>();
for(contact c: Trigger.New){
if( c.AccountId!=Null)
accounts.add(c.AccountId);
}
List<contact> contacts= [select id, phone from contact WHERE Primary_contact__c= true AND AccountId IN:accounts];
for(contact c:trigger.New){
if(c.Primary_contact__c==true){
for(contact con: contacts){
con.Primary_contact__c=false;
}
update contacts;
}
}
}
so after the delete the primary contact object you have to make another primary contact object
trigger makeprimary on Contact (after insert , after Update , before delete )
if(trigger.isAfter){
if(trigger.isInsert){
primarycontact.checkAfterInsert(trigger.new);
}
else if(trigger.isUpdate){
primarycontact.checkAfterUpdate(trigger.new);
}
}
else if(trigger.isbefore){
if(trigger.isDelete){
primarycontact.checkBeforeDelete(Trigger.Old);
}
}
}
//HANDLER CLASS
public class primaryContact {
public static void checkAfterInsert(List<Contact> clist){
Set<Id> sid = new Set<Id>();
String s = '';
List<Account> ac = new List<Account>();
for(Contact c : clist){
if(c.AccountId!= null){
if(c.KaranJain__Is_Primary__c == true && c.AccountId != null){
sid.add(c.AccountId);
s = c.Id;
}
}
}
List<Contact>containsIsPrimary = [select name ,id , accountId , KaranJain__Is_Primary__c from contact where KaranJain__Is_Primary__c = true AND AccountId IN:sid];
for(integer i = 0 ; i < containsIsPrimary.size() ; i++){
if(containsIsPrimary.size() >= 2 ){
if(containsIsPrimary[i].id != s){
containsIsPrimary[i].KaranJain__Is_Primary__c = false;
}
else if(containsIsPrimary[i].id == s){
ac.add(new Account(id = containsIsPrimary[i].accountId , KaranJain__Contact_Primary_Id__c = containsIsPrimary[i].id));
}
}
else if(containsIsPrimary.size() == 1){
ac.add(new Account(id = containsIsPrimary[i].accountId , KaranJain__Contact_Primary_Id__c = containsIsPrimary[i].id));
}
}
System.debug(containsIsPrimary.size());
update containsIsPrimary ;
update ac;
}
public static void checkAfterUpdate(List<Contact> clist){
string s = '';
Set<id> sid = new Set<Id>();
for(Contact sc : clist ){
s = sc.Id;
sid.add(sc.AccountId);
}
List<Contact>containsIsPrimary = [select name ,id , accountId , KaranJain__Is_Primary__c from contact where KaranJain__Is_Primary__c = true AND AccountId IN:sid];
List<Account> ac = new List<Account>();
for(integer i = 0; i< containsIsPrimary.size() ; i++){
if(containsIsPrimary[i].Id != s){
containsIsPrimary[i].KaranJain__Is_Primary__c = false ;
}
else if(containsIsPrimary[i].Id== s ){
containsIsPrimary[i].KaranJain__Is_Primary__c = true;
ac.add(new Account(id = containsIsPrimary[i].accountId , KaranJain__Contact_Primary_Id__c = containsIsPrimary[i].id));
}
}
update ac;
}
public static void checkBeforeDelete(List<Contact> delclist){
list<Account> ac = new List<Account>();
Set<id> sid = new Set<Id>();
for(Contact c : delclist){
if(c.KaranJain__Is_Primary__c == true ){
sid.add(c.AccountId);
}
}
List<Contact>delIsPrimary = [select name ,id , accountId , KaranJain__Is_Primary__c from contact where AccountId IN:sid AND KaranJain__Is_Primary__c = false limit 1 ];
for(contact c : delclist){
if(delIsPrimary.size() > 0 ){
for(contact con : delIsPrimary){
con.KaranJain__Is_Primary__c = true ;
ac.add(new Account(id = con.accountId , KaranJain__Contact_Primary_Id__c = con.id));
}
}
else {
c.addError('Cannot delete becouse this is a last contact that is primary first you have to uncheck the primary contact field ');
}
}
update ac;
update delIsPrimary;
}
}
TRY THIS ......!