Search code examples
triggerssalesforceapex

I am getting error variable Accnew and ACCold does not exist in apex trigger


  1. trigger PractiseM on Account (before update) {
  2. if (trigger.isBefore && trigger.isUpdate) {
  3.    for(Account accNew : trigger.new)
    
  4.        Account Accold = trigger.oldmap.get(Accnew.Id);
    
  5.    If (accNew.Name != AccOld.name) {
    
  6.        accNew.addError ('Please do not change the account name');
    
  7.    }
    
  8. } 9.}

Solution

  • You need curly braces. Your for loop in line 3 doesn't have braces so the scope is just line 4 and then you're out of the loop. So line 5 "thinks" you try to use undeclared variables.

    Fix the braces and - since your indentation was lying to you - consider using something like Prettier / eslint for code formatting checks?