You need to sign in to do that
Don't have an account?
Oleg Malinovsky
On GET request: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
Hi everyone,
I have a simple code that should make GET call to my endpoint and yet I could not make it work.
public class aaa { @InvocableMethod public static List<String> makeACall(List<String> url) { // Execute a call to a Web Service HttpRequest req = new HttpRequest(); req.setEndpoint('www.example.com'); req.setMethod('GET'); HttpResponse response = new Http().send(req); return null; } }
This code should be triggered through process builder workflow each time a new Case is opened but I get error:
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
Every time I try to open a case.
Help is SUPER appreciated!
Please check this link to understand the problem :
https://help.salesforce.com/articleView?id=000003701&type=1 (http://https://help.salesforce.com/articleView?id=000003701&type=1)
In a transaction you cannot perfom a DML operation (update, insert, etc) and do a callout. So if your trying to invoke your method within a process builder, as example, and your process builder is perfoming a DML you will have to create a future method with your call out and then invoke in your maleACall method.
It would be something like this:
The only setback is that future methods are asynchronous so you would have to find another way to return your URL. The other way around is doing it in different moments.
To know more about future annotation:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_future.htm (http://https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_future.htm)
Hope to have helped!
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
All Answers
Please check this link to understand the problem :
https://help.salesforce.com/articleView?id=000003701&type=1 (http://https://help.salesforce.com/articleView?id=000003701&type=1)
In a transaction you cannot perfom a DML operation (update, insert, etc) and do a callout. So if your trying to invoke your method within a process builder, as example, and your process builder is perfoming a DML you will have to create a future method with your call out and then invoke in your maleACall method.
It would be something like this:
The only setback is that future methods are asynchronous so you would have to find another way to return your URL. The other way around is doing it in different moments.
To know more about future annotation:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_future.htm (http://https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_future.htm)
Hope to have helped!
Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Thanks!
After I did changes to code as you suggested, GET request still didnt kick off.
After searching over the internet I found that you need to allow outbound endpoint in Remote Site Settings!!!
After I allowed it and the request was sent my URL was activated (I recieved a call)!