function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek Sharma 527Abhishek Sharma 527 

Null check in trigger

Hello there, I wrote code (by referring a website) to create custom roll up summary for account which will count number of contacts in account. it was evaluated and I was asked to optimize the code such that if contact has no account select while creating, for those contacts it should not go inside loop,
I'm new in development and actually not getting how to achieve that, can anyone plz help.

here's my code -

public class Contact_count_Handler
{     
public static void countContact(List<Contact> newList, List<Contact> oldList)
{
    Set<id> accountIds = new Set<id>();
    
if(newList!=null)  // null check
{
    for(contact c: newList)    
 {
        if(c.accountId!=null)            
{
            accountIds.add(c.accountId);
        }
    }
}

    if(oldList!=null)
    {
            for(contact c: oldList)
        {    
                if(c.accountId!=null)
            {
                    accountIds.add(c.accountId);
            }
        }
    }
List<Account> accounts = [SELECT Id, Number_of_linked_contact__c,(SELECT Id from Contacts) from Account where Id in : accountIds];
        
    if(!accounts.isEmpty())
        {
        for(Account acc: accounts)
            {
            acc.Number_of_linked_contact__c = acc.contacts.size();
            }
        }
    update accounts;
    }
}

===============================
trigger code -

trigger Contact_count_trigger on Contact (after insert, after update, after delete)
{
    if(trigger.isAfter){
        if(trigger.isInsert || trigger.isUpdate || trigger.isDelete)
{
            Contact_count_Handler.countContact(Trigger.new, Trigger.old);
        }
    }
}
Best Answer chosen by Abhishek Sharma 527
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

Then we need to check the Account null condition check when contact is created or updated or deleted.

You can say him, If the contact have linked to account then only trigger will fire otherwise it will not fire.
 
rigger ContactCountOnAccount on Contact (after insert, after update, after dalete){
Set <Id> accountIds = new Set <Id>();
List <Account> lstAccountsToUpdate = new List <Account>();
 if(Trigger.isInsert){
    for(Contact con:trigger.new){
 // we checking if the contact have account or not
    if(con.AccountId !=Null){
        accountIds.add(con.accountID);
    }
 }
}
if(Trigger.isUpdate|| Trigger.isDelete){
    for(Contact con:trigger.old){
// we checking if the contact have account or not
      if(con.AccountId !=Null){
        accountIds.add(con.accountID);
    }
  }
}

for(Account acc:[SELECT Id,Name,Number_of_linked_contact__c,(Select Id from Contacts) from Account where Id IN: accountIds]){
    Account accObj = new Account ();
    accObj.Id = acc.Id;
    accObj.Number_of_linked_contact__c = acc.Contacts.size();
    lstAccountsToUpdate.add(accObj);
}

UPDATE lstAccountsToUpdate;
}

If this helps, Please mark it as best answer.

Thanks!!



 

All Answers

CharuDuttCharuDutt
Hii Abhishek 
Try Below Code
trigger NumberOfChild on Contact (After Insert,After Update,After Delete) {
   If(trigger.isAfter &&Trigger.isInsert){
        NumberOfChildTriggerHandler.insertMethod(trigger.new);
    }
    If(trigger.isAfter &&Trigger.isUpdate){
        NumberOfChildTriggerHandler.updateMethod(trigger.new,trigger.oldMap);
    }
     If(trigger.isAfter &&Trigger.isDelete){
        NumberOfChildTriggerHandler.deleteMethod(trigger.old);
    }
}

#################################################################################

public class NumberOfChildTriggerHandler {
 
    public static Void InsertMethod(list<Contact> lstCon ){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null){
                   setAccIds.add(con.AccountId);      
            }   
        }
        for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
     
        acc.Number_of_linked_contact__c= acc.contacts.size();
       
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList; 
    }
    }
    public static Void UpdateMethod(list<Contact> lstCon,map<Id,Contact>oldmap ){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null && con.AccountId != oldMap.get(con.Id).AccountId){
                   setAccIds.add(con.AccountId); 
                setAccIds.add(oldMap.get(con.Id).AccountId);
            }   
        }
        for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
     
        acc.Number_of_linked_contact__c= acc.contacts.size();
       
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList; 
    }
    }
    public static Void deleteMethod(list<Contact> lstCon){
        List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
         for(Contact con : lstCon){ 
            if(con.AccountId != null){
                   setAccIds.add(con.AccountId);      
            }   
        }
        for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
     
        acc.Number_of_linked_contact__c= acc.contacts.size();
       
        acclist.add(acc);
    }
    if(acclist.size()>0){
        update accList; 
    }
    }

}
Please Mark It As Best Asnwer If It Helps
Thank You!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

Try with below code.
trigger ContactCountOnAccount on Contact (after insert, after update, after dalete){
Set <Id> accountIds = new Set <Id>();
List <Account> lstAccountsToUpdate = new List <Account>();
 if(Trigger.isInsert){
    for(Contact con:trigger.new){
        accountIds.add(con.accountID);
    }
}
if(Trigger.isUpdate|| Trigger.isDelete){
    for(Contact con:trigger.old){
        accountIds.add(con.accountID);
    }
}

for(Account acc:[SELECT Id,Name,Number_of_linked_contact__c,(Select Id from Contacts) from Account where Id IN: accountIds]){
    Account accObj = new Account ();
    accObj.Id = acc.Id;
    accObj.Number_of_linked_contact__c = acc.Contacts.size();
    lstAccountsToUpdate.add(accObj);
}

UPDATE lstAccountsToUpdate;
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
Abhishek Sharma 527Abhishek Sharma 527
Okay, actually my code is also working but my evaluator specifically asked if there's no account selected while creating contact then that contact has no account associated with it, so in that case what will happen.
he asked me to not to let those contacts go inside loop

may I know if your suggested code will achieve this and if you can elaborate the execution flow, it will be great help.

Regards,
Abhishek
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

Then we need to check the Account null condition check when contact is created or updated or deleted.

You can say him, If the contact have linked to account then only trigger will fire otherwise it will not fire.
 
rigger ContactCountOnAccount on Contact (after insert, after update, after dalete){
Set <Id> accountIds = new Set <Id>();
List <Account> lstAccountsToUpdate = new List <Account>();
 if(Trigger.isInsert){
    for(Contact con:trigger.new){
 // we checking if the contact have account or not
    if(con.AccountId !=Null){
        accountIds.add(con.accountID);
    }
 }
}
if(Trigger.isUpdate|| Trigger.isDelete){
    for(Contact con:trigger.old){
// we checking if the contact have account or not
      if(con.AccountId !=Null){
        accountIds.add(con.accountID);
    }
  }
}

for(Account acc:[SELECT Id,Name,Number_of_linked_contact__c,(Select Id from Contacts) from Account where Id IN: accountIds]){
    Account accObj = new Account ();
    accObj.Id = acc.Id;
    accObj.Number_of_linked_contact__c = acc.Contacts.size();
    lstAccountsToUpdate.add(accObj);
}

UPDATE lstAccountsToUpdate;
}

If this helps, Please mark it as best answer.

Thanks!!



 
This was selected as the best answer