-
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 8 years, 2 months ago by
Vikas Kumar.
-
This discussion was modified 8 years, 2 months ago by
Forcetalks.
-
This discussion was modified 8 years, 2 months ago by
Forcetalks.
-
This discussion was modified 8 years, 2 months ago by
Log In to reply.
Popular Salesforce Blogs

Revolutionizing Patient Care with Salesforce Healthcare Cloud
Introduction Technology is the riveting force behind changing health care in the modern world today. Salesforce Healthcare Cloud stands at the forefront of this revolution…

Send Survey Email On Case Closure on Case without using Trigger and Apex Class
Send Survey Email on Case Closure We can create Survey Emails without coding and send them to any status of case closure. Use Flow Builder…

Co-Create Success With the Changing Salesforce Ecosystem
The Salesforce partner community is a powerful force. It is a place where Salesforce partners manage their business, learn best practices, get support on projects,…
Popular Salesforce Videos
Salesforce Customer 360 - It Brings Your Company & Customers Together
Q: With a dispersed workforce, how do you bring all departments together with a single shared view of your customer? A: With Salesforce Customer 360.…
Use Salesforce CMS to Manage and Publish Content for Experience Cloud Sites
Learn how Salesforce CMS helps you create, manage, and publish your content in Experience Cloud. With Salesforce CMS, you can also customize content for specific…