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
megha patil 16megha patil 16 

standard Price Book

I want print Standard Price book if their is else print priceBook in Apex
what I do?
Thanks in Advanced
VinayVinay (Salesforce Developers) 
In Apex, you can create a Price Book (Pricebook2) and a Product (Product2) and relate the two records with a Price Book Entry (PricebookEntry) using the following method:

// Create a Pricebook

Pricebook2 priceBook = new Pricebook2(
    Name = 'Example Price Book',
    Description = 'This is the Price Book description.',
    IsActive = true
);

insert priceBook;

// Create a Product

Product2 product = new Product2(
    Name = 'Example Product',
    Description = 'This is the Product description.',
    ProductCode = 'EX1234',
    StockKeepingUnit = 'EX5678',
    Family = 'Example Product Family',
    QuantityUnitOfMeasure = 'inches',
    DisplayUrl = 'https://www.example.com/',
    ExternalId = 'ID #1234',
    ExternalDataSourceId = '0XCXXXXXXXXXXXXXXX',
    IsActive = true
);

insert product;

// Get the Standard Price Book ID

Pricebook2 standardPriceBook = [
    SELECT Id
      FROM Pricebook2
     WHERE isStandard = true
     LIMIT 1
];

// Insert the Product in the Standard Price Book (if necessary)

PricebookEntry standardPriceBookEntry = new PricebookEntry(
    Pricebook2Id = standardPriceBook.Id,
    Product2Id = product.Id,
    UnitPrice = 100.00,
    UseStandardPrice = false,
    IsActive = true
);

insert standardPriceBookEntry;

// Insert the Product in the New Price Book

PricebookEntry priceBookEntry = new PricebookEntry(
    Pricebook2Id = priceBook.Id,
    Product2Id = product.Id,
    UnitPrice = 100.00,
    UseStandardPrice = false,
    IsActive = true
);

insert priceBookEntry;

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks,
Vinay Kumar
sachinarorasfsachinarorasf
Hi Megha,

I have gone through your problem.

public class CreateNewPriceBook {
public static void addProductsInPriceBook() {
try{
Pricebook2 priceBookObject = new Pricebook2();
priceBookObject.Name = 'Algo pricebook';
priceBookObject.IsActive = true;
Insert priceBookObject;
System.debug(priceBookObject);

Pricebook2 stdPricebook = [SELECT Id, Name, IsActive FROM PriceBook2 WHERE IsStandard=True LIMIT 1];

List<Product2> productList = new List<Product2>();
for(Integer i=1 ; i<=10 ;i++){
Product2 productObject = new Product2();
productObject.Name = 'Product ' + i;
productObject.IsActive = true ;
productList.add(productObject);
}
if(productList.size()>0){
Insert productList;
System.debug(productList);
}

if(productList.size()>0){

List<Pricebookentry> priceBookentryList = new List<Pricebookentry>();
for(Product2 productObject2 : productList){
Pricebookentry priceBookEntryObject = new Pricebookentry();
if(stdPricebook.Id != Null && productObject2.Id != Null && priceBookObject.Id !=Null)
{
priceBookEntryObject.Pricebook2Id = stdPricebook.Id;
priceBookEntryObject.Product2Id = productObject2.Id;
priceBookEntryObject.UnitPrice = 100;
priceBookentryList.add(priceBookEntryObject);

Pricebookentry priceBookEntryObject2 = new Pricebookentry();
priceBookEntryObject2.Pricebook2Id = priceBookObject.Id;
priceBookEntryObject2.Product2Id = productObject2.Id;
priceBookEntryObject2.UnitPrice = 200;
priceBookentryList.add(priceBookEntryObject2);
}
}
if(priceBookentryList.size()>0){
Insert priceBookentryList;
for(PricebookEntry Obj : priceBookentryList)
System.debug(Obj);
}
}
}catch(Exception e){
System.debug('Exception ::' + e.getMessage() + '--Line Number ::' + e.getLineNumber());
}

}
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora