You need to sign in to do that
Don't have an account?
Abhishek 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);
}
}
}
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);
}
}
}
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.
If this helps, Please mark it as best answer.
Thanks!!
All Answers
Try Below Code Please Mark It As Best Asnwer If It Helps
Thank You!
Try with below code.
If this helps, Please mark it as best answer.
Thanks!!
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
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.
If this helps, Please mark it as best answer.
Thanks!!