+ Start a Discussion
Jason Bellis (Admin)Jason Bellis (Admin) 

Retrieve sandbox list via API

We have a large SFDC development shop, with several dozen sandboxes.
There is a lot of churn and management around sandbox creation, deletion, maintenance, user access, etc.
We take every opportunity to create automation around these repetitive tasks.

A real impediment to the automation is the fact that we cannot programmatically get a list of sandboxes.
We have a very real need to retrieve the list of active sandboxes through the API (not via graphical user interface).

Example:  a user leaves the company.  We have 45 active sandboxes.  We need to disable the user in each sandbox.
It was easy to automated the process, except for the fact that we cannot get the list of sandboxes.
We have to maintain this list manually in a TXT/CSV file outside of Salesforce.  
Not fun, especially since even the GUI interface does not give a nice way to copy this data out to text or CSV.
(try to copy / paste off the sandbox page, you get an ugly html table needing extensive formatting in your paste target).
buyan thyagarajanbuyan thyagarajan
Hi Jason,
I know there is no api to list the sandboxes used in an organization. But you can use a custom setting where you can store all the sandbox organization ids and populate them and use them in your apex batch job to get the list. Would that not work for you? Please click like if you like my answer..
Jason Bellis (Admin)Jason Bellis (Admin)
Would this custom setting need to be manually maintained by an Admin as sandboxes were created and removed?  I'm trying to avoid maintaining a list, instead I would like to retrieve the current list of sandboxes in real-time. The sandbox names (not sandbox IDs) would actually be the important data for me. My goal is to login at each sandbox automatically with a programmatic method.

 I.e. the username for each sandbox can be derived from sandbox name in the list  --  prod_username@company.com.sandboxname
buyan thyagarajanbuyan thyagarajan
Hi Jason,
If your goal is to login to each sandbox and retrieve information, this approach may not work as you cannot have code run in a production environment logging in to another sandbox. However this can be automated using ant build scripts where you would store all the credentials of the sandbox in a text file and have ant query each sandbox using the meta data api login and activate users. I have done this and can share this with you further on this. Could you tell me what automated tasks you want to perform on each sandbox?
WoW - this is an old thread but interesting.  Are you stil around Buyan or Jason?  I see something interesting and concerning that Buyan mentioned!  Is it true that I can't have production custom apex code execute sandbox code?  For example, do a http callout to a custom REST in sandboxes?  I want to buid a solution to deactivate users from all production associated sandboxes when a user is deactivated by our production deprovisioning process.  We don't want the overhead of using third party coupling as that requires many tickets for us to open just to get all the pieces working.  We would rather have it self contained in our SF production org so when we spin up a new sandbox, we simply create a connected app / User Credentials, etc. and a trigger on the User object would handle calling out to each sandbox and deactivating the matching username.  If we cannot have production code call a sandbox then can we have a sandbox call another sandbox?  I have POC'd this from developer org to developer org and it works.  Your help is appreciate from ALL.
Jason Bellis (Admin)Jason Bellis (Admin)

We did find a "sort of work-around" to retrieve the current list of Sandbox names from production.
We found this Salesforce app called "Environment Hub".

Now that we installed this in our production org, we have a table that we can query which holds all the sandbox names.

SELECT Id, DisplayName, MemberEntity, Instance, IsSandbox, OrgStatus, OrgEdition
FROM EnvironmentHubMember
WHERE OrgStatus != 'Deleted'
ORDER BY DisplayName


I am using Named Credentials, Connected App and Auth provider.  I was able to derive each sandbox name from the URL on the named credential since our sandboxes are using "My Domain" and our end username is always '.sandbox name', i.e '.uat', etc.  I can't seem to find an example on the internet that show a callout from apex to an apex custom REST service passing in a list (usernames in this case).  Do you have a website / link that provides very basic callout (apex) sending a list of string to a custom rest apex service and how it serializes the list and deserializes it on the service end?