How to Insert Pricebook Entry in Test Class Salesforce?

on

|

views

and

comments

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 Singhhttps://www.pantherschools.com/
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
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential