0

Does anyone have an idea why my trigger doesn't want to save the o.Closedate, only the stagename works fine. but the error I get on closedate is

"variable doesn't exists"

...thanks in advance.

trigger Opportunity_Before_Update on Opportunity (before insert) {

    for(Opportunity o: trigger.new)
        o.Stagename = 'New';
        o.CloseDate = Date.today()+90;
}
2
  • Hi Prady, although it must be an easy one for you, but it helped me out perfectly :-) thanks!
    – Michiel
    Mar 9, 2016 at 10:41
  • Glad i could be of help. Believe me we all have gone through this :)
    – Prady
    Mar 10, 2016 at 5:02

1 Answer 1

3

If you do not use {} after your for loop, then only the first line after the for loop is within the loop, and the rest of the statements are considered outside of the loop. You need to have the for loop code block within {}so it can find a defined reference to o.

trigger Opportunity_Before_Update on Opportunity (before insert) {
    for(Opportunity o: trigger.new)
    {
        o.Stagename = 'New';
        o.CloseDate = Date.today()+90;
    }
}
3
  • 1
    Prady -- SFSE etiquette is to not solicit for 'mark as answered' (unlike some other forum I can think of).
    – cropredy
    Mar 9, 2016 at 2:06
  • @crop1645 Thanks for letting me know about it. Do you have link on the SFSE etiquette? i want to make sure i comply with other etiquite's as well :). I remember we were trying get people to accept answers during the initial stages of SFSE. Maybe this is a question for meta
    – Prady
    Mar 9, 2016 at 6:00
  • Prady - see meta.salesforce.stackexchange.com/questions/794/… - LaceySnr has a particularly good answer -- you can prompt the OP to mark as solved if the OP acknowledges in the comment thread that your answer 'worked'
    – cropredy
    Mar 9, 2016 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .