+ Start a Discussion
emillar_altiaemillar_altia 

Quote to Opp Sync - 1 error, can't figure out what it is

I've written a trigger but keep getting the same error, I'm unsure what the error is, when I run my test class the message reads: Variable does not exist: productIdToQli

My trigger is below and I've boldened the issue, does anyone know what Im doing wrong? 

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){
            productIdToQli.put(qli.PricebookEntry.Product2Id, qli);
        }
    }
//Iterate across quote line items
    for (OpportunityLineItem oli : Olis) {
       
        Quotelineitem qli=productIdToQli.get(oli.PricebookEntry.productId);
        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;
}
lakslaks
Hi,

The problem seems to be exactly what the error message says. You haven't created the map productIdToQli which you seem to be using later in the line that gives the error. Or maybe you used the wrong map name. I see that you have created a map called producttoQli which you haven't used.

Regards,
Lakshmi.