-
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, 11 months ago by Vikas Kumar.
- This discussion was modified 7 years, 11 months ago by Forcetalks.
- This discussion was modified 7 years, 11 months ago by Forcetalks.
Log In to reply.
Popular Salesforce Blogs
Salesforce lightning:treegrid - The Developer Guide
A tree grid displays structured data in a table with expandable rows. lightning:treeGrid component displays hierarchical data in a table. Suppose, you have to show…
What Are The Benefits of A Salesforce Consulting Partner
Salesforce Consulting services help the organizations to fulfill their business needs by assisting them in projects like Salesforce implementation, data integration, customization, AppExchange, and many more.…
3 Ways Salesforce is Driving the EdTech Industry in 2023
Salesforce is a cloud-based customer relationship management (CRM) platform that is driving several technology trends in education. The company’s products are used by millions of…
Popular Salesforce Videos
Salesforce Spring ‘21 Release Notes Highlights & Summer ‘21 Preparation
Spring ‘21 Release Notes Highlights & Summer ‘21 Preparation In this session we discussed 00:00 Bella Vista, Western Sydney Developer Group Overview 03:30 Andrew Whitten…
Introduction to Salesforce Flow Builder
Watch this video to learn about Salesforce Flow Builder. 0:00 Introduction 2:50 Introduction to Salesforce Flows 4:40 Types of flows, Screen, Auto-Launched 7:10 Components related…
Similarity and Difference Between SFRA and PWA | Which One to Choose? | Salesforce
Choosing SFCC as the platform necessitates a major decision: on what architecture will your channels be based? When learning about Salesforce B2C Commerce Cloud, you'll…