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
Nathan Prats 22Nathan Prats 22 

Test Class - CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

Hi, 

I receive this error on a test class. 
It's due to this piece of code : "Database.update(opty)", but I don't get why. Any idea ? 
System.DmlException: Update failed. First exception on row 0 with id 0068E00000EEPmvQAH; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityTriggers: execution of AfterUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.OpportunityTriggers: line 24, column 1: []

My Test Class
 
@isTest
private class OpportunityTriggersTest {
    private static String name='Test Account';
    
    /// Nathan Prats 06/22/2018.
    @isTest static void PrimaryPartnerGrossMarginTest() {
        
        // Create accounts & contact & opportunity & opportunity contact role
        Account acct = TestUtils.CreateAccount('Standard'+ name);
        Account pacct = TestUtils.CreatePartnerAccount(' partner'+ name);
        Contact con  = TestUtils.CreateContact('firstName','lastName',acct);
        Opportunity opty = TestUtils.CreateOpportunity(acct, pacct);
        OpportunityContactRole ocr = TestUtils.CreateContactRoleOnOpportunity(con, opty);
        
        // Set partner information
        opty.Primary_Partner_Role__c='Reseller';
        opty.Primary_Partner_Margin__c=20;
        opty.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('SaaS Enterprise').getRecordTypeId();
        
        // Create a quote and sync it
        Quote qte = TestUtils.CreateQuote(con,opty);
        opty.SyncedQuoteId = qte.Id;
        Database.update(opty);
        
        // Create 2 products, a software product and a non software product
        Product2 pdct1= TestUtils.CreateProduct2('Product1','Software');
        Product2 pdct2= TestUtils.CreateProduct2('Product2','Customer Success');
        
        // Create 2 PriceBookEntries
        PricebookEntry pbe1 = TestUtils.createPricebookEntry(pdct1,100);
        PricebookEntry pbe2 = TestUtils.createPricebookEntry(pdct2,200);
        
        // Add 2 Quote Line Items, a software product and a non software product
        QuoteLineItem qli1 = TestUtils.createQuoteLineItem(pdct1,qte,1,Date.today(),100,pbe1);
        QuoteLineItem qli2 = TestUtils.createQuoteLineItem(pdct2,qte,1,Date.today(),200,pbe2);
        
        // Verify that the Primary_Partner_Gross_Margin__c = sum of software opportunity products * % margin
        // Should be 20 in this case, not 20+40.
        System.assertEquals(20, opty.Primary_Partner_Gross_Margin__c);
        
    }
}

 
Steven NsubugaSteven Nsubuga
It might be because you cannot call Database.update on a new record. Change Database.update(opty); to Database.upsert(opty);   
Rahul.MishraRahul.Mishra
As Steven said, you have created a new opportunity and trying to update that, either perform insert or update on it.

Thanks,
Rahul
Naval Sharma4Naval Sharma4
Hi Nathan, 

You need to check your code as it is clearly mentioned in the error message. Your trigger code is trying to do something with a field/variable which is null. Include try and catch in the trigger code and you will get the exact line where this error is coming from? Looks like it's Line - 24 in the OpportunityTriggers.