How to Insert Pricebook Entry in Test Class Salesforce?

As a Salesforce Developer sometime we need to develop the test class for a piece for Apex code which is using Pricebook and Pricebook Entry.

So, In this blog post, we will see how to insert the Custom pricebook in test class and also how to insert Pricebook Entry in test class.

Table of Contents

Introduction

As a Salesforce Developer sometime we need to develop the test class for a piece for Apex code which is using Pricebook and Pricebook Entry.

So, In this blog post, we will see how to insert the Custom pricebook in test class and also how to insert Pricebook Entry in test class.

Pricebook & Priecbook Entry

Before we discuss about use case let’s discuss what is pricebook and why it is important.

Pricebook is used to define the price of a product per unit. One Product can have multiple price depends upon country or region.

PriceBook Entry is a junction object between Product & Pricebook where we store the multiple price for a single product.

Use Case

As a developer either you or someone else from your team has developed an ApexTrigger or batch apex which is associating the Particular Product based to the Opportunity of a certain PriceBook.

Now you have been tasked to create the Test class for the same.

For this first you need to insert the Pricebook, Product and then Pricebook Entry for that product so that you can associate the Opportunity Line Item under the Opportunity.

Complete Code

				
					/* Insert Pricebook */
Pricebook2 customPricebook = new Pricebook2(
	Name = System.Label.PricebookName,
	IsActive = true
);
insert customPricebook;
/* Create Product */
Product2 pro = new Product2(Name = 'iPhone X', Supplier__c='Heitech', StockKeepingUnit='T1-000991');
Insert pro;
/* Create Standard Pricebook Entry*/
PricebookEntry pbe1 = new PricebookEntry(
	Pricebook2Id = Test.getStandardPricebookId(),
	Product2Id = pro.Id,
	UnitPrice = 1020,
	IsActive = true
);
Insert pbe1;
/* Create Custom Pricebook Entry*/
PricebookEntry pbe = new PricebookEntry(
	Pricebook2Id = customPricebook.Id,
	Product2Id = pro.Id,
	UnitPrice = 1020,
	IsActive = true
);
Insert pbe;
/* Create Opportunity */
Opportunity opp = new Opportunity(
	Name ='Hubspot Opportunity', StageName='Discovery', CloseDate=System.today().addDays(34),
	Created_In_Hubspot__c = true,
	Importsource__c = '8725760083',
	Pricebook2Id = customPricebook.Id
);
insert opp;
				
			

You can use the above code in your test class to cover the lines which is associating the Opportunity with Products.

Note: – You can also use the same code to associate Products with Order.

Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading