3

I'm a beginner and am trying to figure out what is wrong with my Code. I'm trying to create a basic Trigger that will update the Opportunity Description if the Opportunity Stage is marked a certain way. Right now this Trigger is giving me the error

"Error: Compile Error: Variable does not exist: Description at line 4 column 2"

I also think my opens and closes may be wrong. Any help is appreciated.

trigger updateFields on Opportunity (before insert,before update) {
  for(Opportunity obj : Trigger.new)
    if(opp.StageName=='Closed as Won'){
      (opp.description= 'Assessment Completed!!') ;
    }

    if(opp.StageName=='Closed as Lost'){
      opp.description= 'Opportunity Lost';
    }            
}
  • 2
    In addition to the answer below from fred - I would suggest that you add a pick list field and use that instead of using the description field. If you keep it the way you have it, any info entered buy the user into the description field will be lost. For things like this it is always best to use a dedicated field for the specific purpose rather than a generic field that could be used for a lot of things but now (since you did this) can only be used for this specific thing (which may not be known by all) – Eric Nov 17 '15 at 16:25
5

When you declared your for loop you called your Opportunity "obj", but then you call it "opp". Change it to this:

for(Opportunity opp : Trigger.new)

You also need braces around everything you do in your for loop. Like this:

trigger updateFields on Opportunity (before insert,before update) { 
    for(Opportunity opp : Trigger.new){ 
        if(opp.StageName=='Closed as Won'){ 
            (opp.description= 'Assessment Completed!!') ; 
        }
        if(opp.StageName=='Closed as Lost'){ 
            opp.description= 'Opportunity Lost'; 
        }
    }
}

Otherwise, it's all fine!

To make the code slightly more compact, you might consider using else if, which will skip second check when the StageName is "Closed as Won". It can't be "Closed as Lost" too! Also, you can remove some unnecessary brackets.

trigger updateFields on Opportunity (before insert,before update) { 
    for(Opportunity opp : Trigger.new){ 
        if(opp.StageName=='Closed as Won'){ 
            opp.description= 'Assessment Completed!!'; 
        } else if(opp.StageName=='Closed as Lost'){ 
            opp.description= 'Opportunity Lost'; 
        }
    }
}
  • Thank you so much, guys. I really appreciate the help and the recommendations!!! – A Perry Nov 17 '15 at 18:43
  • One more question on this one. I wrote the following Test. Is this well formed? – A Perry Nov 17 '15 at 19:28
  • @isTest public class SampleTest { public static testmethod void MyMethod(){ Test.startTest(); // TO DO: implement unit test Account account = new Account (Name = 'testa'); insert account; Opportunity[] opportunitys = new Opportunity[]{ new Opportunity (name = 'test1', account = account, stagename = 'Closed As Won', closedate = system.today(), new Opportunity (name = 'test2', account = account, stagename = 'Closed As Lost', closedate = system.today() }; update opportunitys; Test.stopTest(); } } – A Perry Nov 17 '15 at 19:30
  • @APerry Post a new question with the code, comments are just for improving existing answers. – IllusiveBrian Nov 17 '15 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.