0

There is an option on the Account object called "Enable As Partner", as you can see below:

enter image description here

I want to allow users to convert an account to partner as long as this account has an Account Primary Contact Role set.

I am thinking of approaching this requirement based on the Account standard field IsPartner. I would use a trigger (before update) and check if it is changing the value of this field and throw an error message if no primary contact role set.

Is this a good approach? Is there any other better approach?

Thank you in advance.

1
  • Sounds like a reasonable approach to me. It would certainly be easy to filter on the fields you've mentioned especially since the first one is a boolean.
    – crmprogdev
    Commented Jul 21, 2016 at 15:10

1 Answer 1

0

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);
                    }
                }
            }
        }

    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .