19 votes
Accepted

How to handle an HTTP response time beyond two minutes?

Salesforce now allows Apex developers to invoke Asynchronous Callouts. This means that now, developers can initiate callouts from within Salesforce which can respond back in more than 2 minutes (...
user avatar
  • 2,559
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
  • 430k
9 votes
Accepted

HTTP Callout Problems in Pre-release

You can't call out from a pre-release org, since the security rules are different from our production/developer environments.
user avatar
  • 16.2k
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
8 votes

You have uncommitted work pending. Please commit or rollback before calling out

If the query in your insertFunction returns more than one Account then you wil be performing this sequence: Http request database update HttpRequest database update ... which as the error message ...
user avatar
  • 129k
8 votes

Missing SOAP action header

Looking at the Partner WSDL, the soapAction for the login operation is the empty string. <operation name="login"> <soap:operation soapAction=""/> <!-- .... --> ...
user avatar
7 votes
Accepted

System.StringException: Unrecognized base64 character: \

The content you are receiving is not Base64: it is Base64 inside JSON, which you are asking the server to provide to you. Base64 encoding uses the characters [A-Za-z0-9+/]. The backslash and quote ...
user avatar
  • 86.1k
5 votes

How to set timeout for web service call

The argument to HttpRequest.setTimout is a millisecond value so for 2 minutes: req.setTimeout(2 * 60 * 1000); PS With the clarification that the desire is to make the web service call after a 2 ...
user avatar
  • 129k
5 votes
Accepted

How to work with HttpResponse?

So, the response you are receiving is a JSON string. You can parse it like this: String json = response.getBody(); Map<String,Object> responseMap = (Map<String, Object>) JSON....
user avatar
4 votes
Accepted

Verify getChildElement() is not null

The previous answer I gave you assumed success, but for robustness, you can certainly iterate through all expected values. For example: Dom.Document doc = new Dom.Document(); doc.load(res.getBody()); ...
user avatar
  • 430k
4 votes
Accepted

Getting error while deserialize

A common cause is that the server is responding with a HTML page that contains error information rather than the JSON. Typically the status code in the response res.getStatusCode() would also be some ...
user avatar
  • 129k
4 votes
Accepted

HttpResponse body is empty even on successful request

Finally after analyzing the request sent by the browser and Hurl.it it seems that I was missing a header called Accept-Encoding whose value was 'gzip'. req.setHeader('Accept-Encoding', 'gzip'); Now ...
user avatar
4 votes

Parse JSON Response and add to Visualforce Page

Using JSON2Apex (here), you can easily generate a class that you can use to deserialize your JSON. JSON2Apex outputs code like this: public class Outer { public List<Ideal> ideal; ...
user avatar
4 votes
Accepted

HTTP Request Ignoring Body

If you use a HEAD request rather than a GET, the server will return all the headers associated with the URL (and so you can tell if it exists, content type etc), but won't return the body. so instead ...
user avatar
  • 7,820
4 votes
Accepted

How to make HttpGet service pagination with a limit of a million or more?

Simply put, you can't. While is trivial to build a service that returns, say, up to 50,000 rows, there's simply no mechanism for preserving a QueryLocator across Apex Code transactions, and with one ...
user avatar
  • 430k
4 votes
Accepted

Parsing JSON response giving null pointer exception

Try it with JSON like: {"stock_prices":[{"code":"AAPL.US","timestamp":1569593940,"gmtoffset":0,"open":220.54,"high":220.96, "low":219.79,"close":220.24,"volume":3397713,"previousClose":219.89, "...
user avatar
  • 26.7k
4 votes
Accepted

Problem with testing void HttpRequest

I imagine that you're probably running into an exception like "List index out of bounds: 0" when you run this test. You haven't inserted any Account records as part of your test method. That,...
user avatar
  • 46.6k
4 votes

Details for function Script.Util.HttpResponse

As a note, Script.Util.HttpResponse is not a user function, it is instead the Response Object from the other Script.Util calls. For details to correctly parse information from it, I would look at my ...
user avatar
  • 27.9k
3 votes

How to handle an HTTP response time beyond two minutes?

@Anshul provided an Amazing answer on how to handle callouts in async way. But still the question here is"The maximum callout time is 120,000 milliseconds" i.e You can't go beyond 2mins via any async ...
user avatar
3 votes
Accepted

Can successfully send GET request to Chatter API but not POST, receiving INVALID_SESSION_ID

the shell is interpreting characters in the URL as shell commands, and not part of the URL, you need to quote the URL string in your curl call, e.g. curl -X POST -H "Authorization: Bearer ..." "https:...
user avatar
  • 7,820
3 votes
Accepted

Changing HTTP header code in POST response

This pattern where you explicitly update the RestContext allows you to set the status value: @HttpPost global static void doPost() { // Do stuff RestResponse res = RestContext.response; ...
user avatar
  • 129k
3 votes

HTTP call using Salesforce

Blob headerValue = Blob.valueOf(username + ':' + password); String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); This code is not correct. The automatic "toString" method ...
user avatar
  • 430k
3 votes
Accepted

JSONParser.getText() results in null but not interpreted as null in if statements

Use VALUE_NULL token getText() Returns the textual representation of the current token or null if there's no current token. Since you explicitly were calling getText() without checking for null ...
user avatar
  • 53.1k
3 votes
Accepted

Downfalls to using HTTPGet() to see if an asset exists

Firstly, you don't need to print (v) a variable if you are using it inside an AMPscript block. You can change: set @ItemAssetCall = HTTPGet(v(@ItemAsset),true,1,@ItemAssetCallStatus) to: set @...
user avatar
  • 15.6k
3 votes
Accepted

Having trouble deserializing a JSON string of Objects

When you use JSON.deserializeUntyped(data), you need to perform type casting to tell the Apex compiler what kind of object you're working with or you'll get errors because the type Object has no ...
user avatar
  • 86.1k
3 votes

How to send error from HttpPost to Callout if ExternalID is the same?

you can do SOQL query on Item__c filtering by ExternalID__c field. If result is not empty, then set statusCode to relevant one. You can find list of all status codes here. do not set RestContext....
user avatar
3 votes
Accepted

System.NullPointerException: Attempt to de-reference a null object Error while parsing XML response

You have <retour> elements without a child <url> element, as for example <retour> <publication>20180917123004</publication> <production>20180917123004</...
user avatar
  • 86.1k
3 votes
Accepted

How to handle null keys in JSON response?

You can use the Safe Navigation Operator (?.) to safely ignore null values: accountWrapper.personDeathYear = person.death?.yearOfDeath; This returns null the moment a NullPointerException would ...
user avatar
  • 430k
3 votes
Accepted

Salesforce Apex Api is Returning response with Escape Character

Don't do this: return JSON.serialize(responseWrapper); Just return responseWrapper; You're returning a String, which Apex is then serializing to JSON by escaping it. You don't need to serialize your ...
user avatar
  • 86.1k
2 votes

Missing SOAP action header

I figured out the problem and now im getting the session ID actually the mistake was in setting the action header initially i have setted Soap action as req.setheader('SOAPAction',''); then i ...
user avatar

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