-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);
    }
}
  • 2
    How does it differ from your previous question salesforce.stackexchange.com/questions/245740/… – Reshma Jan 8 '19 at 8:40
  • @Reshma, This code is working not like the previous question I'm a total beginner on salesforce coding so I want to better understand the code. I'll appreciate any help. – Jonathan Araneta Jan 8 '19 at 9:06
  • What do you meant by not working? – Reshma Jan 8 '19 at 9:18
  • I mean I replaced the previous one with this. – Jonathan Araneta Jan 8 '19 at 9:26
  • 2
    Your code can be optimise. Please go through Apex Code Best Practise and learn by yourself – Reshma Jan 8 '19 at 9:40
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.