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
forman.halforman.hal 

Simple trigger?

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!
Best Answer chosen by forman.hal
Sfdc CloudSfdc Cloud
Hi,
I have done some changes in your code.Now its working fine
trigger onOpportunityWon on Opportunity (after update) {
Set<Id> relevantIds = new Set<Id>();
list<OpportunityLineItem> oppline=new list<OpportunityLineItem>();

    // 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);
        }
    }

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

    
    update oppline;
}

If this answer will helps out.Please mark it as best answer to help others.
Thanks :)
 

All Answers

Sfdc CloudSfdc Cloud
Hi,
I have done some changes in your code.Now its working fine
trigger onOpportunityWon on Opportunity (after update) {
Set<Id> relevantIds = new Set<Id>();
list<OpportunityLineItem> oppline=new list<OpportunityLineItem>();

    // 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);
        }
    }

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

    
    update oppline;
}

If this answer will helps out.Please mark it as best answer to help others.
Thanks :)
 
This was selected as the best answer
Navee RahulNavee Rahul

what you have done is, you are adding the result set to  oli.WonWhen__c = system.today(); you need to add them to a list

thats not teh right way you need to add in a LIST and then update,so all corresponding and required values will be filled inside the LIST
BECAUSE YOU WERE UPDATING LIST,then obviously the value/recordset must be list.

 

for Opportunity list example 
list<OpportunityLineItem> oppline=new list<OpportunityLineItem>();

assign in loop
for( OpportunityLineItem oli : [SELECT ID, WonWhen__c, OpportunityId FROM OpportunityLineItemWHERE OpportunityId IN :relevantIds])
    {
        //Below for assignment
         oli.WonWhen__c = system.today();
         //Below add it to the list so your assigned value will be added to a list
         oppline.add(oli);

     }

 

Thanks
D Naveen Rahul.