-3

The function of this apex code is when Account has no Contacts the first contact created become the primary contact and when there is existing primary contact it cannot assign a other primary contact on account. Please help to optimized my code to easy understand.Thanks for the help.

public class CreatePrimaryContactHandler
{
    public static void createPrimary(List<Contact> contactList)
    {
       Set<Id> accountId = new Set<Id>();
        for(Contact c : contactList)
        {
            accountId.add(c.AccountId);
        }
        List<Contact> allContact = [SELECT Id, Account.Id, Primary_Contact__c FROM Contact WHERE (Account.Id IN : accountId)];
        Map<Id, List<Contact>> acctContactMap = new Map<Id, List<Contact>>();
            for(Id aId : accountId)
            {
             List<Contact> conList = new List<Contact>();
                for(Contact allcont : allContact)
                {
                    if(allcont.AccountId == aId)
                    {
                    conList.add(allcont);
                    }
                }
                acctContactMap.put(aId, conList);
            }
            for(Id accId : acctContactMap.keySet())
            {
                Integer count = 0;
                for(Contact c : contactList)
                {
                    if(c.AccountId == accId && c.Primary_Contact__c)
                    {
                        count++;
                    }
                }
                if(count <= 1)
                {
                    List<Contact> cList = acctContactMap.get(accId);
                    Contact primary;
                    Boolean isPrimary = false;
                    for(Contact cont : cList)
                    {
                        if(cont.Primary_Contact__c)
                        {
                            isPrimary = true;
                        }
                    }
                    for(Contact cont : contactList)
                    {
                        if(cont.AccountId == accId)
                        {
                            if(cont.Primary_Contact__c)
                            {
                                if(primary != null)
                                {
                                    primary.Primary_Contact__c = false;
                                     isPrimary = false;
                                }
                                else if(isPrimary)
                                {
                                cont.addError('Invalid Primary Contact. This Account has already an existing Primary Contact.');
                                }
                                    else
                                    {
                                        cont.Primary_Contact__c = true;
                                        isPrimary = true;
                                    }
                            }   
                                else if(!isPrimary)
                                {
                                    cont.Primary_Contact__c = true;
                                    isPrimary = true;
                                    primary = cont;
                                }
                        }
                    }
                }
                else
                {
                    contactList.get(0).addError('Invalid Primary Contact. This Account has already an existing Primary Contact.');
                }
        }  
    }
}

This is the Trigger

trigger CheckPrimaryContact on Contact (before insert,after update,before update){
    if(trigger.IsInsert && trigger.IsBefore){
        CreatePrimaryContactHandler.createPrimary(trigger.new);
    }
    if(trigger.IsBefore && trigger.IsUpdate){
        CreatePrimaryContactHandler.createPrimary(trigger.new);
    }
}

closed as too broad by David Reed, glls, Pranay Jaiswal, Derek F, battery.cord Jan 9 at 14:58

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2

I can give you some tips on how to optimse.

  1. Use AccountId instead of Account.Id in query
  2. Use Map to get results and avoid multiple collection variables and loops

So you can replace the below code block by one line.

List<Contact> allContact = [SELECT Id, Account.Id, Primary_Contact__c FROM Contact WHERE (Account.Id IN : accountId)];
Map<Id, List<Contact>> acctContactMap = new Map<Id, List<Contact>>();
for(Id aId : accountId)
{
 List<Contact> conList = new List<Contact>();
    for(Contact allcont : allContact)
    {
        if(allcont.AccountId == aId)
        {
        conList.add(allcont);
        }
    }
    acctContactMap.put(aId, conList);
}

Use Map of sObjects and replace the above lines by:

Map<Id, Account> acctContactMap = new Map<Id, Account>([SELECT Id, (SELECT Id, AccountId, Primary_Contact__c FROM Contacts) WHERE Id IN :accountId]);

Understand your logic and optimise the remaining based on Apex code best practices

Not the answer you're looking for? Browse other questions tagged or ask your own question.