0

My idea is to have identical code executing in all environments to solve the issue when refreshing or creating new sandboxes.

I want the code to determine where it is executing and based on the answer to execute accordingly. I can do this fairly easily with the code below.

I want to avoid executing the select statement each time this block runs as I think it will count toward my limits.

I want to store the answer to the select once (perhaps after login) and then make reference (global variable?) to it vs. having to query for it each time.

Can this be done?

List<organization> lstOrganization = [Select isSandbox from Organization];

try

{

    if (lstOrganization[0].isSandbox == False)
        system.debug('EXECUTE CODE PRODUCTION');
    else
        system.debug ('EXECUTE CODE SANDBOX');
}
catch (Exception e)
{

        system.debug('Generic Error: ' + e);
}
finally 

{

    system.debug (lstOrganization);
}

1 Answer 1

2

One simple thing you can do is limit this to one query per transaction by caching the result in a lazily loaded static variable:

public with sharing Environment {
    public static Boolean isSandbox {
        get {
            if (isSandbox == null) {
                isSandbox = [select isSandbox from Organization].isSandbox;
            }
            return isSandbox;
        }
        private set;
    }
}

so that the query is done the first time the value is referenced in a transaction but in subsequent references the cached value is used.

So to use it:

if (Environment.isSandbox) {
    ....
}

You must log in to answer this question.

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