2

After my app is granted an OAuth token by the user, I take the "url" provided in the OAuth request, and the token, and make a request back to salesforce to store some settings.

In my unmanaged package testing the url looked something like this:

https://na3.salesforce.com/services/apexrest/customrestendpoint

And this domain matched the domain in salesforce while the user was using the app.

Now that I uploaded a *Managed package though, it seems to be different. The URL in Salesforce looks like this

https://appnamespace.na3.salesforce.com/apex/blahblah

and the POST request to https://na3.salesforce.com/services/apexrest/customrestendpoint is returning a 404

Is this expected? Do I need to append the app namespace to my API requests to the custom REST endpoint? Or is the namespace optional and can I just omit it when creating the managed package?

5

In the org you are developing the managed package in that namespace is implied when no namespace is specified. In the case of customer orgs this is not true - they have their own namespace for unmanaged components, and your namespace needs to be explicitly included on apexrest requests.

The docs do describe this, but it's a bit down the page and easy to miss if you aren't looking for it.

Apex REST methods can be used in managed and unmanaged packages. When calling Apex REST methods that are contained in a managed package, you need to include the managed package namespace in the REST call URL. For example, if the class is contained in a managed package namespace called packageNamespace and the Apex REST methods use a URL mapping of /MyMethod/*, the URL used via REST to call these methods would be of the form https://instance.salesforce.com/services/apexrest/packageNamespace/MyMethod/

This is substantially easier than trying to prepend the subdomain, since the format for that can vary wildly based on if my domain is enabled, if you're in a sandbox, and other factors.

In the event it's helpful to do so, you can determine your code's current namespace at runtime. See https://salesforce.stackexchange.com/a/28977/60

  • Interesting. So if my external app is running the same code on the OAuth Callback in staging/development as it is in production, how am I supposed to know if I need to append the prefix or not? – user1813867 Jul 23 '16 at 0:07
  • @user1813867 You can query the Organization object for NamespacePrefix. It'll be null for subscribers and not-null for your developer org where the package lives. – sfdcfox Jul 23 '16 at 2:06
  • Interesting. I'll try that. Hope it works! – user1813867 Jul 23 '16 at 5:23
  • Be aware that only users with view all data or apex running without sharing will be able to query the Organization table, which is one of the reasons I prefer the answer I linked at the end of mine - you won't have to justify it in security review and it doesn't burn a query. – ca_peterson Jul 23 '16 at 5:24
  • 1
    Extra note: the namespace is case sensitive (as URLs should be), for in my scenario I had to use CAPS for the namespace part of the URL or I got a miss. – Matt Lacey Oct 30 '16 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.