2

So in visualforce I can access all kinds of organization variables like:

'{!$Organization.Name}'
'{!$Organization.City}'
'{!$Organization.Country}'
'{!$Organization.Id}'

etc...

Now what I need is to determine if a user is in a sandbox. Organization object has the very legitimate field 'IsSandbox'. But doing '{!$Organization.IsSandbox}' results in a push error saying IsSandbox field does not exist. I have API version of 45 on my visualforce component that is trying to do this.

Did they really not allow access to IsSandbox from visualforce? Is there any other way to do this? I don't want to create a controller JUST to get the IsSandbox flag from a query...

1
  • The docs specify that it has to be queried. I can't fathom why it's not available as a regular old global variable, so we can use it in formula fields, flow, etc. Also.
    – gorav
    Commented Jul 19, 2019 at 23:46

2 Answers 2

2

From the documentation of $Organization it's quite clear that possible values are

{!$Organization.Id}
{!$Organization.Name}
{!$Organization.Division}
{!$Organization.Street}
{!$Organization.City}
{!$Organization.State}
{!$Organization.PostalCode}
{!$Organization.Country}
{!$Organization.Fax}
{!$Organization.Phone}
{!$Organization.GoogleAppsDomain}
{!$Organization.UiSkin}

It does not contain IsSandbox so you have to look for alternative.

Based on my Limited knowledge of VF I can say we can use AJAX Toolkit in VF.

AJAX Toolkit allows you to query any record and use JS to alter HTML and post your content.

Your code will be like:

<apex:page >
    <script type="text/javascript">
    var __sfdcSessionId = '{!GETSESSIONID()}';
    </script>
    <script src="../../soap/ajax/46.0/connection.js"
          type="text/javascript"></script>
    <script type="text/javascript">     window.onload = setupPage;
    function setupPage() {
      //function contains all code to execute after page is rendered

      var state = { //state that you need when the callback is called
          output : document.getElementById("output"),
          startTime : new Date().getTime()};

      var callback = {
          //call layoutResult if the request is successful
          onSuccess: layoutResults,

          //call queryFailed if the api request fails
          onFailure: queryFailed,
          source: state};

      sforce.connection.query(
          "Select Id,isSandbox FROM Organization",
           callback);
  }

  function queryFailed(error, source) {
    source.output.innerHTML = "An error has occurred: " + error;
  }

  /**
  * This method will be called when the toolkit receives a successful
  * response from the server.
  * @queryResult - result that server returned
  * @source - state passed into the query method call.
  */
  function layoutResults(queryResult, source) {
    if (queryResult.size > 0) {
      var output = "";

      //get the records array
      var records = queryResult.getArray('records');
       console.log(JSON.stringify(records));
      output = 'Is Sandbox '+ records[0].IsSandbox;
    source.output.innerHTML = output;
    }
  }
  </script>

    <div id="output"> </div>

</apex:page>

Src: https://developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/sforce_api_ajax_vf_sample.htm

1
  • Similar to what I would have wrote, but using apex:remoteObjects instead.
    – sfdcfox
    Commented Jul 19, 2019 at 16:15
0

Please Upvote this Idea $Organization.IsSandbox global variable should be accessible declaratively and see some workarounds in the comments (not sure about Visualforce though)

1
  • Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – identigral
    Commented Aug 9, 2023 at 15:48

You must log in to answer this question.

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