You need to sign in to do that
Don't have an account?
forman.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!
I have done some changes in your code.Now its working fine
If this answer will helps out.Please mark it as best answer to help others.
Thanks :)
All Answers
I have done some changes in your code.Now its working fine
If this answer will helps out.Please mark it as best answer to help others.
Thanks :)
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.