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

Set Contact as primary if one does not exist
Hello All,
I have the following code that only allows a single Primary Contact per Account. But what I would like to do is set the newly created Contact as Primary(Primary__c==True) if one does not exist. Can I utilize my existing Class or do I have to create a new one
Thank you for your time and help.
M
I have the following code that only allows a single Primary Contact per Account. But what I would like to do is set the newly created Contact as Primary(Primary__c==True) if one does not exist. Can I utilize my existing Class or do I have to create a new one
Thank you for your time and help.
M
public without sharing class ContactTrgHandler { public static void onBeforeInsert(List<Contact> trgNew) { Map<Id,Set<Id>> accountIdToNewContactRtMap = new Map<Id,Set<Id>>(); for(Contact c : trgNew) { if(String.isNotEmpty(c.AccountId) && c.Primary__c) { if(!accountIdToNewContactRtMap.containsKey(c.AccountId)) accountIdToNewContactRtMap.put(c.AccountId,new Set<Id>()); accountIdToNewContactRtMap.get(c.AccountId).add(c.RecordTypeId); } } if(!accountIdToNewContactRtMap.isEmpty()) { Map<Id,Set<Id>> existingContactRtMap = new Map<Id,Set<Id>>(); for(Account a : [SELECT Id,(SELECT RecordTypeId FROM Contacts WHERE Primary__c=true) FROM Account WHERE Id in :accountIdToNewContactRtMap.keySet()]) { existingContactRtMap.put(a.Id,new Set<Id>()); for(Contact c : a.Contacts) existingContactRtMap.get(a.Id).add(c.RecordTypeId); } for(Contact c : trgNew) { if(existingContactRtMap.containsKey(c.AccountId) && existingContactRtMap.get(c.AccountId).contains(c.RecordTypeId)) c.addError('There is already a primary contact for this record type.'); } } } public static void onBeforeUpdate(List<Contact> trgNew,Map<Id,Contact> oldMap) { Map<Id,Set<Id>> accountIdToNewContactRtMap = new Map<Id,Set<Id>>(); for(Contact c : trgNew) { if(String.isNotEmpty(c.AccountId) && c.Primary__c && !oldMap.get(c.Id).Primary__c) { if(!accountIdToNewContactRtMap.containsKey(c.AccountId)) accountIdToNewContactRtMap.put(c.AccountId,new Set<Id>()); accountIdToNewContactRtMap.get(c.AccountId).add(c.RecordTypeId); } } if(!accountIdToNewContactRtMap.isEmpty()) { Map<Id,Set<Id>> existingContactRtMap = new Map<Id,Set<Id>>(); for(Account a : [SELECT Id,(SELECT RecordTypeId FROM Contacts WHERE Primary__c=true) FROM Account WHERE Id in :accountIdToNewContactRtMap.keySet()]) { existingContactRtMap.put(a.Id,new Set<Id>()); for(Contact c : a.Contacts) existingContactRtMap.get(a.Id).add(c.RecordTypeId); } for(Contact c : trgNew) { if(existingContactRtMap.containsKey(c.AccountId) && existingContactRtMap.get(c.AccountId).contains(c.RecordTypeId)) c.addError('There is already a primary contact for this record type.'); } } } }
Check this
You can try below code, which will also update the primary contact in case of insert, update and delete. Apex trigger is like: Helper Class: Hope this helps you.
Thanks,
Neetu
Bhaswanthnaga, I will modify my existing code as you suggested and let you know how it works.
Thnaks again to you both.
M
Is the syntax below correct?
[SELECT Id,(SELECT Id, Id FROM Contacts WHERE Primary__c=true)
Also, do I need to create a new field on the Account Object? PrimaryContact__c?
Thanks
sorry that was [SELECT Id,(SELECT Id, recordtypeId FROM Contacts ..............
Yes, you have to create a new field if you want to maintain PC informaiton on Account
Cheers,
M
What would the list look like? And where would I insert the code since this has an Else statement?
Map<Id,Set<Id>> NewContactRtMap = new Map<Id,Set<Id>>();
for(Account a : [SELECT Id,(SELECT Id, recordtypeId FROM Contacts WHERE Primary__c=true) FROM Account WHERE Id Not In :accountIdToNewContactRtMap.keySet()])
Thanks,
M
Variable does not exist: ac - line 34
Variable does not exist: newContactRtMap - line 38
Any help is appreciated.
Use the above code
I am getting an error at line 35. ' newAccountRtMap.add(ac.Id, ac); '
Do we need to include the ac.id?
Method does not exist or incorrect signature: void add(Id, Account) from the type Map<Id,Account>
If there is not a Primary Contact for an Account and a user creates a contact. That contact needs to be the set as the Primary. Which means even if the user does not select the checkbox I nned to select the Primary checkbox and make the newly created Contact the Primary Contact.
The code as it stands is only looking for Contacts where the Primary is already checked and this may not be the case. If you have any other thoughts let me know. Thnaks for all your help.
Michael
Not sure why have you changed the code that I have kept in first place please use that code.
So the for loop stys correct?
for(Account ac : [SELECT Id,(SELECT id,email,RecordTypeId FROM Contacts WHERE Primary__c=true) FROM Account WHERE Id not in :accountIdToNewContactRtMap.keySet()])
Here is what I have with your recommendations:
Errors:
Variable does not exist: Primary_c
Variable does not exist: ac
Missing '<EOF>' at 'public'
Are these {} issues?
I will test it now and let you know.
Thanks for your patuence and help.
Michael
The list is empty so it is never goes into the If statements correct?