+ Start a Discussion
Amol KandeAmol Kande 

how to create an order record for every opportunity inserted with initial status as 'Draft'.

AnudeepAnudeep (Salesforce Developers) 
Hi Amol, 

Here is a sample code
trigger OpportunityTriggerForOrder on Opportunity(after insert, after update) {

    Set<Id> oppIdSet = new Set<Id>();
    List<Order> ordList = new List<Order>();
    for(Opportunity opp: Trigger.new) {
               if(opp.AccountId != null) {
                oppIdSet.add(opp.Id);
                Order ord = new Order();
                ord.OpportunityId = opp.Id;
                ord.AccountId = opp.AccountId;
                ord.EffectiveDate = opp.CloseDate;
                ord.Status= 'Draft';
                ord.Pricebook2Id = opp.Pricebook2Id;
                ordList.add(ord);
            }
        }
insert ordList;

}