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
acl5acl5 

How to set the "standard price" for a product2 object in Apex?

I am trying to unit test an Apex class that I've written but I am having a lot of trouble with the "standard price"

 

Below is the code and the error I am recieving: 

 

Pricebook2 standardPb = [SELECT Id, Name from Pricebook2 WHERE isStandard=true];

 PricebookEntry standardpbEntry = new PricebookEntry(unitPrice=0, useStandardPrice=true, Product2Id=testProduct.Id,Pricebook2Id=standardPb.Id );

 

 

 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: UnitPrice (unit price not equal to standard price): [UnitPrice]

 

I have no idea how the standardPrice got set, or where to find it so I can use the correct unit price.

 

Any help would be very much appreciated

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JohannesBorrmanJohannesBorrman
done. as i needed this for testclasses i set the UseStandardPrice=false

All Answers

JohannesBorrmanJohannesBorrman

Hi there!

 

 any progress here? just puzzled myself by this "problem"

JohannesBorrmanJohannesBorrman
done. as i needed this for testclasses i set the UseStandardPrice=false
This was selected as the best answer
PranavLAXPranavLAX

I am still having issues with this and still get that message:

 

Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Prospecting',CloseDate=Date.today());
insert opp1;

Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1');
insert pbk1;

Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1');
insert prd1;

PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=pbk1.id,UnitPrice=50,UseStandardPrice=false);
insert pbe1;

 

Can you please tell whats wrong with this code?