+ Start a Discussion
HTANIRSHTANIRS 

Trigger on Quote after update to create Order

Hello Friends,

I have completed trigger on Quote (after Update). When Quote Status is updated as Approved. Order is created.
This is working fine. But when I update the quote any field Order is getting created again and again.

Below is my trigger. Kindly check and let me know what to change. 
trigger createOrderOnQuote on Quote (after update) {
        
    // Create Order once Quote is Approved.
    if(Trigger.isafter & Trigger.isupdate) {
        Set<Id> quoteIds = new Set<Id>();
        List<Order> orderList = new List<Order>();
        
        for(Quote q : Trigger.New) {
            quoteIds.add(q.Id);
            System.debug('--- Picking Quote Id ---' + quoteIds);
        }
        
        List<Quote> quoteList = [SELECT Id, Name, Status, OpportunityId, Email, AccountId, ContactId, BillingCountry, BillingPostalCode, BillingState, BillingCity, BillingStreet, 
                                        ShippingCountry, ShippingCity, ShippingPostalCode, ShippingState, ShippingStreet, BillingName, QuoteNumber, TotalPrice, ShippingName
                                   FROM Quote 
                                   WHERE Id =: quoteIds];
        
        // Create Order when Quote Approved.
        for(Quote q : quoteList){
            system.debug('--- Quote List ---' + quoteList);
            if(q.Status == 'Approved') {
                System.debug('--- Quote Status ---' + q.Status);
                
                Order o = new Order();
                o.EffectiveDate = system.today();
                o.Status = 'Draft';
                o.AccountId = q.AccountId;
                o.OpportunityId = q.OpportunityId;
                o.QuoteId = q.Id;
                o.Bill_To_Name__c = q.BillingName;
                o.BillingStreet = q.BillingStreet;
                o.BillingCountry = q.BillingCountry;
                o.BillingState = q.BillingState;
                o.BillingCity = q.BillingCity;
                o.BillingPostalCode = q.BillingPostalCode;
                o.Ship_To_Name__c = q.ShippingName;
                o.ShippingStreet = q.ShippingStreet;
                o.ShippingCountry = q.ShippingCountry;
                o.ShippingState = q.ShippingState;
                o.ShippingCity = q.ShippingCity;
                o.ShippingPostalCode = q.ShippingPostalCode;
                orderList.add(o);
            }
            System.debug('--- Order List Size ---' + orderList.size());
        }
            
        try {
            if(orderList.size() > 0) {    
                insert orderList;
            }
            System.debug('--- Inserted Order List: ---' + orderList.size()); 
        }
        catch(Exception e) {
            System.debug('The Following Exception has occurred: '+ e.getLinenumber() + ' : ' + e.getMessage());
        }
    }   
}
Thanks in Advance..
 
Best Answer chosen by HTANIRS
David Zhu 🔥David Zhu 🔥
You may need to change the loop to get the quote ids as below:
 
      for(Quote q : Trigger.New) {
          if (q.status == 'Approved' && trigger.oldMap.get(q.id).status != 'Approved')
          {
              quoteIds.add(q.id);
          }
          System.debug('--- Picking Quote Id ---' + quoteIds);
      }