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

Look ups and Master Detail
We were done with just the two fields Auto Number and Formula Field, In this section we will move on and consider the other fields.…

Top Salesforce Trends 2023 – A Guide of Growth for Businesses
As one of the best CRM solutions, Salesforce allows companies to create customer databases. It also streamlines operations. While many companies have benefitted from Salesforce consulting…

Salesforce Lightning vs. Salesforce Classic: Which One You Should Choose
What is Salesforce Lightning? Salesforce Lightning, first announced at the Dreamforce conference in 2015, represents the company’s vision for the CRM platform moving forward. The…
Popular Salesforce Videos
How can Salesforce Field Service Help in Providing a Better Customer Experience?
Salesforce Field Service Management automates and optimizes the work of field service workers (agents, dispatchers, mobile technicians). It is part of the Service Cloud and…
Transform the Consumer Experience & Take the Trail to Learn More | Salesforce
Watch this video to learn how uniting Commerce, Marketing and Service teams in Salesforce can help you deliver more connected consumer experiences. Then take the…
How to Get Your Data In and Get Insights Out | Salesforce Essentials
Organize all your data with Salesforce Essentials. With a quick and easy import, you’ll be able to manage your business and watch it grow. Essentials…