You need to sign in to do that
Don't have an account?

Too many DML Statements
Hi Guys ! i am getting too many dml statements error by using this code i know the problem that is i am using a dml statement inside a loop but i dnt know how to do it out side loop because i need the product id for pricebook entry is there a way to do it
for (SalesLine__c SL : SlList){
if(SL.Product__c == null && SL.Unit_Price__c != null){
Product2 Prod = new Product2( Name = SL.Description__c,
ProductCode = SL.No__c + ForName.get(Sl.Sales_Order__c),
IsActive= True,
Product_Type__c = 'Charge(item)');
Insert Prod;
system.debug('Updating' + SL);
PricebookEntry pbe = new PricebookEntry(
Product2Id = Prod.Id,
UnitPrice = SL.Unit_Price__c,
Pricebook2Id = pb.Id,
IsActive = TRUE);
LinesToUpdate.add(new SalesLine__c(Id=SL.Id,Product__c = prod.Id,Sales_Order__c = SL.Sales_Order__c));
// ProductsToAdd.add(Prod);
PbeToAdd.add(pbe);
}
}
//Insert ProductsToAdd;
Insert PbeToAdd;
Update LinesToUpdate;
for (SalesLine__c SL : SlList){
if(SL.Product__c == null && SL.Unit_Price__c != null){
Product2 Prod = new Product2( Name = SL.Description__c,
ProductCode = SL.No__c + ForName.get(Sl.Sales_Order__c),
IsActive= True,
Product_Type__c = 'Charge(item)');
Insert Prod;
system.debug('Updating' + SL);
PricebookEntry pbe = new PricebookEntry(
Product2Id = Prod.Id,
UnitPrice = SL.Unit_Price__c,
Pricebook2Id = pb.Id,
IsActive = TRUE);
LinesToUpdate.add(new SalesLine__c(Id=SL.Id,Product__c = prod.Id,Sales_Order__c = SL.Sales_Order__c));
// ProductsToAdd.add(Prod);
PbeToAdd.add(pbe);
}
}
//Insert ProductsToAdd;
Insert PbeToAdd;
Update LinesToUpdate;