You need to sign in to do that
Don't have an account?
Hi I need a batch class to insert 10 Opportunity line items on each closed Won Opportunity exists in your org.
global class BatchOnContact implements Database.Batchable<sObject>, Database.Stateful{
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator('select Id, name, StageName from opportunity where StageName=\'Closed Won\'');
}
global void execute(Database.BatchableContext bc, List<opportunity> Opplist){
List<OpportunityLineItem> OList = new List<OpportunityLineItem>();
List<Product2> prolist= [SELECT Id from Product2 Limit 10];
For(opportunity opp: Opplist){
for(Integer i=0; i<10; i++){
OpportunityLineItem op= new OpportunityLineItem(opportunityId=opp.id, Product2Id=prolist.get(i).id,Quantity =1, TotalPrice=1000);
OList.add(op);
} opp.Pricebook2Id='0066F00000yUeDpQAK';
}update opplist;
insert OList;
}
global void finish(Database.BatchableContext bc){ }
}
global Database.QueryLocator start(Database.BatchableContext bc){
return Database.getQueryLocator('select Id, name, StageName from opportunity where StageName=\'Closed Won\'');
}
global void execute(Database.BatchableContext bc, List<opportunity> Opplist){
List<OpportunityLineItem> OList = new List<OpportunityLineItem>();
List<Product2> prolist= [SELECT Id from Product2 Limit 10];
For(opportunity opp: Opplist){
for(Integer i=0; i<10; i++){
OpportunityLineItem op= new OpportunityLineItem(opportunityId=opp.id, Product2Id=prolist.get(i).id,Quantity =1, TotalPrice=1000);
OList.add(op);
} opp.Pricebook2Id='0066F00000yUeDpQAK';
}update opplist;
insert OList;
}
global void finish(Database.BatchableContext bc){ }
}
Try Below i've Changed Code Please Mark It As Best Asnwer If It Helps
Thank You!
All Answers
Try Below Code Please Mark It As Best Asnwer If It Helps
Thank You!
Try Below i've Changed Code Please Mark It As Best Asnwer If It Helps
Thank You!
Hii Harsh

Working Fine In My Org Check If There's Any Process
The error is stating: you are adding the products under some opportunity as lineitem where opportunity is already linked with different Pricebook and the Product you are adding under that opportunity having different Pricebook Id that is standard pricebook.
So i will suggest you keep atleast 10 products under all pricebooks then execute the batch... Otherwise you have to write big logic inside your apex... Better keep it simple add dummy products in your org itself and add those products under pricebooks whicj having 10 lesser entry count.
Once your data work done you can execute the below batch:
Thanks,
Maharajan.C