Salesforce Developer Tips & Tricks

Salesforce is full of quirks. Avoid these common pitfalls.

Custom Settings are Not Accessible from Test Classes

You can use @IsTest(SeeAllData=True). However, this is not a best practice since all data will be visible and it will be difficult to identify what your automation has created or changed.

Instead, you need to create a test instance of the Custom Setting and populate all relevant fields.

Wrapper Classes are Your Best Friend

If you have custom data in your process, the best way to store that data is by using what’s called a “Wrapper Class”

SettingsWrapper.cls
public class SettingsWrapper {
    public Integer numberOfRecordsToGenerate { get; set; }
    public String firstDate { get; set; }
    public Boolean customHandling { get; set; }
    
    public SetupPaymentWrapper() {
        // set defaults
        this.numberOfPayments = 1;
        this.firstPaymentDate = System.today();
        this.customHandling = false;
    }
}

I even use Wrappers in my test classes so that I can generate / store / manipulate all required test data to cover any conditions throughout my test methods.