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
Mike DeMilleMike DeMille 

Cannot insert update activate entity error! help

I've got a trigger on contact to update an account field with the count of child contacts meeting a certain criteria.  My trigger works upon testing, but my test class is giving me some huge errors.  FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, BuyingCenterCount: execution of AfterInsert

Here is my Trigger:

trigger BuyingCenterCount on Contact (after insert, after update, after delete, after undelete) {
   
   
 
    List<Contact> contacts = Trigger.isDelete ? Trigger.old : Trigger.new;

  
    Set<Id> acctIds = new Set<Id>();
   
    for (Contact c : contacts) {
     
        if (c.AccountId != null) {
            acctIds.add(c.AccountId);
        }
    }
   
    List<Account> acctsToRollup = new List<Account>();
    
    
    for (AggregateResult ar : [SELECT AccountId AcctId, Count(id) ContactCount 
                               FROM Contact 
                               WHERE AccountId in: acctIds 
                               AND OP_Buying_Center__c = True
                               GROUP BY AccountId]){
        Account a = new Account();
        a.Id = (Id) ar.get('AcctId'); 
        a.Number_of_OP_BC_Contacts__c = (Integer) ar.get('ContactCount');
        acctsToRollup.add(a);
    }
    
    
    update acctsToRollup;

}





Here is my test class - I've simplified it to only try and insert an account and contact for now but still getting the same error. 

@isTest
private class BuyingCenterCountTest {
    
    @isTest static void BuyingCenterCount() {
        
        Account t = New Account();
        t.Name = 'Tester';
        t.BillingCountry = 'UK';
        t.Website = 'www.creep.gov';
        
        insert t;
        
        Contact c = New Contact();
        c.LastName = 'Terry';
        c.OP_Buying_Center__c = True;
        c.AccountId = t.Id;
      
        insert c;
    }

}



Does anyone have experience with these errors? 
Suraj TripathiSuraj Tripathi
Hi Mike,

Your, Code for trigger is working fine in my Org
with 100% code coverage.


User-added image


and the Error, you mentioned "Cannot insert update activate entity error!" is coming due to some other trigger.
You can check it by, making other triggers "Inactive" 

It will work fine with 100% coverage, If problem still arises, then you can ask me.

I hope this will solve your problem, if it solves your problem, please close this question by marking this as solved,  

Regards
Suraj