-
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, 11 months ago by
Vikas Kumar.
-
This discussion was modified 8 years, 11 months ago by
Forcetalks.
-
This discussion was modified 8 years, 11 months ago by
Forcetalks.
-
This discussion was modified 8 years, 11 months ago by
Log In to reply.
Popular Salesforce Blogs
Reasons Why Commercial Banking Should Consider Moving to Salesforce Financial Services Cloud
The banking industry has completely transformed and now what matters the most is how a bank is able to deliver personal experiences to its customers…
Your Salesforce Implementation Guide
Early-stage startups, mid-sized companies, large enterprises — no matter your size, you can only ignore the importance of implementing a Salesforce solution for so long.…
Salesforce Community Cloud and Its Importance for Your Business
One of the most widely used CRM programs worldwide is Salesforce. Organizations can cater to different aspects of their operations with the help of several…
Popular Salesforce Videos
Modern App Development On Salesforce - Apex Testing
In this series, you will explore Modern App Development on the Salesforce platform, including Heroku. The live hands-on sessions will take you through the journey…
Summer Internship | Career with Salesforce
This Summer, dive into the ocean of Salesforce with S2 Labs Apply for the much-awaited S2 Labs Summer Internship Program and start accelerating your career…
Algoworks attended Dreamfest 2019!
Algoworks attended Dreamfest 2019 and we had the most enjoyable time ever! Fleetwood Mac played at the venue and the crowd went crazy, energy was…