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
Oleg MalinovskyOleg 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!

Best Answer chosen by Oleg Malinovsky
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello Oleg,

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: 
 
public class aaa {
   
    @InvocableMethod
    public static List<String> makeACall(List<String> url) {
		aaa.doCalloutFromFuture(url);
    }


	@future(callout=true)
	public static void doCalloutFromFuture(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);
		
		
	}
	
}

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

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello Oleg,

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: 
 
public class aaa {
   
    @InvocableMethod
    public static List<String> makeACall(List<String> url) {
		aaa.doCalloutFromFuture(url);
    }


	@future(callout=true)
	public static void doCalloutFromFuture(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);
		
		
	}
	
}

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.
This was selected as the best answer
Oleg MalinovskyOleg Malinovsky

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)!