Retry REST Message in case of failure for GET call (outbound)

GD11
Tera Expert

using below code I am triggering a REST message through Scheduled job, I want to retry the same REST call if that if the response is other than 200, I want to do it retry 5 times or till the time I get 200(whichever is earlier) wit the interval of 10 seconds, ifretry is successful then break the retry and come out the if/while an run next line of code, I am not sure whatever I have written is correct, please advise.

 

var retrycount = 0 ;

var r = new sn_ws.RESTMessageV2(REST message, REST Method));
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
 while(httpStatus != '200' && retrycount < gs.getProperty('integration.retry_count')){  // property to store max retries

retrycount++;

 gs.sleep(gs.getProperty('integration.retry_interval')); .// property that has interval time defined in MS
var response = r.execute();

if(httpStatus == '200'){

break;

}

}

 

@Ankur Bawiskar @Maik Skoddow @Mike_R @Saurav11 @Gunjan Kiratkar @AnubhavRitolia @Mohith Devatte @Murthy Ch @Chuck Tomasi 

5 REPLIES 5

Mike_R
Kilo Patron
Kilo Patron

If using flow designer/integration hub is an option, then I would suggest using that instead. There is a built in retry policy that you configure

https://docs.servicenow.com/bundle/tokyo-servicenow-platform/page/administer/flow-designer/concept/r...

Hi @Mike_R  I am not using flow designer that's the issue, my code is already ready for through scheduled so jsut wanted to know if there is any suggestion in the above code snippet that I tried, I think I am at right place but not sure if that is correct. please help.

You'll need to code your own error handler then. I don't have recommendations for that at this time.

I usually just do this through flow designer (or even workflow editor) because it's a lot simpler to just loop back to the previous activity. Since you're not using/can't use flow designer, here's an example in wf editor

Mike_R_0-1669925626908.png

 

Hi @Mike_R I will try to accommodate this in the workflow instead of scheduled job or calling triggering workflow through scheduled job, till that time can you please advise me on below code whether it will trigger the same REST outbound call in case of response other than 200/201/202

 

var retrycount = 0 ;

var r = new sn_ws.RESTMessageV2(REST message, REST Method));
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
 while(httpStatus.startsWIth('20') == false  && retrycount < gs.getProperty('integration.retry_count')){  // property to store max retries

retrycount++;

 gs.sleep(gs.getProperty('integration.retry_interval')); .// property that has interval time defined

var response = r.execute();

var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

if((httpStatus.startsWIth('20') == false ){

retrycount++;

} else {

break;

}

}

thanks in advance! 🙂