-
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, 1 month ago by
Vikas Kumar.
-
This discussion was modified 9 years, 1 month ago by
Forcetalks.
-
This discussion was modified 9 years, 1 month ago by
Forcetalks.
-
This discussion was modified 9 years, 1 month ago by
Log In to reply.
Popular Salesforce Blogs
Hellstar Builds a Community of Bold Dressers
Hellstar Shapes a Distinct Fashion Identity Explore the latest in bold streetwear and innovative fashion trends at officialhellstr.com, where style meets rebellion. Hellstar builds a…
Top 5 Best Salesforce Integration Practices in 2021
Adopting Salesforce integration is always beneficial for automating and scaling the businesses efficiently. The whole idea of integrating data might look easy, but there’s a catch. To…
Seamless Data Migration: Best Practices in Salesforce Implementation Services
Salesforce CRM is a robust tool with countless applications. Businesses invest in CRM implementation with the goal of managing complex business challenges while achieving no…
Popular Salesforce Videos
How to Write Your First Apex Program? | Salesforce Tutorial
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Lightning platform server in conjunction…
Salesforce Integration Crash Course | The Ultimate Guide to Salesforce Integrations
In these 100 minutes, you are going to learn almost all the integration concepts with the help of 2 projects. You will learn about API,…
What can Salesforce Service Cloud do for your Businesses
Salesforce Service Cloud allows you to Streamline Workflow, Automate Service Processes, and Surface Key Topics, Articles, and Experts to transform the agent experience. It simplifies…