You need to sign in to do that
Don't have an account?
Michael M
Help with callout class
Hello, I have done most of my requirement, but am getting stuck on part of it- as I will explain now. My requirement is as follows:
1. After a Lead is inserted, make an @future POST callout
2. That callout returns a request id. (It looks like this:
{
"request_id": "69a9eb02-fc0c-11ea-afe0-8eadfccf0806",
"return_results" : "True"
}
3. I need to take that request id, and use make another POST callout, with the request id in the body of that callout.
4. This callout will return 1 of 2 possible responses. Either it will return the full JSON response. In that case, all is good and I will then parse the results. However, the way the endpoint works is that the request sits in a queue, so I might need to keep on making this request several times until it returns the JSON. If the queue is not ready for it, then it will return a response like this:
{
"request_id": "69a9eb02-fc0c-11ea-afe0-8eadfccf0806",
"request_status": "waiting_for_processing"
}
This is where I am getting stuck. I keep on getting stuck where it reutrns the "waiting_for_processing" response, but I have not figured out how to tell it to keep making the callout until it returns the full JSOn response. My question is: how can I tell the callout to keep trying until it returns the full JSON? Is there a way to do this with Apex?
Here is my code now:
trigger ReferralCreateContact on Lead (after insert) {
if (trigger.isInsert){
List<string> leadIds = new List<string>();
for (lead ref : trigger.new){
if(System.IsBatch() == false && System.isFuture() == false){
if (ref.Epaces_Checked__c != true && (ref.Emed_Request_Id__c == null || ref.Emed_Request_Id__c == '')){
lead newl = new lead();
newl.Id = ref.id;
leadIds.add(newl.id);
EMedCalloutsExtension.makePostCallout1(leadIds);
}}}}}
public class EMedCalloutsExtension {
static string EmedrequestId;
static boolean firstCalloutSuccess = false;
static string requestStatus;
@future(callout = true)
public static void makePostCallout1(list<id> refIds) {
List <Lead> refs =[Select Id, firstname, lastname, gender__c, patient_dob__c, patient_ssn__c, Epaces_Checked__c, Emed_Request_Id__c from Lead where Id in :refIds];
List<lead> refToUpdate = new List<Lead>();
for (lead ref : refs){
//REQUEST #1
string reqbody;
StaticResource r =[Select Id,Body from StaticResource where Name='EMedCalloutBody' limit 1];
reqBody=r.body.toString();
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'application/json');
req.setMethod('POST');
req.setBody(reqBody);
req.setEndpoint('callout:Emed');
req.setTimeout(2 * 60 * 1000);
system.debug('ENDPOINT: ' + req.getendpoint());
system.debug('BODY: '+ req.getBody());
Http http = new Http();
HttpResponse response = http.send(req);
if (response.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
system.debug(response.getstatuscode());
System.debug(response.getBody());
string requestId = response.getbody().substringAfter('"request_id": ').substringBefore(',');
// ref.Emed_Request_Id__c = requestId;
EmedrequestId = requestId;
//Request 2 TRY 1
string reqbodyResp;
StaticResource r2 =[Select Id,Body from StaticResource where Name='EmedResponseBody' limit 1];
reqbodyResp=r2.body.toString();
reqbodyResp=reqbodyResp.replace('{{requestId}}', EmedrequestId);
HttpRequest req2 = new HttpRequest();
req2.setHeader('Content-Type', 'application/json');
req2.setMethod('POST');
req2.setBody(reqbodyResp);
req2.setEndpoint('callout:Emed_Response');
req2.setTimeout(2 * 60 * 1000);
system.debug('ENDPOINT2: ' + req2.getendpoint());
system.debug('BODY2: '+ req2.getBody());
Http http2 = new Http();
HttpResponse response2 = http2.send(req2);
if (response2.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response2.getStatusCode() + ' ' + response2.getStatus());
} else {
system.debug(response2.getstatuscode());
System.debug(response2.getBody());
string reqStatus = response.getbody().substringAfter('"request_status"').substringBefore('}');
requestStatus = reqStatus;
//Request 2 TRY 2
if (response.getbody().contains('"waiting_for_processing"')){
HttpRequest req22 = new HttpRequest();
req22.setHeader('Content-Type', 'application/json');
req22.setMethod('POST');
req22.setBody(reqbodyResp);
req22.setEndpoint('callout:Emed_Response');
req22.setTimeout(2 * 60 * 1000);
system.debug('ENDPOINT2: ' + req2.getendpoint());
system.debug('BODY2: '+ req2.getBody());
Http http22 = new Http();
HttpResponse response22 = http22.send(req22);
if (response22.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response22.getStatusCode() + ' ' + response22.getStatus());
} else {
system.debug(response2.getstatuscode());
System.debug(response22.getBody());
}}}
1. After a Lead is inserted, make an @future POST callout
2. That callout returns a request id. (It looks like this:
{
"request_id": "69a9eb02-fc0c-11ea-afe0-8eadfccf0806",
"return_results" : "True"
}
3. I need to take that request id, and use make another POST callout, with the request id in the body of that callout.
4. This callout will return 1 of 2 possible responses. Either it will return the full JSON response. In that case, all is good and I will then parse the results. However, the way the endpoint works is that the request sits in a queue, so I might need to keep on making this request several times until it returns the JSON. If the queue is not ready for it, then it will return a response like this:
{
"request_id": "69a9eb02-fc0c-11ea-afe0-8eadfccf0806",
"request_status": "waiting_for_processing"
}
This is where I am getting stuck. I keep on getting stuck where it reutrns the "waiting_for_processing" response, but I have not figured out how to tell it to keep making the callout until it returns the full JSOn response. My question is: how can I tell the callout to keep trying until it returns the full JSON? Is there a way to do this with Apex?
Here is my code now:
trigger ReferralCreateContact on Lead (after insert) {
if (trigger.isInsert){
List<string> leadIds = new List<string>();
for (lead ref : trigger.new){
if(System.IsBatch() == false && System.isFuture() == false){
if (ref.Epaces_Checked__c != true && (ref.Emed_Request_Id__c == null || ref.Emed_Request_Id__c == '')){
lead newl = new lead();
newl.Id = ref.id;
leadIds.add(newl.id);
EMedCalloutsExtension.makePostCallout1(leadIds);
}}}}}
public class EMedCalloutsExtension {
static string EmedrequestId;
static boolean firstCalloutSuccess = false;
static string requestStatus;
@future(callout = true)
public static void makePostCallout1(list<id> refIds) {
List <Lead> refs =[Select Id, firstname, lastname, gender__c, patient_dob__c, patient_ssn__c, Epaces_Checked__c, Emed_Request_Id__c from Lead where Id in :refIds];
List<lead> refToUpdate = new List<Lead>();
for (lead ref : refs){
//REQUEST #1
string reqbody;
StaticResource r =[Select Id,Body from StaticResource where Name='EMedCalloutBody' limit 1];
reqBody=r.body.toString();
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'application/json');
req.setMethod('POST');
req.setBody(reqBody);
req.setEndpoint('callout:Emed');
req.setTimeout(2 * 60 * 1000);
system.debug('ENDPOINT: ' + req.getendpoint());
system.debug('BODY: '+ req.getBody());
Http http = new Http();
HttpResponse response = http.send(req);
if (response.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
system.debug(response.getstatuscode());
System.debug(response.getBody());
string requestId = response.getbody().substringAfter('"request_id": ').substringBefore(',');
// ref.Emed_Request_Id__c = requestId;
EmedrequestId = requestId;
//Request 2 TRY 1
string reqbodyResp;
StaticResource r2 =[Select Id,Body from StaticResource where Name='EmedResponseBody' limit 1];
reqbodyResp=r2.body.toString();
reqbodyResp=reqbodyResp.replace('{{requestId}}', EmedrequestId);
HttpRequest req2 = new HttpRequest();
req2.setHeader('Content-Type', 'application/json');
req2.setMethod('POST');
req2.setBody(reqbodyResp);
req2.setEndpoint('callout:Emed_Response');
req2.setTimeout(2 * 60 * 1000);
system.debug('ENDPOINT2: ' + req2.getendpoint());
system.debug('BODY2: '+ req2.getBody());
Http http2 = new Http();
HttpResponse response2 = http2.send(req2);
if (response2.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response2.getStatusCode() + ' ' + response2.getStatus());
} else {
system.debug(response2.getstatuscode());
System.debug(response2.getBody());
string reqStatus = response.getbody().substringAfter('"request_status"').substringBefore('}');
requestStatus = reqStatus;
//Request 2 TRY 2
if (response.getbody().contains('"waiting_for_processing"')){
HttpRequest req22 = new HttpRequest();
req22.setHeader('Content-Type', 'application/json');
req22.setMethod('POST');
req22.setBody(reqbodyResp);
req22.setEndpoint('callout:Emed_Response');
req22.setTimeout(2 * 60 * 1000);
system.debug('ENDPOINT2: ' + req2.getendpoint());
system.debug('BODY2: '+ req2.getBody());
Http http22 = new Http();
HttpResponse response22 = http22.send(req22);
if (response22.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +
response22.getStatusCode() + ' ' + response22.getStatus());
} else {
system.debug(response2.getstatuscode());
System.debug(response22.getBody());
}}}
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm
All Answers
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm
The logic would be, when receiving 'waiting_for_response' response, call the same queueable callout method again.