Oh my god. It's full of code!

Publicly Hosted Apex REST Class bug (maybe?)

I seem to have run across an odd bug. Custom Apex REST classes hosted via a Salesforce site will not work in a production version. It does work in sandbox and developer versions, so I am fairly convinced the approach is valid and my config is correct. This is a sample class.

global class testPublicRest {
global static String doGet() {
String name = RestContext.request.params.get('name');
return 'Hello '+name;

global static void testRespondentPortal()
// set up the request object
System.RestContext.request = new RestRequest();
System.RestContext.response = new RestResponse();

//First lets try and create a contact.
RestContext.request.requestURI = '/testservice';
//send the request

Sandbox version sans namespace – Works

Developer version with namespace – Works

Production version sans namespace – Fails

It fails saying that it cannot find a resource with that name.

<message>Could not find a match for URL /testPublicRest</message>

If you attempt to access it via the non secure domain you will get an HTTPS required message, so the resource is at least being located. It throws this error, which makes sense.

<message>HTTPS Required</message>

Seems like I found a bug maybe? To test yourself just copy and paste the above code. Host it via a salesforce site. Access it in your sandbox it should work (remember to access it via https. To get to a REST service just include /services/apexrest/yourService at the end of your site url. Then try deploying it to prod and doing the same. It will most likely fail.

I’d love to hear any feedback/ideas on this, as it’s a fairly critical part of a framework I am developing. Thanks!

Also if you do have any info, make sure to post it on the stack exchange. That’s probably the best place for this kind of thing.

UPDATE: Got it figured out. It was due to a permissions error on the guest account the site was using. Somehow an object for the services profile had an impossible permission setup (it had full read write modify all on an child object where it did not have read write modify all on the parent object (an opportunity)). So fixing the permissions and making sure the service had read/write to all objects and fields it required seems to have fixed this error. If you are getting this, make sure to check your object permissions and that everything the service needs is there, and that you don’t have some kind of weird setup issue like I did.

8 responses

  1. Just check in your production instance that the remote access is of connected app or normal as it is for all other instances

    January 12, 2013 at 3:07 pm

  2. In my production instance, works well. May be your site configuration ‘webServices’?

    January 13, 2013 at 12:15 am

  3. I really like what you guys are usually up too. Such
    clever work and coverage! Keep up the wonderful works guys I’ve included you guys to blogroll.

    March 15, 2013 at 2:52 am

  4. Admiring the dedication you put into your site and in depth
    information you offer. It’s great to come across a blog every once in a while that isn’t the same outdated rehashed information.
    Great read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

    April 16, 2013 at 9:58 pm

  5. This post will help the internet viewers for building up new website or even a blog
    from start to end.

    September 13, 2013 at 4:21 am

  6. I’m not sure why but this weblog is loading very slow for me.
    Is anyone else having this problem or is it a issue on my end?
    I’ll check back later on annd see if the problem still exists.

    September 29, 2013 at 12:20 am

  7. CIA

    Woah! I’m really enjoying the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance” between usability and appearance.
    I must say you have done a very good job with this.

    Additionally, the blog loads extremely fast for me on Internet explorer.
    Exceptional Blog!

    October 17, 2013 at 9:26 pm

  8. Boris Bachovski

    I just got the same issue and none of the above was the case. The problem was that the public site had 2 domains and I was using the wrong one. Ensure that you use the correct domain for the site.

    September 5, 2014 at 4:14 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s