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

Opportunity & Opportunity Line Item to Service Contract - Best Practice
Hi there,
I have a requirement to create a service Contract when Opportunity is Cclosed won and copy all Opp line items to contract Line Items.
Here is the sample Code , there are two DMLs in the loop , one for ServiceContract and other for contractlineitem(that need service contract id).
I have a workaround to avoid dml in loop is that insert service contract on Opportunity trigger , set some custom field that hold opportunity id on service contract.
And on ServiceContract add another trigger that will query opportunity line item relevant to opportunity (using custom field on servicecontract object that hold opportunity id) and insert them as contract line item.
I want to know if there is any other way of avoiding DMLs in loop as this could cause DML limit.
I have a requirement to create a service Contract when Opportunity is Cclosed won and copy all Opp line items to contract Line Items.
Here is the sample Code , there are two DMLs in the loop , one for ServiceContract and other for contractlineitem(that need service contract id).
Map<Id,List<OpportunityLineItem>> OppLineItems = new Map<Id,List<OpportunityLineItem>>(); for(OpportunityLineItem ol : [Select opportunityId,UnitPrice, Quantity , Description From OpportunityLineItem where opportunityId IN: Trigger.new]) { List<OpportunityLineItem> tempList = new List<OpportunityLineItem>(); if(OppLineItems.containskey(ol.opportunityId)) { templist = OppLineItems.get(ol.OpportunityId) ; templist.add(ol); OppLineItems.put(ol.OpportunityId,tempList); } else { templist.add(ol); OppLineItems.put(ol.OpportunityId,tempList); } } //Create Service Contract and Contract Line Item for(Opportunity o : Trigger.New) { if(o.IsClosed && o.Iswon) { ServiceContract sc = new ServiceContract(); sc.AccountId = o.AccountId; sc.Name = o.Name; sc.ApprovalStatus = 'Draft'; sc.StartDate = System.today(); insert sc; List<ContractLineItem> ContractLineInsert = new List<ContractLineItem>(); for(OpportunityLineItem ol : OppLineItems.get(o.Id) { ContractLineItem cl = new ContractLineItem(); cl.ServiceContractId = sc.Id; // some other field mapping from opp line item ContractLineInsert.add(cl); } //insert line item insert ContractLineInsert; } }
I have a workaround to avoid dml in loop is that insert service contract on Opportunity trigger , set some custom field that hold opportunity id on service contract.
And on ServiceContract add another trigger that will query opportunity line item relevant to opportunity (using custom field on servicecontract object that hold opportunity id) and insert them as contract line item.
I want to know if there is any other way of avoiding DMLs in loop as this could cause DML limit.
Below code is customize and follow best practise
All Answers
Below code is customize and follow best practise