4

The inspiration for this question comes from an apparent requirement to hard-code a production org id. Is it safe to rely on the production org id always remaining the same?

Reason for hard-coding is because this is in a mobile app rather than part of the Salesforce environment.

Context: we want our app to be able to download documents successfully when logged in to Sandbox as well as Production. In order to determine the correct URL to build to use servlet.shepherd, we need the mobile app to be able to determine whether it is logged into production or not, and if it's logged into a sandbox, ideally specifically which sandbox. We are using the iOS Mobile SDK version 4.0

EDIT: for the avoidance of doubt my question is not "how else can I solve my problem", my question is exactly as specified in the title. I'm looking more to discover whether hard coding production id in a mobile app is safe, than alternatives to doing so.

Bradley Thomas
  • 1,369
  • 4
  • 15
  • 33

2 Answers2

4

It shouldn't change, but why hardcode?

I would put the OrgId in a custom setting, and read it from there. That way you can test in Sandboxes, work in Prod and, if it ever changes, it's 5 minutes to modify without any code "movement".

UPDATE

There are a few things you can do that do not involve hardcoding... hopefully one of them will work

  1. To log into production you hit login.salesforce.com, sandbox is test.salesforce.com. Perhaps you can identify your environment because of that.
  2. Alternatively, you can call a method in SFDC to return whether the org you're hitting is "Prod" or Sandbox". A simple SoQL query will return that: select Id, IsSandbox from Organization limit 1. See here for more details.
Sebastian Kessel
  • 13,146
  • 12
  • 36
  • 59
  • Thanks, I've edited my question to include the reason for hard-coding – Bradley Thomas Mar 03 '16 at 16:21
  • Good Edit.... can you give us more context? I have a few ideas, but not sure which one(s) apply... – Sebastian Kessel Mar 03 '16 at 16:22
  • OK I have added more context to the question, thanks – Bradley Thomas Mar 03 '16 at 17:01
  • I've gave you some more ideas.... if one of them works, please mark the question as accepted so others can benefit. Thanks! – Sebastian Kessel Mar 03 '16 at 17:06
  • 1
    @SebastianKessel When you log in through the REST API, salesforce gives you this information. There's no need to do a query to figure out this data. – sfdcfox Mar 03 '16 at 17:15
  • Brian, I learned something new today. :) Thanks! (I don't use the REST API very often) – Sebastian Kessel Mar 03 '16 at 17:16
  • Mainly I just want to know if production org ids can ever change, and if so in what circumstances. That was my question, and even though I feel your answer is very helpful and I am grateful (have upvoted), as it stands I don't feel that it actually answers the question that I posed. I don't actually really need to change any code if production org ids never change. I really just want to know the situations (if any) that production org ids might change. That was my question. – Bradley Thomas Mar 03 '16 at 17:19
  • 1
    Brad, I don't know that they can change. But I also don't know that they can't. I don't think Salesforce guarantees that an ID will stay immutable. The good thing, though, if that if SFDC is planning to change the format of IDs you will have enough notice to change your code accordingly. I think you can leave your code as is. – Sebastian Kessel Mar 03 '16 at 17:34
4

A Sandbox's ID may change during a refresh, and definitely if you create a new Sandbox. Production ID values will always remain the same. However, when you log in, your session information already includes the org ID, and is available when you log in. You'll get an Identity ID URL through most authentication schemes, that might look like this:

https://login.salesforce.com/id/00Dx0000000BV7z/005x00000012Q9P

(Example URL copied from the Salesforce REST API docs).

You can call this URL with your new Access token to get information about the org and user you're logged in as.

sfdcfox
  • 489,769
  • 21
  • 458
  • 806
  • Just for clarity -- if SFDC moves your Prod org to a new POD, will the orgId change? – cropredy Apr 15 '17 at 02:52
  • @cropredy Production will not change, even if your org migrates pods. Many older orgs actually have an org Id that do not match their pod identifier. – sfdcfox Apr 15 '17 at 02:55