You need to sign in to do that
Don't have an account?

trigger code for Avoid multiple primary contact using checkbox
Take one checkbox on contact object.
Have only one primary contact from multiple contact in Account.
Avoid multiple primary contact from Trigger.
Have only one primary contact from multiple contact in Account.
Avoid multiple primary contact from Trigger.
I think you can use the standard feature "Contact Roles".
In Account related List you will get this.
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AbL6IAK
set<id> accIdSet = new set<id>();
for(Contact con : trigger.new){
if(con.accountId!=null){
if(trigger.isUpdate){
Contact oldDetails=Trigger.oldMap.get(con.Id);
if(con.IsPrimaryContact__c && !oldDetails.IsPrimaryContact__c){
accIdSet.add(con.accountId);
}
}else{
if(con.IsPrimaryContact__c){
accIdSet.add(con.accountId);
}
}
}
}
if(accIdSet.size()>0){
List<Contact> contactList=[select id,AccountId,IsPrimaryContact__c,FirstName,LastName from Contact where IsPrimaryContact__c=true and accountid in:accIdSet];
//check if contact list has any value
if(contactList!=null && contactList.size()>0)
{
map<id, list<contact>> accToContactListMap = new map<id, list<contact>>();
for(Contact con: contactList){
List<Contact> dummyConList = accToContactListMap.get(con.accountId);
if(dummyConList == null){
dummyConList = new list<contact>();
}
dummyConList.add(con);
accToContactListMap.put(con.accountId, dummyConList);
}
system.debug('### accToContactListMap : '+accToContactListMap);
for(id accId:accToContactListMap.keyset()){
List<Contact> dummyConList = accToContactListMap.get(accId);
if(dummyConList != null || dummyConList.size() >=0){
Contact cont=dummyConList[0];
trigger.new[0].addError('Primary contact already assgined to [ '+cont.FirstName+' '+cont.LastName +' ]. You can not change the primary contact.');
}
}
}
}
}