How to Wait for a Response Until Retry Count is Exhausted in Rest MessageV2 executeAsync?

Divyesh
Tera Contributor

I have a use case where, in the event of an API failure, I want to retry the request up to a maximum of 3 times. If all 3 retries fail, an appropriate error message should be displayed to the user.

I am using the RestMessageV2 class with the executeAsync method to make the REST call and have configured the ECC queue retry policy to handle retries in case of an API error.

While I can see that ServiceNow is retrying the API calls as expected (visible in the Queue Retry Activities table), the script does not wait for all retries to complete. Instead, it throws an exception immediately after the initial failure.

Ideally, the script should wait until all retries are exhausted before proceeding, as otherwise, the retry mechanism loses its purpose.

Can anyone suggest how to make the script wait until all retries are completed or share alternative approaches to achieve this behavior?

 

var requestBody;
var responseBody;
var status;
var sm;
try{
sm
= new sn_ws.RESTMessageV2("Yahoo Finance", "get");   // Might throw exception if message doesn't exist or not visible due to scope.
sm
.setBasicAuth("admin","admin");
sm
.setStringParameter("symbol", "NOW");
sm
.setStringParameterNoEscape("xml_data","<data>test</data>");
sm.setEndpoint("http://not_valid.com");
response
= sm.executeAsync(); //Might throw exception if http connection timed out or some issue with sending request itself because of encryption/decryption of password.

response
.waitForResponse(60);// In seconds. Wait at most 60 seconds to get response from ECC Queue/Mid Server //Might throw exception timing out waiting for response in ECC queue.

responseBody
= response.haveError() ? response.getErrorMessage() : response.getBody();
status
= response.getStatusCode();
} catch(ex) {
responseBody
= ex.getMessage();
status
= '500';
} finally {
requestBody
= sm ? sm.getRequestBody():null;
}
gs
.log("Request Body: " + requestBody);
gs
.log("Response: " + responseBody);
gs
.log("HTTP Status: " + status);

 

0 REPLIES 0