function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Secured Signing DeveloperSecured Signing Developer 

@future method not working

I have a method in Lightning server controller. This method will call an external API. And before calling out, it would check the token. if it's expiring soon, e.g. 1 day, the method will call a method to refresh token. 
The @future method should be very perfect for refreshing token. otherwise it would have a calloutexception error.

But now the problem is even I do nothing ('comment all the code') in the future method, it throws calloutexception error 'You have uncommitted work pending. Please commit or rollback before calling out'. if I remove @future annotation, it would be fine.

So strange!
Best Answer chosen by Secured Signing Developer
Secured Signing DeveloperSecured Signing Developer
https://salesforce.stackexchange.com/questions/64143/calling-a-callout-via-future-still-gives-uncommitted-work-pending-error

call @future method itself will create a record so no callout can be called after calling a future method

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

I think you can do 10 callouts in a single class, and you must do these before calling DML statments.

so, make the loop collect up a list of objects values , then insert or Update  them after you have completed the callouts

hope it will be helpfull. 

Thanks
karthik
 
Secured Signing DeveloperSecured Signing Developer
https://salesforce.stackexchange.com/questions/64143/calling-a-callout-via-future-still-gives-uncommitted-work-pending-error

call @future method itself will create a record so no callout can be called after calling a future method
This was selected as the best answer