How to implement retry policy outside flow designer?

Suggy
Giga Sage

How to implement retry policy concept outside flow designer?

Ex - I want to retry API call 3 times. If still not successful, then create an incident.

 

PS - I know this capability is there in Flow designer but customer is having IH Starter package which has limited license count for IH transactions. Hence we are looking for solution outside the FLOW DESIGNER.

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Hi @Suggy 

 

ServiceNow offers the same concept OOTB with the ECC queue. And before you ask: No you don't need a MID server for that!

 

For you it means only a small change in your code and an additional configuration for retry policies. Please refer to the following article for more information: https://www.servicenow.com/community/itom-blog/ecc-failure-retry-async-rest-web-service-example/ba-p... 

 

Maik

View solution in original post

8 REPLIES 8

YaswanthKurre
Giga Guru

Hi Suggy,

 

To implement a retry policy outside of Flow Designer in ServiceNow, you can use Server-Side Scripts (Script Includes, Business Rules, or Scheduled Jobs) to handle API retries.

 

Try this function inside your script :

executeWithRetry: function() {
        var response = null;
        var maxRetries= 5;
        for (var i = 0; i < maxRetries; i++) {
            try {
                var restMessage = new sn_ws.RESTMessageV2(restMessageName, httpMethod);
                restMessage.setRequestBody(parameters);
                response = restMessage.execute();
     
                // Check if the API call was successful (status code 2xx)
                if (response.getStatusCode== 200) {
                    return response; // Return successful response
                } else {
                    gs.info("Retry " + (i + 1) + ": API call failed with status code " + response.getStatusCode());
                }
            } catch (e) {
                gs.error("Retry " + (i + 1) + ": Error during API call: " + e.message);
            }
            
            // Wait before retrying (e.g., 5 seconds)
            if (i < maxRetries - 1) {
                gs.sleep(5000); // Sleep for 5 seconds
            }
        }
        return null; // Return null to indicate failure
    }

 

tip: Adjust gs.sleep(5000) to change the delay between retries (like 10 seconds = gs.sleep(10000)), make sure your are not doing too much delays.

 

Please mark this as helpful and correct if this answers your question.

Thanks,

Yaswanth

@YaswanthKurre Thanks for your inputs, but gs.sleep is not allowed in our production instance.

Maik Skoddow
Tera Patron
Tera Patron

Hi @Suggy 

 

ServiceNow offers the same concept OOTB with the ECC queue. And before you ask: No you don't need a MID server for that!

 

For you it means only a small change in your code and an additional configuration for retry policies. Please refer to the following article for more information: https://www.servicenow.com/community/itom-blog/ecc-failure-retry-async-rest-web-service-example/ba-p... 

 

Maik

Thanks Mark. Actually I thought it was deprecated when I couldnt find in my PDI. But its available in customer instances.