-
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 9 years, 9 months ago by
Vikas Kumar.
-
This discussion was modified 9 years, 9 months ago by
Forcetalks.
-
This discussion was modified 9 years, 9 months ago by
Forcetalks.
-
This discussion was modified 9 years, 9 months ago by
Log In to reply.
Popular Salesforce Blogs
AI, Sustainability, and Innovation: Highlights from Dreamforce 2023
Dreamforce 2023 Key Takeaways Dreamforce 2023 was the largest and most successful Dreamforce yet, with over 180,000 attendees from all over the world. The conference…
Assign Permission Set at the User Creation Made Simple: What You Need to Know | Salesforce Guide
Step 1: Create a Permission Set Step 2: Create a Record- Triggered Flow to Assign Permission Set at the time of User Creation Don't forget…
How Agentforce Sales Speeds Up the Lead-to-Cash Cycle with MuleSoft and Flow
Sales teams lose time at every handoff. A lead sits in one system. A quote lives in another. Approvals crawl through email. By the time…
Popular Salesforce Videos
Introduction to Salesforce CPQ | EP1
Session 1: Introduction to Salesforce CPQ: The video provides an introduction to the general quoting challenge many Customers face, and how Salesforce CPQ with its…
How to use Einstein Engagement Scoring in Salesforce Marketing Cloud
Cameron Robert shows how to gain actionable insights from the Einstein Engagement Scoring Dashboard in Salesforce Marketing Cloud, and talks through a few activity ideas…
Salesforce On-Demand Administrator
Apphienz's On-demand Administrator delivers much more than the company's expectations. It is hands-on, makes the work easy, efficient and productive along with being available to…