Skip to main content
56 votes
Accepted

Use a Named Credential with API key?

Well... sort of. The only two officially supported methods are by Basic Authentication and OAuth tokens. That site uses a non-standard API design, so it can't directly benefit from using a Named ...
sfdcfox's user avatar
  • 495k
22 votes
Accepted

Apex Framework / library for consuming REST services

The ffhttp library should do the trick: https://github.com/financialforcedev/ffhttp-core It includes error handling, redirections, building header fields, oauth, mime attachments and more. EDIT - ...
frup42's user avatar
  • 414
21 votes
Accepted

{"error":"unsupported_grant_type","error_description":"grant type not supported"}

Change: request.setEndpoint('https://ap5.salesforce.com/services/oauth2/token'); to either Login or test depending on if in production or sandbox request.setEndpoint('https://[login | test]....
Eric's user avatar
  • 54.2k
20 votes
Accepted

Where to Store Token Auth?

Your assessments are pretty close, but you've got some glaring errors I'd like to bring up. Custom Settings / No Encryption? You can encrypt the token using the Crypto class if you need to. Just ...
sfdcfox's user avatar
  • 495k
19 votes
Accepted

Does queueing a future method count as DML?

Using System.enqueueJob, Database.executeBatch, System.scheduleBatch, System.enqueueJob, and @future methods all modify the state of the database, so for purposes of callouts, count as a DML operation....
sfdcfox's user avatar
  • 495k
18 votes
Accepted

How do I unit test code that includes a callout?

As part of the isolation of the test context, Salesforce does not allow your code to make REST or SOAP callouts during test execution. This includes all code that's executed in test context, even if ...
17 votes
Accepted

Uncommitted work pending in unit test with trigger and queueable callout

You'll need to test the method directly, and test the Queueable separately. The easiest way to do so is a static flag. public class MyQueueable implements Queueable @TestVisible static Boolean ...
Adrian Larson's user avatar
  • 150k
16 votes

How to solve callout error on VF Page? You have uncommitted work pending. Please commit or rollback before calling out

So many wrong answers here... You definitely CAN do a callout and then DML in same transaction. You cannot do it THE OTHER WAY AROUND. Only Oles Malkov got it right. Also switching to @future may ...
Jo_gurt's user avatar
  • 161
15 votes
Accepted

Can I use Named Credential merge fields as URL parameters on the endpoint?

You can only use merge fields within setHeader and setBody, as the options specify. This may mean that you need to use POST with application/x-www-form-urlencoded, or arrange for a specific header to ...
sfdcfox's user avatar
  • 495k
14 votes
Accepted

What can cause "System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out"?

Here is a list of the operations that could possibly count as a DML when executed before a callout: Database methods convertLead delete deleteAsync deleteImmediate emptyRecycleBin executeBatch ...
14 votes
Accepted

Named Credentials and support for the OAuth2 Client Credentials Grant Type and alternatives

In my experience, the bullet points you've highlighted are pretty much on-target. What I would add is that both NamedCredentials and CustomApplications, make the assumption the OAuth provider (the idP)...
crmprogdev's user avatar
14 votes
Accepted

Named credential not accessible by non system admin profiles

Ok I found out the root cause : the profile was missing Read access on the object User External Credentials.
Chris's user avatar
  • 336
13 votes
Accepted

Batch Apex that does DML and multiple callouts

You can combine the callout and the DML in the same method if you want to; the only restriction is that no callouts are allowed after a DML. Each call to start, execute, and finish are separate ...
sfdcfox's user avatar
  • 495k
13 votes

Can you call a webservice from a Platform Event Trigger?

Older question but felt compelled to add an answer, as I was confused by what was presented here so far. If you are asking "can I make an Apex HTTP Callout from my Apex Platform Event Trigger?&...
Grekker's user avatar
  • 1,092
13 votes
Accepted

Callout Limits for future methods and queueable apex

I quickly wrote a class with future method to test this behaviour. public class FutureClassLimitsTest { @future(callout=true) public static void docallouts(){ for(Integer i=0;i<...
Pranay Jaiswal's user avatar
13 votes

Named Credentials and support for the OAuth2 Client Credentials Grant Type and alternatives

EDIT: As of Winter '24, Client Credentials flow is natively supported by the "new" Named Credentials construct. The below workaround is no longer necessary. Bit of a hack indeed, but there ...
James Quinn's user avatar
12 votes
Accepted

Help with FLOW/Apex Error: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

Found the answer, I think, or at least a reasonable workaround. I remembered that Triggers can't make callouts -- they can only call an @future method to do so. I wondered if Flows are the same, so I ...
PatMcClellan__c's user avatar
12 votes
Accepted

Passing a List of Custom Objects to Batch Apex

You can use this signature instead: public List<SObject> start(Database.BatchableContext context) { return myRecords; } Please note that your batch classes do not need to be global and you ...
Adrian Larson's user avatar
  • 150k
12 votes
Accepted

Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') in very simple Callout

A GET request to that URL is returning HTML not just the JSON body. HttpRequest req = new HttpRequest(); req.setHeader('Content-Type','application/json;charset=UTF-8'); req.setEndpoint('https://swapi....
Mark Pond's user avatar
  • 23k
12 votes
Accepted

How can I share state between 'Queueable' executions?

The standard approach for this is to pass state via the constructor class MyQueueable { Object state; public void execute(QueueableContext qc) { // do work using state up to the point where ...
cropredy's user avatar
  • 71.9k
11 votes

Can we callout and chain a Queueable class?

As Dan Appleman notes in the latest edition of Advanced Apex, a Queueable can call a Future method, and a Future method can enqueue a Queueable job. This works in Spring 16, and will allow chaining of ...
Jeremy Nottingham's user avatar
11 votes
Accepted

Diference between @future (callout= true / false)

If you want to call third party system or any http call outside of salesforce system you need to set callout=true in the future method. If you set it to false it means the method is not making any ...
RedDevil's user avatar
  • 7,821
11 votes
Accepted

Making a callout in Apex to Marketing Cloud SOAP API

So first of all you need to perform login action to retrieve fuel OAuth token. Request format is following due to the documentation POST https://auth.exacttargetapis.com/v1/requestToken Content-Type:...
Patlatus's user avatar
  • 17.2k
11 votes

Error on Batch Apex for sun.security.validator

This happens when your endpoint uses a Load Balancer. So when you make calls in quick succession the Load Balancer Splits them to different servers. This is the reason why it doesn't happen every ...
Pranay Jaiswal's user avatar
11 votes
Accepted

Guarantee only a single asynchronous job runs at a time

I've implemented the following in numerous orgs and it works pretty well. It's similar to Keith's suggestion, but a bit more detailed and it does usually do near-realtime processing: Create a custom ...
Aidan's user avatar
  • 13.7k
11 votes

Create a Time Delay in APEX without Exceeding CPU Limit

If your current context allows PageReference.getContent, you can get your approximate 2 second wait using a Visualforce page. Controller public class DelayController { public DelayController() { ...
sfdcfox's user avatar
  • 495k
11 votes

Handle chain of long-running callouts from Salesforce

Roughly speaking, this is what Queueables are for. Once Transaction Finalizers are GA, I think they'll be a pretty good solution for this. There are many concerns, though: The Queues If you're ...
Aidan's user avatar
  • 13.7k
11 votes

How can I share state between 'Queueable' executions?

A class calling itself can enqueue itself. No need to construct a new object. public class MyQueueable implements Queueable { Integer counter = 0; public void execute(QueueableContext context) { ...
sfdcfox's user avatar
  • 495k
10 votes
Accepted

Can we callout and chain a Queueable class?

Spring '17 provides: Make Web Service Callouts from Chained Queueable Jobs Apex now allows web service callouts from chained queueable jobs. Previously, a queueable job could make a web service ...
Adrian Larson's user avatar
  • 150k
10 votes
Accepted

System.AsyncException: Maximum callout depth has been reached

There is an Idea out there called Allow callouts from chained queueable Apex jobs. Here's the description, with some minor reformatting: Allow callouts from chained queueable Apex jobs Currently (...
Adrian Larson's user avatar
  • 150k

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