47 votes
Accepted

How to deserialize a JSON list in Apex

Assuming that the JSON data is from a List<Account> in the first place (as it appears to be) you do not need to define your own class but instead can use the Account object type: String s = res....
user avatar
  • 129k
32 votes
Accepted

How to correctly return data and errors from REST annotated methods?

Having written a Salesforce managed package app making use of the automatic serialization and deserialization based on the method signature, in future I will avoid that pattern. The primary reason is ...
user avatar
  • 129k
19 votes
Accepted

Apex Rest Vs Platform Events for Inbound SF integration which one to choose?

UPDATE I have also discussed this topic with some more detail on on my blog here. Based on your requirements where: External System will push data in Salesforce. My recommendation is to ...
user avatar
  • 28.9k
18 votes
Accepted

REST error handling design

You should use standard http status codes. You can set the status code on RestContext.response anytime before you return. Salesforce will automatically handle most of these. For example, you don'...
user avatar
  • 9,426
16 votes
Accepted

RestContext.response returns null when run in Test Context

You will have to set up your own RestResponse and RestRequest object. RestRequest req = new RestRequest(); RestResponse res = new RestResponse(); req.requestURI = 'https://na11....
user avatar
  • 22.2k
13 votes
Accepted

RestResource Class: Set the HTTP Response Status in APEX

RestContext.response.statusCode = 400; You can set any status code defined in RestResponse; simply set RestContext.response.statusCode to any value you'd like from the following list: 200 OK 201 ...
user avatar
  • 429k
13 votes
Accepted

Help on Invokable Apex Test class and @Future callout Apex test Class

Basically, you need to know about how to test HTTP callouts, and how to test future methods. Once you've gotten that far, you'll see that the unit test would look like: @isTest class MakeCalloutTest ...
user avatar
  • 429k
12 votes
Accepted

apexrest: controlling HTTP error codes on exceptions

Below is a pattern I use to vary the status code returned. It only maps two categories of exceptions but if you need to you can add more. Note that only status codes from this list can be returned - ...
user avatar
  • 129k
12 votes
Accepted

Is SOAP API becoming obsolete?

This statement is simply stating that a particular version of the API is only guaranteed to be supported for three years. In other words, salesforce guarantees version compatibility for three years. ...
user avatar
  • 429k
12 votes
Accepted

How to see JSON response for REST service?

You can head over Salesforce Workbench and use the REST Utility to test the endpoint . Your endpoint to test will be /services/apexrest/BP_Routes Also looks like the method is accepting a string ...
user avatar
11 votes

How to remove attributes before JSON is sent from Custom REST Apex?

It's a bit hacky, but you could remove the attributes by serialising and deserialising the SObjects to Maps, like this example: Account acc = [Select Id, Name, (Select Id, Name From Contacts) From ...
user avatar
11 votes
Accepted

What are the reasons to use a Visualforce page on Force.com Sites versus a @RestResource to accept incoming REST calls?

Historically the ability to expose APIs as publicly accessible via sites wasn't very well known, which might have lead to a lot of REST-like visualforce pages by those simply ignorant of the neat ...
user avatar
  • 22.1k
11 votes
Accepted

Publicly exposed Rest class does not support post method

I found the problem as I make my site secure by HTTPS and was doing a request on HTTP, So the post request was discarded. Thanks
user avatar
  • 1,018
10 votes
Accepted

How I can know who is calling a REST resource?

Regardless as to whether the REST API is exposed behind authentication or not, you have access to the RestRequest within your Apex code. You can use the RestRequest.remoteAddress to discover where the ...
user avatar
  • 26.6k
9 votes

"Session expired or invalid" problem calling an @RestResource from Apex in Winter '15 preview; UserInfo.getSession returns null for Force.com Sites

Support have come back with: confirmed with R&D that this is not a BUG rather a security update that is introduced in Winter'15 release and advise: affected customers will need to adjust ...
user avatar
  • 129k
9 votes

Does API calls to Apex REST service published on a public Force.com site come under Daily API Limit

I built a similar service some time ago, and made some similar verifications and tests, I can provide some answers as follows: Any request for 'Your Domain" will count as a page view, see docs You ...
user avatar
  • 2,842
9 votes
Accepted

APEX FATAL_ERROR System.CalloutException: Read timed out

We can't increase the timeout more than 120 sec since that is the maximum limit. The only workaround is to modify the query on the external webservice application to respond within 120 secs. Its ...
user avatar
9 votes
Accepted

Can API access be limited to only via Apex classes?

In Winter '18, they added an ability to control access to Apex REST Services that is distinct from the API Enabled permission on profiles that provides access to the standard APIs. This allows you to ...
user avatar
9 votes
Accepted

Check if apex is being run in Rest Resource

You can check if you're in a REST call via: Request.getCurrent().getQuiddity() == Quiddity.REST You can read more about the Request object, as well as the possible values for Quiddity.
user avatar
  • 429k
8 votes

Rest Routing with Multiple Wildcards / Supporting REST Routing for Nested Resources

While it's not clear why that doesn't already work, to get your code out the door, my suggestion is to build your own REST dispatching service. The following pseudo code implementation works as ...
user avatar
8 votes
Accepted

Posting data to SFDC via rest API

Yes there is standard way to create any record in Salesforce via POST /services/data/v34.0/sobjects/OBJECTNAME/ replace OBJECTNAME with object name for which you want to insert the record. for ...
user avatar
  • 10.3k
8 votes

Database SavePoint and Rollback

For the current code shown in the question the database.setsavepoint() and database.rollback() calls have no functional value and could be removed entirely for the same result. You aren't doing any ...
user avatar
8 votes
Accepted

Apex REST request requires attributes field

According to the JSON documentation: Object deserialize(String jsonString, System.Type apexType) Deserializes the specified JSON string into an Apex object of the specified type. You can use ...
user avatar
  • 4,057
8 votes

what is difference between JSON generator and JSON parser?

The two are opposite features. The JSONGenerator takes data input and outputs JSON strings. The JSONParser takes JSON string input and outputs objects. If you're sending data to an outside source, ...
user avatar
  • 429k
8 votes
Accepted

Apex Suicide Scheduler getting stuck as a queued job

Suicide scheduling has never been officially supported, and in fact was explicitly stated that at some point they might choose to limit it to some period of time, such as every 5 minutes minimum: ...
user avatar
  • 429k
8 votes
Accepted

Variable is not visible

You cannot assign to a property AccLinks = (kvl != null && kvl.Value__c != null ? kvl.Value__c : 'some_url'); if that property does not have a setter method declared. To make the property ...
user avatar
  • 85.9k
7 votes
Accepted

Passing custom model to Apex REST custom endpoint

You can design your own interface using simple Apex classes. So to allow this JSON to be posted: [ {"query": "SELECT ...", "externalId": "65202639238"}, {"query": "SELECT ...", "externalId": "...
user avatar
  • 129k
7 votes
Accepted

How to Consume JSON Array in Apex REST API Class : Stuck with deserialization Error

The { starts an object and the [ starts an array. It is convenient to deserialise into inner classes (because the class field names must match the JSON names) and then transfer from those into the ...
user avatar
  • 129k
7 votes
Accepted

Future method cannot be called from a future or batch method: Error

As the error states you are calling a future method from another future method . Your class A calls Class B but I see again you call Class A methods in Class B causing recursion . Hence would ...
user avatar
7 votes
Accepted

URL_NOT_RESET error REST API

test.salesforce.com or login.salesforce.com can be used to first login. Thereafter requests should be made using the instance_url and the access_token that you receive as a response on first login. ...
user avatar
  • 230

Only top scored, non community-wiki answers of a minimum length are eligible