Skip to main content
57 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....
Keith C's user avatar
  • 136k
35 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 ...
Keith C's user avatar
  • 136k
21 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 ...
Jayant Das's user avatar
  • 30.6k
20 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....
Ashwani's user avatar
  • 22.6k
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'...
NSjonas's user avatar
  • 10.2k
14 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. ...
Thamilhan's user avatar
  • 312
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 ...
sfdcfox's user avatar
  • 497k
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. ...
sfdcfox's user avatar
  • 497k
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 ...
Mohith Shrivastava's user avatar
11 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 ...
Phil W's user avatar
  • 37k
10 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.
sfdcfox's user avatar
  • 497k
9 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 ...
David Reed's user avatar
  • 93.2k
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 ...
Kris Goncalves's user avatar
9 votes
Accepted

Is it possible to have multiple POST calls in the same Apex REST class?

You're free to write your resource as: @RestResource(urlMapping='/abc/*') Where * indicates a wildcard; this will match any resource that matches this wildcard pattern. From here, you can then ...
sfdcfox's user avatar
  • 497k
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 ...
Daniel Ballinger's 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 ...
Ilya Lepesh's user avatar
  • 4,125
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, ...
sfdcfox's user avatar
  • 497k
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: ...
sfdcfox's user avatar
  • 497k
7 votes
Accepted

System.JSONException: For input string

public Integer learningRate; //0 In your JSON, it appears as a Decimal value. Change the code as follows: public Decimal learningRate; //0.0
sfdcfox's user avatar
  • 497k
7 votes

Clear platform event limits?

The phrasing is a bit ambiguous in terms of which link you should visit: For more information see the Resources section. However, that section does include the below document which I believe answers ...
Adrian Larson's user avatar
  • 151k
7 votes
Accepted

Delete multiple records using REST Api

I think endpoint URL should be: https://ap6.salesforce.com/services/data/v43.0/composite/sobjects?ids=00P0K000016YWusUAG,00P0K000016YWutUAG
Ayub's user avatar
  • 3,543
7 votes

Apex REST API - Unable to set StatusCode to 429

429 Too Many Requests is not an appropriate status to return from Apex that is processing a REST request, and so presumably that's why the server prevents you from doing so. The relevent RFC indicates ...
Thomas Taylor's user avatar
7 votes
Accepted

Improve performance of a rest resource

Processing server-side will always cost performance. The best performance would be if you could return the data without a wrapper. Failing that, the next best option would be to optimize the code. ...
sfdcfox's user avatar
  • 497k
7 votes

Is it possible to have multiple POST calls in the same Apex REST class?

The URL that you use to access a custom apex rest service is determined by the @RestResource annotation, which can only be used at the class level. So it makes sense for Salesforce to only allow a ...
Derek F's user avatar
  • 62.7k
6 votes

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

You can write a Wrapper object instead of the actual object . Create a wrapper class like below public class ResponseWrapper{ public list<Account> lstaccounts; public boolean isError ; ...
Mohith Shrivastava's user avatar
6 votes
Accepted

Json Generator Object within Array

I think you're missing out on starting some of your objects, so it should be gen.writeStartObject(); gen.writeFieldName('tasks'); gen.writeStartArray(); for (integer i = 0; i < ...
Aidan's user avatar
  • 13.7k
6 votes
Accepted

Passing parameters while testing REST web service

Since the test isn't actually making a REST request to that URI (it's just setting a global context var), we can't actually unit test our urlMapping parameter. Instead, we have to add the parameters ...
Dman100's user avatar
  • 2,455
6 votes
Accepted

Unexpected character ('}' during JSON desearilization

Once you add the curly brackets, your JSON looks like this { [ { \"sampleNumber\":\"WO-02609\", \"SampleID\":\"a3F5C000000CHATTAM\", \"state\":\"Open\" } ] } Which is ...
Sebastian Kessel's user avatar
6 votes
Accepted

Generate PDF with REST Web service

See getContentAsPDF() after insert a record in the same excution context that explains that the PDF generation call won't see any changes made in the transaction that calls it. If you don't require ...
Keith C's user avatar
  • 136k
6 votes
Accepted

@IsTest - How to get the RestContext.response from @HttpPost if the post method is not returning

You need to call the actual http method for it to invoke the API and then if you check, it should have the response. @IsTest static void TestBehavior() { RestRequest request = new RestRequest(); ...
metasync's user avatar
  • 1,772

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