-
Getting Error of FIELD_INTEGRITY_EXCEPTION while Executing Batch Class in salesforce
Hi All,
While Executing Batch Class i Am Getting the following Error
First error: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]
Batch Class
global class UpdateProductInOpportunity implements Database.Batchable<sobject> {
global List <Id> inputforid;
global String query;
global String oppproduct;public UpdateProductInOpportunity(List<Id> userInputId){
inputforid=userInputId;
oppproduct='productForTest';}
global Database.QueryLocator Start(Database.BatchableContext BC){query='Select id FROM Opportunity WHERE id=:inputforid';
System.debug('####check###');
System.debug(query);
return Database.getqueryLocator(query);}
global void execute(Database.BatchableContext BC,list<opportunity>scope){
System.debug('####check###');for(opportunity oppp:scope){
Product2 pr=new Product2();pr.Name=oppproduct;
pr.IsActive=true;
insert pr;
PriceBook2 customPriceBook=new PriceBook2();
customPriceBook.Name='customPriceBook';
customPriceBook.IsActive=True;
insert customPriceBook;
PriceBook2 customPriceBookRec=[select Id from PriceBook2 WHERE id=:customPriceBook.id];
PriceBook2 standardPriceBookRecId=[select Id from PriceBook2 WHERE isstandard=true];
PriceBookEntry standardPriceBookEntry = new PriceBookEntry();
standardPriceBookEntry.Product2Id=pr.Id;
standardPriceBookEntry.Pricebook2Id=standardPriceBookRecId.id;
standardPriceBookEntry.UnitPrice=2000;
standardPriceBookEntry.IsActive=true;
insert standardPriceBookEntry;PriceBookEntry customPriceBookEntry = new PriceBookEntry();
customPriceBookEntry.Product2Id=pr.Id;
customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
customPriceBookEntry.UnitPrice=5000;
customPriceBookEntry.IsActive=true;
insert customPriceBookEntry;
System.debug('####check###');
OpportunityLineItem oppLineItem = new OpportunityLineItem();
oppLineItem.OpportunityId = oppp.Id;
//oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
oppLineItem.PricebookEntryId = standardPriceBookEntry.Id;
oppLineItem.UnitPrice = 7000;
oppLineItem.Quantity = 5;
insert oppLineItem;}
}global void finish (Database.BatchableContext BC) {
System.debug('####check###');}
Thanks
}- This discussion was modified 7 years, 10 months ago by Vikas Kumar.
- This discussion was modified 7 years, 10 months ago by Forcetalks.
- This discussion was modified 7 years, 10 months ago by Forcetalks.
Log In to reply.
Popular Salesforce Blogs
Top 3 Techniques for Rolling Out Salesforce Lightning Experience
If you are a regular reader of Salesforce and its related products and services, you must already know by now what Salesforce Lightning is. As soon as…
Event In Austin That Will Help You To Get Leads: Texas Dreamin' USA Tour 2023
Texas Dreamin’ is an annual event that brings together Salesforce professionals, administrators, developers, and users worldwide to learn, network, and share best practices. This event…
Order of Execution in Salesforce - An Explanation
At the point when you spare a record with an addition, update, or upsert articulation, Salesforce plays out the accompanying occasions altogether. Before Salesforce executes…
Popular Salesforce Videos
TrailheaDX '18 Opening Keynote - Part 3: Connect Every Experience
Salesforce TrailheaDX ’18 Opening Keynote – Part 1: Create Connected Customer Experiences TrailheaDX ’18 Opening Keynote – Part 2: Salesforce Platform Salesforce TrailheaDX ’18 Opening…
Salesforce Data Archive along with Files
This video tries to explain the way true archive of records as well as the files in Salesforce. Archive files along with the DataArchiva, We…
SQL Row_Number - Salesforce Marketing Cloud Functions in 5 minutes
Cameron Robert shows how to use the Row Number (Over Partition By) SQL function in Salesforce Marketing Cloud. The SQL Row Number function can be…