8

All the test classes are on opportunitylineitem. I tried testing each test classes all are running with code coverage more than 75% in sandbox as well as production. When I try to migrate the newly written trigger its throwing Error . When I checked the test classes, I could see the lines are opportunitylineitem.

At AccOppsegmentTestClass :The Error line is insert olii and in the test class it is declared as

OpportunityLineItem olii = new OpportunityLineItem (Quantity=2, OpportunityId=opp.Id, TotalPrice=10, PriceBookEntryId='01ud0000004YWFqAAO'); 
insert olii; 

At BlxOppclonecontrollerTestClass :The Error line is insert olli and in the test class it is declared as

OpportunityLineItem olli = new OpportunityLineItem (Quantity=2, OpportunityId=opp1.Id, TotalPrice=10, PriceBookEntryId='01ud0000004YWFqAAO'); 
insert olli; 

At Oli_multiple_Products_TestClass :The Error line is insert ooli and in the test class it is declared as

OpportunityLineItem ooli = new OpportunityLineItem (Quantity=2, OpportunityId=opp.Id, TotalPrice=10, PriceBookEntryId='01ud0000004YWFqAAO'); 
insert ooli;

Error as :

AccOppsegmentTestClass  validateAccOppsegment   System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Populate_Extension: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.Populate_Extension: line 15, column 1: []
    Stack Trace: Class.AccOppsegmentTestClass.validateAccOppsegment: line 19, column 1

Similarly for other test class i do get the same error.Any help very much appreciated

Trigger :

trigger Populate_Extension on OpportunityLineItem (Before Insert) {

 //List<OpportunityLineItem> oli= new List<OpportunityLineItem>();

  Set<Id> pbeIds = new Set<Id>();

 for (OpportunityLineItem opli : Trigger.new) {
    pbeIds.add(opli.PricebookEntryId);
 }

Map<Id, PricebookEntry> pbeMap = new Map<Id, PricebookEntry>([select Id, Product2.Product_Line__c from PricebookEntry where Id in :pbeIds]);

 for(OpportunityLineItem opli:Trigger.new){

          if(pbeMap.get(opli.PricebookEntryId).Product2.Product_Line__c == 'DIE'){
          system.debug('PricebookEntry.Product2.ProductLine!!!!!!!!!'+opli.PricebookEntry.Product2.Product_Line__c);

            opli.UnitPrice=0.01;
            system.debug('Unitprice%%%%%%%%%%%%'+opli.UnitPrice);

           opli.Newextension__c=opli.Quantity*0.01;
           system.debug('Newextension@@@@@@@@@@@'+opli.Newextension__c);


        }

        else {


            opli.UnitPrice=1;
            system.debug('Unitprice^^^^^^^^^^^^^^'+opli.UnitPrice);

            opli.Newextension__c=opli.Quantity*1;
            system.debug('Newextension$$$$$$$$$$$'+opli.Newextension__c);


      }

    }

}

Test class :

@istest
public class Populate_ExtensionTestclass{

static testmethod void  PopulateExtensiontest(){

Date closeDt = Date.Today();

 date myDate = date.today();

Account a2 = new Account(Name ='icrm testing acc');
insert a2;

opportunity oppr = new opportunity(Name='testing DIE 4/6/2015' ,  AccountId= a2.Id,StageName = 'Prospecting', 
                                   CloseDate = closeDt);
insert oppr;

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

Product2 pro2 = new Product2(Product_Line__c='DIE',Name='BXCD',Product_Code_Item_Number__c='BXCD24', isActive=true);
insert pro2;

PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                         isActive=true,UseStandardPrice = false);
 insert pbe2;

 OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=2, OpportunityId=oppr.Id,UnitPrice=0.01,PriceBookEntryId=pbe2.Id,
                                     Newextension__c=0.02,Bin_Item_Code__c='BXCD2424');
 insert OPplineitem2;
 }
 static testmethod void  PopulateExtensiontest1(){

Date closeDt = Date.Today();

 date myDate = date.today();

Account a2 = new Account(Name ='Non icrm testing acc');
insert a2;

opportunity oppr = new opportunity(Name='testing NONDIE 4/6/2015' ,  AccountId= a2.Id,StageName = 'Prospecting', 
                                   CloseDate = closeDt);
insert oppr;

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

Product2 pro2 = new Product2(Product_Line__c='NONDIE',Name='BXCDXXX',Product_Code_Item_Number__c='BXCD2424', isActive=true);
insert pro2;

PricebookEntry pbe2 =new PricebookEntry(unitprice=1,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                         isActive=true,UseStandardPrice = false);
 insert pbe2;

 OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=25, OpportunityId=oppr.Id,UnitPrice=1,PriceBookEntryId=pbe2.Id,
                                     Newextension__c=25);
 insert OPplineitem2;
 }
 }
3
  • can you share your Populate_Extension trigger code as well Apr 21, 2015 at 11:49
  • @ Vamsi Krishna:I have updates the trigger and test class.Any help very much appreciated.
    – nikkey
    Apr 21, 2015 at 12:16
  • If Chris' answer has solved your question, please make sure to mark his answer as accepted. It is a good practice to encourage users to answer, verify to visitors of your question that it's valid, and a kind way to tell the person who took the time to answer your question, "Thanks". Apr 21, 2015 at 22:46

1 Answer 1

22

When working with Opportunity Line Items in Test Classes, you need to first make sure you have a pricebook, then you need to create a product, then you need to create a pricebook entry and then you can create an Opportunity Line Item.

See the code snippets below on how to create a Opporunity Line Item in a test class.

// This is how we get the Standard PriceBook Id.  Prior to Summer '14, we needed
// to use SeeAllData=true, so this is a big improvement
Id pricebookId = Test.getStandardPricebookId();

//Create your product
Product2 prod = new Product2(
     Name = 'Product X',
     ProductCode = 'Pro-X',
     isActive = true
);
insert prod;

//Create your pricebook entry
PricebookEntry pbEntry = new PricebookEntry(
     Pricebook2Id = pricebookId,
     Product2Id = prod.Id,
     UnitPrice = 100.00,
     IsActive = true
);
insert pbEntry;

//create your opportunity line item.  This assumes you already have an opportunity created, called opp
OpportunityLineItem oli = new OpportunityLineItem(
     OpportunityId = opp.Id,
     Quantity = 5,
     PricebookEntryId = pbEntry.Id,
     TotalPrice = quantity * pbEntry.UnitPrice
);
insert oli;
5
  • :Thanks for your reply.So we need to add seealldata=true for the test classes ,while using Test.getStandardPricebookId.In my test classes RecordTypeid is also hardcoded so I need to replace them also.
    – nikkey
    Apr 21, 2015 at 12:19
  • 1
    @nikkey It's best practice to create test data in your test class, instead of setting seealldata to true. See salesforce.com/us/developer/docs/apexcode/Content/… What Chris is saying that you don't need to use seealldata anymore, because Test.getStandardPricebookId()
    – Folkert
    Apr 21, 2015 at 12:30
  • 1
    Exactly, thank you @Folkert. Now that Test.getStandardPricebookId() exists, we don't need to use SeeAllData=true, which is a great improvement. Apr 21, 2015 at 12:46
  • 1
    if using custom pricebooks, you'll need to insert a PBE for the standard pricebook first and then insert the PBE for the custom pricebook
    – cropredy
    Apr 21, 2015 at 17:21
  • @ crop1645:In my test classes RecordTypeid is also hardcoded so I need to replace with value or RecordTypename
    – nikkey
    Apr 21, 2015 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.