Thursday, May 31, 2018

Compile errors due to custom classes with the same names as standard objects

Symptom


Trigger or class won't compile with error "Variable does not exist: Name", where "Name" is a field on a standard object such as Account. 

e.g.

trigger myAccountTrigger on Account (before insert) {
    for(Account a:Trigger.New){
        system.debug(a.Name);
    }
}

Cause


There is a custom class with the same name as the standard object, the trigger is referring to this class and the specified attribute doesn't exist on the custom class.

e.g.

public class Account {
    //test class to object conflict
}



Resolution

The thing is, we don't want our code to refer to the class - we want it to refer to the standard object.

Obviously if we knew this problem was going to come up we would never have named the class "Account" in the first place. But after the fact it could be a lot of work to change the class name (and all references to it) to something like "AccountClass".

The easy way to achieve this is by fully qualifying the references to the standard object. This is done by prefixing the object name with "Schema." No need to change the class!

e.g.

trigger myAccountTrigger on Schema.Account (before insert) {
    for(Schema.Account a:Trigger.New){
        system.debug(a.Name);
    }
}

Monday, April 23, 2018

Notepad++ Search Results pane goes awol

Symptom

Search Results pane disappears and won't come back no matter how many times you press F7


Resolution

remove %appdata%\Notepad++ folder

Cause

Notepad++ had a spaz.

Reference


https://stackoverflow.com/questions/46632070/notepad-search-window-disappeared