Using the field IsPartner for the Account primary contact constraint is working.
In this case, I wanted that an account cannot be converted into a partner account if the account does have a primary contact set.
So, I used a trigger(before update) and throw an error message if there is an attempt to convert an account to partner and there is no primary contact role set.
The code below is an example so you will need to clean it up a bit, but is working. Also, note that I want it to do the check for only a particular Account type and this case is "Business Partner":
trigger AccountTrigger on Account (before update) {
if (Trigger.isBefore && Trigger.isUpdate) {
set<Id> accountIds = Trigger.oldMap.keySet();
List<Account> oldBusinessPartnerAccounts = [
SELECT Id, IsPartner, Type
FROM Account
WHERE Id IN :accountIds AND Type = 'Business Partner'
];
Map<Id, Account> oldBusinessPartnerAccountMap = new Map<Id, Account>(oldBusinessPartnerAccounts);
if (oldBusinessPartnerAccounts.size() > 0) {
set<Id> businessPartnerIdsWithPrimaryContactRole = new set<Id>();
set<Id> businessPartnerIdsWithOutPrimaryContactRole = new set<Id>();
for (AccountContactRole businessParterContactRole : [
SELECT Id, IsPrimary, Account.Id
FROM AccountContactRole
WHERE Account.Id IN :oldBusinessPartnerAccountMap.keySet() AND IsPrimary = true
]) {
businessPartnerIdsWithPrimaryContactRole.add(businessParterContactRole.Account.Id);
}
if (businessPartnerIdsWithPrimaryContactRole.size() == 0) {
businessPartnerIdsWithOutPrimaryContactRole = oldBusinessPartnerAccountMap.keySet();
} else {
for (Id businessPartnerAccountId : oldBusinessPartnerAccountMap.keySet()) {
if (!businessPartnerIdsWithPrimaryContactRole.contains(businessPartnerAccountId)) {
businessPartnerIdsWithOutPrimaryContactRole.add(businessPartnerAccountId);
}
}
}
for (Account targetAccount: Trigger.new) {
if ((oldBusinessPartnerAccountMap.get(targetAccount.Id).IsPartner == false) && (targetAccount.IsPartner == true)) {
if (businessPartnerIdsWithOutPrimaryContactRole.contains(targetAccount.Id)) {
targetAccount.addError('<br/><p style = "color:red;">If you want to convert this account into a partner account, you will need first to set a primary contact role to this account.</p>', false);
}
}
}
}
}
}