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
Shuhbam SinhaShuhbam Sinha 

Getting error while creating pricebook entry

Hello,

I am trying to create price book entry through apex but I am getting this error USER_DEBUG|[56]|DEBUG|  Before creating a custom price, create a standard price. 
for (Opportunity opp: [Select ID, Pricebook2ID FROM Opportunity WHERE ID = : oppId]){
        	priceBookId = String.isBlank(priceBookId) ? opp.Pricebook2ID : priceBookId;
   
        }
        if(lstPricebookEntry.size()== 0){
                PricebookEntry pbe = new PricebookEntry();
           		pbe.Pricebook2Id = priceBookId; 
			    pbe.Product2Id = productId; // getting id from aura.
            	pbe.UnitPrice = 10;
                pbe.UseStandardPrice=true;
                pbe.IsActive=true;  
				pbeList.add(pbe);
            
               Database.SaveResult[] srPBEList = Database.insert(pbeList, false);
Could anyone please help what I am missing here.
 
Best Answer chosen by Shuhbam Sinha
AnkaiahAnkaiah (Salesforce Developers) 
Hi Shubham,

before adding product to custom PriceBook it must have Standard Price. In other words it must be added to Standard PriceBook.

try with below code.
public class createOppLineItemclass {
    
    public static string createOppLineItem(String oppId, string productId){
    	List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        List<PricebookEntry> pbeList = new List<PricebookEntry>();
        String priceBookId;
    	List<PricebookEntry> lstPricebookEntry =  [Select Id,Product2ID FROM PricebookEntry WHERE Product2ID = :productId];
       	system.debug('productIdrwos '+lstPricebookEntry.size());
        for (Opportunity opp: [Select ID, Pricebook2ID FROM Opportunity WHERE ID = : oppId]){
        	priceBookId = String.isBlank(priceBookId) ? opp.Pricebook2ID : priceBookId;
   
        }
		
		Pricebook2 standardPriceBook = [SELECT Id FROM Pricebook2 WHERE isStandard = true LIMIT 1];
		
        if(lstPricebookEntry.size()== 0){
                system.debug('productIdrwos1 '+lstPricebookEntry.size());
                PricebookEntry pbe = new PricebookEntry();
           		pbe.Pricebook2Id = standardPriceBook.id;
			    pbe.Product2Id = productId;
            	pbe.UnitPrice = 10;
                pbe.UseStandardPrice=true;
                pbe.IsActive=true;  
				pbeList.add(pbe);
                PricebookEntry pbe1 = new PricebookEntry();
           		pbe1.Pricebook2Id = priceBookId;
			    pbe1.Product2Id = productId;
            	pbe1.UnitPrice = 10;
                pbe1.UseStandardPrice=true;
                pbe1.IsActive=true;  
				pbeList.add(pbe1); 
            
               Database.SaveResult[] srPBEList = Database.insert(pbeList, false);
		for(Database.SaveResult sr : srPBEList){
            if (sr.isSuccess()){
                OpportunityLineItem oli = new OpportunityLineItem();
                oli.OpportunityId = oppId;
                oli.PricebookEntryId = sr.getId();
                oli.Product2Id = productId;
                oliList.add(oli);
            }
                   if(!sr.isSuccess()){
                        String error = '';
                        for(Database.Error err : sr.getErrors()){error = error + err.getMessage();}
                    
             system.debug('erromsg '+ error);
            }
           
}
		Database.SaveResult[] srOLIList = Database.insert(oliList, false);
             
        
                return 'Success';
            }

    	}

}

If this helps, Please mark it as best answer.

Thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Shubham,

you need to assign standard price book to price book entry.

try with below.
for (Opportunity opp: [Select ID, Pricebook2ID FROM Opportunity WHERE ID = : oppId]){
        	priceBookId = String.isBlank(priceBookId) ? opp.Pricebook2ID : priceBookId;
   
        }
		
		// Get the Standard Price Book ID

Pricebook2 standardPriceBook = [
    SELECT Id
      FROM Pricebook2
     WHERE isStandard = true
     LIMIT 1
];
		
        if(lstPricebookEntry.size()== 0){
                PricebookEntry pbe = new PricebookEntry();
           		pbe.Pricebook2Id = standardPriceBook.id; 
			    pbe.Product2Id = productId; // getting id from aura.
            	pbe.UnitPrice = 10;
                pbe.UseStandardPrice=true;
                pbe.IsActive=true;  
				pbeList.add(pbe);
            
               Database.SaveResult[] srPBEList = Database.insert(pbeList, false);

Refer the below link.
https://salesforce.stackexchange.com/questions/232265/create-standard-price-book-programmatically

If this helps, Please mark it as best answer.

Thanks!!
Shuhbam SinhaShuhbam Sinha
Hello Ankaiah  ,
I tried that as well but my opportunity falls on the custom price book and if i used this standard price book then I am getting 
FIELD_INTEGRITY_EXCEPTION: unknown (pricebook entry is in a different pricebook than the one assigned to the opportunity . Do you have any alternative for this
AnkaiahAnkaiah (Salesforce Developers) 
Hi Shubham,

Can you share the full code?

Thanks!!
Shuhbam SinhaShuhbam Sinha
Sure 
 
public static string createOppLineItem(String oppId, string productId){
    	List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        List<PricebookEntry> pbeList = new List<PricebookEntry>();
        String priceBookId;
    	List<PricebookEntry> lstPricebookEntry =  [Select Id,Product2ID FROM PricebookEntry WHERE Product2ID = :productId];
       	system.debug('productIdrwos '+lstPricebookEntry.size());
        for (Opportunity opp: [Select ID, Pricebook2ID FROM Opportunity WHERE ID = : oppId]){
        	priceBookId = String.isBlank(priceBookId) ? opp.Pricebook2ID : priceBookId;
   
        }
        if(lstPricebookEntry.size()== 0){
                system.debug('productIdrwos1 '+lstPricebookEntry.size());
                PricebookEntry pbe = new PricebookEntry();
           		pbe.Pricebook2Id = priceBookId;
			    pbe.Product2Id = productId;
            	pbe.UnitPrice = 10;
                pbe.UseStandardPrice=true;
                pbe.IsActive=true;  
				pbeList.add(pbe);
            
               Database.SaveResult[] srPBEList = Database.insert(pbeList, false);
		for(Database.SaveResult sr : srPBEList){
            if (sr.isSuccess()){
                OpportunityLineItem oli = new OpportunityLineItem();
                oli.OpportunityId = oppId;
                oli.PricebookEntryId = sr.getId();
                oli.Product2Id = productId;
                oliList.add(oli);
            }
                   if(!sr.isSuccess()){
                        String error = '';
                        for(Database.Error err : sr.getErrors()){error = error + err.getMessage();}
                    
             system.debug('erromsg '+ error);
            }
           
}
		Database.SaveResult[] srOLIList = Database.insert(oliList, false);
             
        
                return 'Success';
            }

    	}


 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Shubham,

before adding product to custom PriceBook it must have Standard Price. In other words it must be added to Standard PriceBook.

try with below code.
public class createOppLineItemclass {
    
    public static string createOppLineItem(String oppId, string productId){
    	List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
        List<PricebookEntry> pbeList = new List<PricebookEntry>();
        String priceBookId;
    	List<PricebookEntry> lstPricebookEntry =  [Select Id,Product2ID FROM PricebookEntry WHERE Product2ID = :productId];
       	system.debug('productIdrwos '+lstPricebookEntry.size());
        for (Opportunity opp: [Select ID, Pricebook2ID FROM Opportunity WHERE ID = : oppId]){
        	priceBookId = String.isBlank(priceBookId) ? opp.Pricebook2ID : priceBookId;
   
        }
		
		Pricebook2 standardPriceBook = [SELECT Id FROM Pricebook2 WHERE isStandard = true LIMIT 1];
		
        if(lstPricebookEntry.size()== 0){
                system.debug('productIdrwos1 '+lstPricebookEntry.size());
                PricebookEntry pbe = new PricebookEntry();
           		pbe.Pricebook2Id = standardPriceBook.id;
			    pbe.Product2Id = productId;
            	pbe.UnitPrice = 10;
                pbe.UseStandardPrice=true;
                pbe.IsActive=true;  
				pbeList.add(pbe);
                PricebookEntry pbe1 = new PricebookEntry();
           		pbe1.Pricebook2Id = priceBookId;
			    pbe1.Product2Id = productId;
            	pbe1.UnitPrice = 10;
                pbe1.UseStandardPrice=true;
                pbe1.IsActive=true;  
				pbeList.add(pbe1); 
            
               Database.SaveResult[] srPBEList = Database.insert(pbeList, false);
		for(Database.SaveResult sr : srPBEList){
            if (sr.isSuccess()){
                OpportunityLineItem oli = new OpportunityLineItem();
                oli.OpportunityId = oppId;
                oli.PricebookEntryId = sr.getId();
                oli.Product2Id = productId;
                oliList.add(oli);
            }
                   if(!sr.isSuccess()){
                        String error = '';
                        for(Database.Error err : sr.getErrors()){error = error + err.getMessage();}
                    
             system.debug('erromsg '+ error);
            }
           
}
		Database.SaveResult[] srOLIList = Database.insert(oliList, false);
             
        
                return 'Success';
            }

    	}

}

If this helps, Please mark it as best answer.

Thanks!!
This was selected as the best answer
Shuhbam SinhaShuhbam Sinha
Just one correction when we are creating the standard PriceBook entry , we should be setting the value as 

pbe.UseStandardPrice=FALSE; not true