• forman.hal
  • NEWBIE
  • 10 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
I'm trying to create a simple trigger on Opportunity that updates a custom field on all related OpportunityLineItems when the Opportunity stage is updated to Closed/Won.  I have this code, but I'm getting an error saying oli variable does not exist.
trigger onOpportunityWon on Opportunity (after update) {
Set<Id> relevantIds = new Set<Id>();
    // select relevant opportunities
    for(Opportunity o : trigger.new)
    {
        if(o.StageName == 'Closed Won' || trigger.oldMap.get(o.Id).StageName == 'Closed Won')
        {
            relevantIds.add(o.Id);
        }
    }

    // Line Items
    for(OpportunityLineItem oli : [SELECT ID, WonWhen__c, OpportunityId FROM OpportunityLineItem WHERE OpportunityId IN :relevantIds])
    {
        oli.WonWhen__c = system.today();
    }

if (oli.size()>0)
    update oli.values();
}
What am I doing wrong?  Thank you!
I'm trying to create a simple trigger on Opportunity that updates a custom field on all related OpportunityLineItems when the Opportunity stage is updated to Closed/Won.  I have this code, but I'm getting an error saying oli variable does not exist.
trigger onOpportunityWon on Opportunity (after update) {
Set<Id> relevantIds = new Set<Id>();
    // select relevant opportunities
    for(Opportunity o : trigger.new)
    {
        if(o.StageName == 'Closed Won' || trigger.oldMap.get(o.Id).StageName == 'Closed Won')
        {
            relevantIds.add(o.Id);
        }
    }

    // Line Items
    for(OpportunityLineItem oli : [SELECT ID, WonWhen__c, OpportunityId FROM OpportunityLineItem WHERE OpportunityId IN :relevantIds])
    {
        oli.WonWhen__c = system.today();
    }

if (oli.size()>0)
    update oli.values();
}
What am I doing wrong?  Thank you!