+ 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) {
                Order ord = new Order();
                ord.OpportunityId = opp.Id;
                ord.AccountId = opp.AccountId;
                ord.EffectiveDate = opp.CloseDate;
                ord.Status= 'Draft';
                ord.Pricebook2Id = opp.Pricebook2Id;
insert ordList;