0

Hi in my account every trigger throwing same error

Error: Compile Error: Variable does not exist: website at line 4 column 1

Example:

trigger websiteass on Account (before insert) {
    for(Account acc:trigger.new) {
        acc.website='smak.com';
    }
}
1
  • I think website field is a custom one, here you have called the lable name,please use API Name.
    – user44771
    Commented Jun 8, 2017 at 9:52

1 Answer 1

7

Looks like you have a class in your org with name as Account.

Error Cause: When you access an Account or any SObject; compiler looks for a class first whether it exists then goes to assume its an SObject.

Try deleting the class and try again!


Added:

Trying to add an example on how it works or atleast how I think:

  1. Works: Org has a Class with same name as any of Standard Object:

    • Class is not defined, hence compiler would assume its an SObject.

      Account objAccount = new Account();
      // following is legal
      objAccount.Name 
      // following is legal as SObject has all these properties
      objAccount.get('Name') 
      
  2. Doesn't Work: Org doesn't has a Class with same name as any of Standard Object:

    • Class is defined, hence compiler would assume you are trying to access a wrapper class.

      // Class with name as an Account
      public class Account {
      
          public class name;
      
          // constructor
          public Account() {
      
          }
          // methods
          public void add() { }
      }
      
      // example starts
      Account objAccount = new Account();
      // following is legal as there is a property defined in wrapper class
      objAccount.Name 
      // illeagal: no such method defined
      objAccount.get('Name') 
      

Workaround:

  • In worst/edge case scenario, if you still want to have a class with same name as any standard Object, like for instance while deserializing a JSON in Rest service; use wrappers as inner class as mentioned below:

    public class MainClass {
    
        // wrapper class
        public class Account {
    
        }
    }
    

Better alternative is to use Schema Prefix for accessing standard Object:

trigger websiteass on Account (before insert) {
    for(Schema.Account acc: trigger.new) {
        acc.website = 'smak.com';
    }
}
3
  • @khaleelmohammed, Don't forget to mark this as solved !!
    – HSdev
    Commented Jun 8, 2017 at 10:16
  • Hi @khaleelmohammed, I had already added the cause, highlighted it now. Let us know if you have any additional questions.
    – Raul
    Commented Jun 8, 2017 at 11:11
  • 1
    @khaleelmohammed If this answer addressed your problem, please consider accepting it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either.
    – Adrian Larson
    Commented Jun 8, 2017 at 14:03

You must log in to answer this question.

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