You need to sign in to do that
Don't have an account?
priyanka mohapatra
Bulkification issue on Contact object
Question :To Show The Total Number Of Active Child Contacts On A Parent Account.
**Issue : For Bulk Operations I am getting an error ‘First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContactTrigger: execution of AfterInsert’**
• Contact Object Has A Custom Field (checkbox) called “Active”.
• Account Object Has A Custom Field (number) called “Active Contacts”.
**Code Written:**
public class ContactTriggerHandler {
public static void countOfActiveContacts(List<Contact> lstContact){
Set<Id> setAccId=new Set<Id>();
List<Account> lstAccount=new List<Account>();
If(lstContact!=null){
For(Contact cont:lstContact){
setAccId.add(cont.AccountId);
}}
If(setAccId!=null){
For(Account acc:[SELECT Id,Active_Contacts__c,(SELECT AccountId,Id,Active__c FROM Contacts WHERE Active__c =True) FROM Account WHERE Id IN:setAccId]){
acc.Active_Contacts__c=acc.Contacts.size();
lstAccount.add(acc);
}
}
Update lstAccount;
}
}
**My Test Calss Code:**
@isTest
private class ContactTriggerHandlerTest {
@isTest static void countOfActiveContactsBulk() {
Account acct = new Account(Name='Test Account');
insert acct;
List<Contact> lstCont= New List<Contact>();
for(Integer i=0;i<200;i++)
{
Contact cont= new Contact(LastName='cont'+i,
AccountId=acct.Id,
Active__c=True);
lstCont.add(cont)
}
test.startTest();
insert lstCont;
update acct;
test.stopTest();
system.assertEquals(acct.Active_Contacts__c,200);
}
**Issue : For Bulk Operations I am getting an error ‘First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContactTrigger: execution of AfterInsert’**
• Contact Object Has A Custom Field (checkbox) called “Active”.
• Account Object Has A Custom Field (number) called “Active Contacts”.
**Code Written:**
public class ContactTriggerHandler {
public static void countOfActiveContacts(List<Contact> lstContact){
Set<Id> setAccId=new Set<Id>();
List<Account> lstAccount=new List<Account>();
If(lstContact!=null){
For(Contact cont:lstContact){
setAccId.add(cont.AccountId);
}}
If(setAccId!=null){
For(Account acc:[SELECT Id,Active_Contacts__c,(SELECT AccountId,Id,Active__c FROM Contacts WHERE Active__c =True) FROM Account WHERE Id IN:setAccId]){
acc.Active_Contacts__c=acc.Contacts.size();
lstAccount.add(acc);
}
}
Update lstAccount;
}
}
**My Test Calss Code:**
@isTest
private class ContactTriggerHandlerTest {
@isTest static void countOfActiveContactsBulk() {
Account acct = new Account(Name='Test Account');
insert acct;
List<Contact> lstCont= New List<Contact>();
for(Integer i=0;i<200;i++)
{
Contact cont= new Contact(LastName='cont'+i,
AccountId=acct.Id,
Active__c=True);
lstCont.add(cont)
}
test.startTest();
insert lstCont;
update acct;
test.stopTest();
system.assertEquals(acct.Active_Contacts__c,200);
}
try with below trigger code.
If this helps, Please mark it as best answer.
Thanks!!
Please post your full error msg here. So that it will be easy to help you. You apex handler looks good.
I hope you are calling this handle class from contact trigger and after transaction.
In test class check the below things:
Same kind of code you can find below please compare your code once.
http://vishvendrasfdc.blogspot.com/2016/06/how-to-count-number-of-child-records-in.html
https://sfdcgenius.com/write-a-trigger-to-count-the-number-of-related-child-records-on-the-parent/
Thanks,
Maharajan.C