An apex method that returns a continuation object can’t perform DML. The solution given by the Salesforce developer documentation is the “perform DML operations in the Apex callback method”. https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_continuations_limits.htm
I tried to do DML operations in the Apex callback method in new Continuation approach. But it was not working. Continuation was failed. It gave below error. What should I do for this?
public with sharing class newContinuation1 {
private static final String LONG_RUNNING_SERVICE_URL =
'http://dummy.restapiexample.com/api/v1/employees';
public static List<String> requestLabel;
public static String result;
@AuraEnabled(continuation=true cacheable=false)
public static Object getstartRequest() {
Continuation con = new Continuation(40);
con.continuationMethod='processResponse';
con.state='SUCCESS';
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(LONG_RUNNING_SERVICE_URL);
con.addHttpRequest(req);
return con;
}
@AuraEnabled(cacheable=false)
public static Object processResponse(List<String> labels, Object state) {
HttpResponse response = Continuation.getResponse(labels[0]);
String result = response.getBody();
Account acct1 = new Account(Name='samushika333');
try
{
insert acct1;
return result;
} catch (DmlException e) {
System.debug('Exception'+e);
return result;
}
}
}