I have written a trigger where if the user attempts to make a 2nd related contact on the account object a Primary Contact too, an error is thrown. I am trying to add syntax to my trigger so if the user adds a related contact to an Account that currently has 0 related contacts, this new contact will automatically become the primary contact. This is the code I have so far:
Trigger PrimaryContactTrigger on Contact (Before Insert, Before Update) {
List<Id> actIds = New List<Id>();
List<Contact> comingCons = New List<Contact>();
List<Id> conIds = New List<Id>();
If (Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate)) {
For (Contact Con : Trigger.New) {
If (Con.IsPrimary__c == TRUE) {
actIds.add(Con.AccountId);
conIds.add(Con.Id);
comingCons.add(Con);
}
}
}
List<Account> allRelatedAccounts = [
Select Id, (
Select Id, IsPrimary__c
FROM Contacts
WHERE IsPrimary__c = TRUE
AND Id != :conIds
)
FROM Account
WHERE Id = :actIds
];
For (Contact EveryCon : comingCons) {
For (Account EveryAccount : allRelatedAccounts) {
If (EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0) {
EveryCon.addError('THERE is already a primary contact for this account');
}
}
}
}

Robert, that is correct. The scenario was overlooked. Please use below code. It will solve the purpose.Trigger PrimaryContactTrigger on Contact (Before Insert, Before Update) {List<Id> actIds = New List<Id>();List<Contact> comingCons = New List<Contact>();List<Id> conIds = New List<Id>();List<Account> actIds1 = New List<Account>();If (Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate)) { For (Contact Con : Trigger.New) { If (Con.IsPrimary__c == TRUE) { actIds.add(Con.AccountId); conIds.add(Con.Id); comingCons.add(Con); }else{ actIds.add(Con.AccountId);
conIds.add(Con.Id);
List<Account> allRelatedAccounts = [
Select Id, (
Select Id, IsPrimary__c
FROM Contacts
WHERE IsPrimary__c = TRUE
AND Id != :conIds
)
FROM Account
WHERE Id = :actIds];
for (Account everyAccount : allRelatedAccounts){
If (everyAccount.Contacts.size() == 0){
Con.IsPrimary__c = True;
}
}
}
}
}List<Account> allRelatedAccounts = [ Select Id, ( Select Id, IsPrimary__c FROM Contacts WHERE IsPrimary__c = TRUE AND Id != :conIds ) FROM Account WHERE Id = :actIds];For (Contact EveryCon : comingCons) { For (Account EveryAccount : allRelatedAccounts) { If (EveryCon.AccountId == EveryAccount.Id && EveryAccount.Contacts.size() > 0) { EveryCon.addError('THERE is already a primary contact for this account'); } }}}