function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Donald HurseyDonald Hursey 

Get Instance for an org in apex class.

I have an visualforce page controller that stopped working because the code snippet below is looking for the instance name in the the url. I have another instance tha is working, and has the instance number available in the url for the visualforce page. I've checked over domains, sites, remote site settings to see if that was the issue, but didn't see anything that sttod out as the problem. Is there another way to get the instance number to be used in the tooling API url?
 
HttpRequest req = new HttpRequest();
            req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
            req.setHeader('Content-Type', 'application/json');
            
String host = System.URL.getSalesforceBaseUrl().getHost();
            Integer firstDot = host.indexOf('.')+1;
            String sUrlRewrite = 'https://'+ host.substring(firstDot,host.indexOf('.',firstDot)) + '.salesforce.com';
            req.setEndpoint(sUrlRewrite+'/services/data/v33.0/tooling/query?

 
Best Answer chosen by Donald Hursey
Donald HurseyDonald Hursey
Finally figured out the issue and am posting the fix in the event anyone runs into the same issue. The problem was in the company information --> critical updates --> Remove Instance Names From URLs for Visualforce, Community Builder, Site.com Studio, and Content Files

Make sure the option here is deactivated. Doesn't really show the instance in the url, but if you look at the headers you will see that instance number in the url when the option above is not active.

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Donald,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

https://salesforce.stackexchange.com/questions/64616/how-to-get-salesforce-instance-url

https://stackoverflow.com/questions/9369653/retrieve-salesforce-instance-url-instead-of-visualforce-instance

https://salesforce.stackexchange.com/questions/53201/getting-instance-name-of-org-with-a-domain

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Donald HurseyDonald Hursey
Thanks Khan,

The issue seems to be in one org where it is working the instance number is included in the url and headers so I'm able to get the instance and re-write the url to use in the tooling api link. https://na11.salesforce.com The org where it isn't working doesn't have the instance number inthe url so I can re-write it for the tooling api url. Here are link examples from both. I'm not sure why the instance would be in one and not the other, and I'm also wondering if there isn't another way to get the instance. 

Works in this org
https://myorg1--namespace.na11.visual.force.com/apex/FieldUsage

Doesn't work in this org.
https://myorg2--namespace.visualforce.com/apex/FieldUsage
Donald HurseyDonald Hursey
Finally figured out the issue and am posting the fix in the event anyone runs into the same issue. The problem was in the company information --> critical updates --> Remove Instance Names From URLs for Visualforce, Community Builder, Site.com Studio, and Content Files

Make sure the option here is deactivated. Doesn't really show the instance in the url, but if you look at the headers you will see that instance number in the url when the option above is not active.
This was selected as the best answer