I'm trying to call showData()
after makeCallout()
finish executing.
makeCallout()
has insert
data; so I'm getting this error:
Content cannot be displayed: You have uncommitted work pending. Please commit or rollback before calling out.
So I created a new method that has @future
anotation. since according to Salesforce
Each future method is queued and executes when system resources become available.
What I'm trying to achieve is running showData()
after makeCallout ()
finishes, I trying the below but the variables in showData()
are null since both methods are executed at the same time.
public class myClass {
.....
private static integer a = 0 ;
public void makeCallout {
Http http = new Http();
HttpRequest request = new HttpRequest();
Transient string cookie = 'X';
request.setEndpoint(url);
request.setMethod('POST');
request.setHeader('Authorization', authheader);
request.setHeader('Cookie', cookie);
if (response.getStatusCode() == 200) {
while (parser.nextToken() != null){
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
if (parser.getCurrentToken() == JSONToken.START_OBJECT){
try {
a = parser.getText();
}
}
}
}
insert a;
showData();
}
@future
public static void showData(){
//make another HTTP call
insert newData
System.Debug('a = ' + a); // a=0
}