You need to sign in to do that
Don't have an account?
CBN
how to write single SOQL quert to below trigger
Hi All,
Use single SOQL quert to fetch the list of contacts. use if condition to check if contact is active then set active contact accordingly.Put logic in helper class and call tis class from trigger
trigger countContact on Contact (after insert, after update, after delete, after undelete)
{
Set<Id> setAccountIds = new Set<Id>();
//Whenever your working with After Undelete operation you can access data through
//Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate)
{
for(Contact con : Trigger.new)
{
setAccountIds.add(con.AccountId);
}
}
if(Trigger.isDelete)
{
//if you use Trigger.new below in place of Trigger.old you will end up with
//System.NullPointerException:Attempt to de-reference a null object
for(Contact con : Trigger.old)
{
setAccountIds.add(con.AccountId);
}
}
List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id from contacts) from Account where Id in : setAccountIds];
for(Account acc :listAccs)
{
acc.Total_No_Of_Contacts__c = acc.contacts.size();
}
List<Account> listAccs1 = [Select id,name,No_of_active_contacts__c ,(select id from contacts where Contact_Roll__c = True) from Account where Id in : setAccountIds];
for(Account acc1 :listAccs1)
{
acc1.No_of_active_contacts__c = acc1.contacts.size();
}
update listAccs;
update listAccs1;
}
Kindly Support and Suggest
Thanks
Use single SOQL quert to fetch the list of contacts. use if condition to check if contact is active then set active contact accordingly.Put logic in helper class and call tis class from trigger
trigger countContact on Contact (after insert, after update, after delete, after undelete)
{
Set<Id> setAccountIds = new Set<Id>();
//Whenever your working with After Undelete operation you can access data through
//Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate)
{
for(Contact con : Trigger.new)
{
setAccountIds.add(con.AccountId);
}
}
if(Trigger.isDelete)
{
//if you use Trigger.new below in place of Trigger.old you will end up with
//System.NullPointerException:Attempt to de-reference a null object
for(Contact con : Trigger.old)
{
setAccountIds.add(con.AccountId);
}
}
List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id from contacts) from Account where Id in : setAccountIds];
for(Account acc :listAccs)
{
acc.Total_No_Of_Contacts__c = acc.contacts.size();
}
List<Account> listAccs1 = [Select id,name,No_of_active_contacts__c ,(select id from contacts where Contact_Roll__c = True) from Account where Id in : setAccountIds];
for(Account acc1 :listAccs1)
{
acc1.No_of_active_contacts__c = acc1.contacts.size();
}
update listAccs;
update listAccs1;
}
Kindly Support and Suggest
Thanks
All Answers
Please see below code -
Happy Coding!!
I Getting Below error will please find the Solution
Error: Compile Error: Extra ';', at '}'. at line 19 column 17
Thanks,
CBN
Handler Code is Not working its getting Error While iam testing
Error: Apex trigger countContact caused an unexpected exception, contact your administrator: countContact: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.CountContactHandler.countContacts: line 18, column 1
Please review once
Thanks
CBN
Thanks For Your Help
If you Dint mine Can you give Test Class For this Code Using System.Asserts staements
Thanks in Advance,