0

I am trying to put together a test class for a Visualforce page controller, but I keep getting the error message. Does anyone know how I can fix?

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityLineItemTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.OpportunityLineItemTrigger: line 3, column 1: [] Stack Trace Class.VF_opportunityProductEntryTests.theTests: line 55, column 1

TEST CLASS

@istest
private class VF_opportunityProductEntryTests {

    static testMethod void theTests(){

    Account acc = new Account(Name = 'Test');
    insert acc;

    Contact con = new Contact(
        FirstName = 'Test',
        LastName = 'Name',
        AccountId = acc.Id,
        Phone = '555-555-5555',
        MailingStreet = 'TestStreet',
        MailingCity = 'TestCity',
        MailingState = 'TestState',
        MailingPostalCode = '00000',
        MailingCountry = 'MailingCountry',
        email = '[email protected]'
    );
    insert con;

    Opportunity opp = new Opportunity(
        AccountId = acc.Id,
        Contact__c = con.Id,
        Billing_Contact__c = con.Id,
        Name = 'Test Account - Opp',
        Amount = 2000,
        CloseDate = date.today(),
        StageName = 'Order Pending',
        Type = 'New',
    );
    insert opp;

    Pricebook2 pb22 = new Pricebook2(Name='testPB');
    insert pb22;

    Product2 newProd = new Product2(Name = 'test product', ProductCode = 'aaaaa', isActive = true, family = 'test family');
    insert newProd;

    PriceBookEntry pbEntry = new PriceBookEntry(
        UnitPrice = 300.00,
        PriceBook2Id = Test.getStandardPricebookId(),
        Product2Id = newProd.Id,
        UseStandardPrice = false,
        IsActive = true);
    insert pbEntry;

    OpportunityLineItem ol = new OpportunityLineItem(
        pricebookentryid=pbEntry.Id,
        TotalPrice = 2000,
        Quantity = 1,
        UnitPrice = 1000.00,
        OpportunityID = opp.Id);
    insert ol;

TRIGGER IN ERROR (TriggerActivationControl__c = Custom Setting)

trigger OpportunityLineItemTrigger on OpportunityLineItem (before insert, before update, after insert, after update) {
    TriggerActivationControl__c activation = TriggerActivationControl__c.getValues('OpportunityLineItemTrigger');
    Boolean isActive = activation.TriggerActive__c;
    if(isActive){
        new OpportunityLineItemTriggerHelper().process();
    }
}

CLASS

public class OpportunityLineItemTriggerHelper extends TriggerHelper{
    public override void processAfterInsert(){
        Set<Id> productIdSet = new Set<Id>();
        for(OpportunityLineItem productInstance : (List<OpportunityLineItem>)Trigger.new){
            productIdSet.add(productInstance.Id);
        }

        ShutterstockProductMethods.processShutterstockPublicAPIInfo(productIdSet);
    }

    public override void processAfterUpdate(){
        Set<Id> productIdSet = new Set<Id>();
        for(OpportunityLineItem productInstance : (List<OpportunityLineItem>)Trigger.new){
            OpportunityLineItem oldProductInstance = (OpportunityLineItem)Trigger.oldMap.get(productInstance.Id);
            if(productInstance.Image_ID__c != oldProductInstance.Image_ID__c){
                productIdSet.add(productInstance.Id);
            }
        }
        if(productIdSet.size() > 0){
            ShutterstockProductMethods.processShutterstockPublicAPIInfo(productIdSet);
        }
    }
}
3
  • 1
    Looks like the problem comes from your OpportunityLineItemTriggerHelper class. We can't help without your code. Sep 12, 2017 at 15:51
  • Sorry, just added helper class. Sep 12, 2017 at 15:57
  • 1
    Verifying that your code works as you expect it to work is the most important part of unit testing. It’s also one of the things that Force.com developers commonly neglect. Unit tests that do not verify the results of the code aren’t true unit tests. They are commonly referred to as smoke tests, which aren’t nearly as effective or informative as true unit tests. - How to Write Good Unit Tests
    – Adrian Larson
    Sep 12, 2017 at 16:19

1 Answer 1

2
Boolean isActive = activation.TriggerActive__c;

Your custom setting is null, because you didn't initialize your custom setting in the unit test. Add this line of code somewhere in your unit test (before the DML operation):

insert new TriggerActivationControl__c(Name='OpportunityLineItemTrigger', 
       TriggerActive__c=true);

As far as your code is concerned, you should probably modify your trigger to accept a possibly null setting:

Boolean isActive = activation == null || activation.TriggerActive__c;

You can read more about why this happens in Isolation of Test Data from Organization Data in Unit Tests.

3
  • Thanks. That got rid of the error and now gave me a new one: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]. Sep 12, 2017 at 19:06
  • 1
    @JohnNeilan You didn't set a price book on the opportunity, apparently.
    – sfdcfox
    Sep 12, 2017 at 19:25
  • Thanks. I did notice that and changed it, but I am still getting the error. I believe there is a process or WFR setting the pricebook when the Opp is inserted. Do you know how I can account for that when inserting my OpportunityLineItem? Sep 12, 2017 at 19:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .