0

I know there's ways in apex to detect if in sandbox or prod. What i'm looking to do is in my Aura component, show a link when i'm in a sandbox and show a different link when i'm in prod (not a sandbox) What is the best route?

Here are snippets of my code

Aura component/Controller.js

({
    handleClick : function(component, event, helper) {
        var caseId = component.get("v.recordId");
        var action = component.get("c.sendOptyData");
        action.setParams({ OptyName : OptyName });
        action.setCallback(this, function(response) { 
              var state = response.getState();
           if (state === "SUCCESS") {
               var returnValue =response.getReturnValue();               
              //open new browser tab with URL below
               window.open('https://yahoo.com');
             }else if (state === "ERROR") {
                helper.errorToast(response, component);
            }
        });
        $A.enqueueAction(action);
}
})

I'm aware of code in apex like this:

 public static Boolean isSandbox
    {
        get
        {
            return [select IsSandbox from Organization limit 1].IsSandbox;
        }
    }

What is the best route to have this translate over to the Aura component when showing different URL's? thanks!

2 Answers 2

0

We need to have a server call to identify the org is sandbox or not. You can use the same query which you mentioned in question i.e. [select IsSandbox from Organization limit 1].IsSandbox;

Based on value returned from Apex, you can navigate to different url.

if(response.getReturnValue()){
   //navigate to url 1
}else{
  // navigate to url 2
}

The other ways are:-

  1. You can create a formula field on user object to identify the org is sandbox or not. Once that done, you can directly access it in your component by following this way:- force:recordData on user object
  2. If you just looking to get base url directly in aura js, you can use window.location.hostname
1
  • Thank you Sanket. If I use the code example you provided in my Aura, how do i hook it up to recognize if it's a sandbox from the apex? do i need to pass anything additional? Aug 6, 2022 at 13:41
0

In Apex the best way to do this is with the built-in URL class because it won't cost you any queries and it doesn't require any setup.

It looks like you are already calling some aura enabled method with a return value (maybe the opportunity that was created). You could make your original response a property on the response instead, alongside other properties like the base URL or URL for the record you created.

// sample apex controller method
@AuraEnabled
public static map<string,object> sendOpportunityData(string oppName){
    Opportunity opp = new Opportunity(name = oppName);
    insert opp; 
    string baseUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/';
    string sandboxName = baseUrl.substringBetween('--', '.');
    return new map<string,object>{
        'opportunity'=>opp,
        'opportunityUrl'=>baseUrl+opp.Id,
        'baseUrl'=>baseUrl,
        'sandboxName'=>sandboxName,
        'isSandbox'=>sandboxName != null
    };
}

You must log in to answer this question.

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