Advice on a trigger that wont work

My colleagues need to create a number of quotations on an opportunity, send them out and then when the customer comes back with their decided quote, the quote needs to be synced to the opportunity line items to make the opportunity value correct. I’ve created a process that allows a number of quotations to be created and sent out at once.

I’ve written a trigger to copy the opportunity line items from the opportunity into the Quote line items, including 3 custom fields I have created called Start_Date__C, Duration__c and End_Date__c.

That trigger works but now I need all the information from the chosen quotation to be taken from the Quote Line Items back into the Opportunity Line Items, including the custom fields as there is often a lot of changes to add. I’ve written a trigger which is below, but nothing is happening when I try to sync my chosen quote. Is there something I’m missing?

trigger QuotetoOppSync on OpportunityLineItem (after insert) {

    //Query opportunity line items
    List<OpportunityLineItem> olis = [SELECT Id, PricebookEntry.Product2Id, OpportunityId, 
        Start_date__c, Duration__c, End_date__c FROM OpportunityLineItem 
        WHERE Id IN: Trigger.new];
    //Get a list of all opp ids:
    List<id> oppids = new List<id>();
    for (opportunityLineItem oli:olis){
        oppIds.add(oli.OpportunityId);
    }

    //Query QuoteLineItems by Opportunity ID
    Map<Id, QuotelineItem> producttoQli = new Map<Id, QuoteLineItem>();
    List<QuoteLineItem> qlis = [SELECT Id, PricebookEntry.Product2Id, Start_date__c, Duration__c,
        End_date__c, Quote.OpportunityId FROM QuoteLineItem 
        WHERE Quote.OpportunityId IN :oppIds];

    if(qlis!=null){
        for(QuoteLineItem qli:qlis){
            productToQli.put(qli.PricebookEntry.Product2Id, qli);
        }
    }

    //Iterate across quote line items
    for (OpportunityLineItem oli : Olis){ 
        Quotelineitem qli=productToQli.get(oli.PricebookEntry.product2Id);
        if(qli != null){
            if(qli.Start_Date__c != null){ oli.Start_Date__c=qli.Start_Date__c;}
            If(qli.Duration__c != null) {oli.Duration__c=qli.Duration__c;}
            if(qli.End_Date__c != null){ oli.End_Date__c=qli.End_Date__c;}
        }
    }

    //Update
    update olis;
} 

Answer

Sycning Quotes and Opportunities is a pretty common requirement. There is a force.com labs package, Custom Quote Sync that will do what you want without writing a trigger.

The source code is public if you want to look at how they implemented it.

Attribution
Source : Link , Question Author : Emma Millar , Answer Author : Ralph Callaway

Leave a Comment