function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Robert Davis 16Robert Davis 16 

Trigger on OpportunityLineItem to Update Field on Opportunity

I have gotten myself turned around on this trigger and can not figure out how to get it right. I have a custom field on the OpportunityLineItem / Opportunity Product that has the Length of Months that the product will be used for called Number_of_Months__c. I need to update the Opportunity in a field called Contract_Length_Months__c with the maximum date of all the products when a customer enters it.

I would appreciate any help in pointing me in the right direction:
 
trigger OpportunityLineItemTrigger on OpportunityLineItem ( after update) {
    List<Id> oppIds = new List<Id>();
    Decimal contractLengthMonths = 0;

    if(trigger.isAfter) {
        for (OpportunityLineItem oli3: trigger.new){
            oppids.add(oli3.opportunityId);
        }//END for (OpportunityLineItem oli3: trigger.new)
        List<OpportunityLineItem> allOLI = [SELECT id, Number_of_Months__c FROM OpportunityLineItem WHERE OpportunityId in: oppids];
        List<Opportunity> oppsToUpdate = [SELECT id, Contract_Length_Months__c FROM Opportunity WHERE id in: oppids];
        if(allOLI.size() > 0){
            for(OpportunityLineItem allOLI2: allOLI){
                if(allOLI2.Number_of_Months__c > contractLengthMonths){
                    contractLengthMonths = allOLI2.Number_of_Months__c;
                }//END if(allOLI2.Number_of_Months__c > contractLengthMonths)
            } //END for(OpportunityLineItem allOLI2: allOLI)
            for(Opportunity oppUpdate: oppsToUpdate){
                oppUpdate.Contract_Length_Months__c = contractLenghtMonths;
            }// END for(Opportunity oppUpdate: oppsToUpdate)
 
        }
    }//if(trigger.isAfter)
}//END OpportunityLineItem Class

At the line:
oppUpdate.Contract_Length_Months__c = contractLenghtMonths;

I get the following error from the Developer Console IDE : "Variable does not exist: contractLenghtMonths"

Thank you in advance for any help.

Robert
Best Answer chosen by Robert Davis 16
Kai Herng LauKai Herng Lau
Hi Robert,

The error message you are getting is because a typo problem

line 18: contractLenghtMonths
line 13, 14 & 15 is contractLengthMonths

the '...Length...' is cause the problem

All Answers

Kai Herng LauKai Herng Lau
Hi Robert,

The error message you are getting is because a typo problem

line 18: contractLenghtMonths
line 13, 14 & 15 is contractLengthMonths

the '...Length...' is cause the problem
This was selected as the best answer
Robert Davis 16Robert Davis 16
Kai,

Thank you, very much. Sometimes it takes a second set of eyes.

I really appreciate all your help.

Robert