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
AssafAssaf 

Pricebook and PricebookEntry in TestMethod

Hi all,

I wrote a simple test class and I get an Error saying : 'PriceBookEntry is in a different PriceBook than the one assigned to the Opportunity'. (the Error is in the line saying  OpportunityLineItem OppLI_Rec = new Opportunity(...);
                                                                       insert OppLI_Rec;

Code:
public class Test_updateQuarterToOppProd {

static testMethod void UpdateQuarter(){

//insert a new product
Product2 p = new product2(name='x');
insert p;

Product2 p2 = new product2(name='y');
insert p2;

//define the standart price for the product
Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
insert new PricebookEntry(pricebook2id = stdPb.id, product2id = p.id,unitprice=1.0, isActive=true);

Pricebook2 pb = new pricebook2(name='test');
insert pb;

PricebookEntry pbe = new PricebookEntry(pricebook2id=pb.id, product2id=p.id,unitprice=1.0, isActive=true);
insert pbe;


Account AcntRec = new Account(name = 'Acc', Type = 'Customer', Industry = 'Automotive');
insert AcntRec;

Opportunity OppRecA = new Opportunity(AccountId = AcntRec.Id, name='Opp', stageName='Open', closeDate=Date.newInstance(2008,10,10), revenue_recognition__c = 'Q1');
insert OppRecA;

OpportunityLineItem OppLI_Rec = new OpportunityLineItem(OpportunityId = OppRecA.Id, pricebookentryid=pbe.id, UnitPrice = 100, Quantity = 3);
insert OppLI_Rec;


OppRecA.revenue_recognition__c = 'Q2';
update OppRecA;

system.assertEquals('Q2',[select Quarter__c from OpportunityLineItem where Id = :OppLI_Rec.Id].Quarter__c);
}

}

 I can't find the problem in here.

I appreciate your help.

Thanks, Assaf



TehNrdTehNrd
When you insert the Opportunity you need to set the pricebookId field.

Code:
Opportunity OppRecA = new Opportunity(PricebookID = pb.id, AccountId = AcntRec.Id, name='Opp', stageName='Open', closeDate=Date.newInstance(2008,10,10), revenue_recognition__c = 'Q1');
insert OppRecA;

 

AssafAssaf
Thanks, I probably omitted it while testing and testing...

now I get an Error saying: 'System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateOpportunityWhenOplinePercentageChange: execution of AfterUpdate'

on the following line: 'insert oli';
I tried to retrieve all id necessary for the creation of an opportunityLineItem and still gets the same Error.

any ideas?

Code:
public class Test_updateQuarterToOppProd {

static testMethod void UpdateQuarter(){

Product2 p = new product2(name='unittest');
insert p;

Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
insert new PricebookEntry(pricebook2id = stdPb.id, product2id = p.id,unitprice=1.0, isActive=true);

// Next, it creates a new pricebook with an entry for this product

Pricebook2 pb = new pricebook2(name='unittest');
insert pb;

PricebookEntry pbe = new PricebookEntry(pricebook2id=pb.id, product2id=p.id,unitprice=2.0, isActive=true);
insert pbe;

Account AcntRec = new Account(name = 'Acc', Type = 'Customer', Industry = 'Automotive');
insert AcntRec;

// Next, it creates a new opportunity

Opportunity o = new Opportunity (pricebook2id = pb.id, Name = 'OwnerRoleTest #1',AccountId = AcntRec.Id, Stagename='Open', Closedate=Date.newInstance(2010,02,02));

insert o;

//retrieving the objects I need for creating the OLI.
Opportunity op = [select id from Opportunity where Name like 'OwnerRoleTest #1'];
Pricebook2 pb1 = [select id from Pricebook2 where Name like 'unittest'];
PricebookEntry pbe1 = [select id from PricebookEntry where pricebook2id=:pb1.id];


// Next, it insert a new 2 opportunity product

OpportunityLineItem[] Oli=new OpportunityLineItem[0];

oli.add(new OpportunityLineItem(Opportunityid=op.id,pricebookentryid=pbe1.id,Quantity=1 ,UnitPrice=1000));

insert oli;//HERE I GET THE ERROR SAYING: "CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY"
}
}

any help will be appreciated.

thanks.
 
 




Message Edited by Assaf on 09-29-2008 12:45 AM
AssafAssaf
in one of the trigger the opportunityLineItem tried to access a null field and this is, somehow, ended up with this error.
fixed it.

cloudjedi1.3940876426809475E12cloudjedi1.3940876426809475E12
I found a work around in this thread: How do I avoid STANDARD_PRICE_NOT_DEFINED when unit-testing an OpportunityLineItem in Apex v24.0 (http://stackoverflow.com/questions/9164986/how-do-i-avoid-standard-price-not-defined-when-unit-testing-an-opportunitylineit)