To achieve a retry of the REST call in ServiceNow after 15 minutes if the httpStatus is not 201, you can't simply loop or pause the script in a Business Rule because server-side script execution in ServiceNow must complete quickly.
Recommended approach: Use a Scheduled Job (Scheduled Script Execution) or a Scheduled Flow (Flow Designer) combined with a flag on the record to trigger retries.
Option 1: Using Scheduled Script Execution (Scheduled Job)
-
Modify your business rule or script to set a retry flag and the time of last attempt if HTTP status != 201.
-
Create a Scheduled Script Execution that runs every few minutes (e.g., every 5 minutes).
-
The scheduled job will query all incidents (or relevant records) with the retry flag set and retry the REST call if 15 minutes have elapsed since the last attempt.
Step-by-step example:
1) Add fields to your Incident table (or the relevant table):
u_last_rest_attempt (Date/Time): stores the time when we last tried the REST call.
u_rest_status (String): stores the last HTTP status.
u_rest_retry (Boolean): flag indicating retry pending.
2) Business Rule (or Script Include you already have) - Run on Insert or Update of Incident:
Modify your current script:
(function executeRule(current, previous /*null when async*/) {
try {
var r = new sn_ws.RESTMessageV2('Send Incident to dev203567', 'POST');
r.setStringParameter('short_description', current.short_description.toString());
r.setStringParameter('description', current.description.toString());
r.setStringParameter('category', current.category);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if (httpStatus != 201) {
// Set retry flags and timestamp
current.u_rest_status = httpStatus.toString();
current.u_rest_retry = true;
current.u_last_rest_attempt = new GlideDateTime();
current.update();
} else {
// Success - clear flags
current.u_rest_status = '201';
current.u_rest_retry = false;
current.u_last_rest_attempt = null;
current.update();
}
} catch (ex) {
gs.error('REST call failed: ' + ex.message);
// Optionally mark retry true to attempt again
current.u_rest_retry = true;
current.u_last_rest_attempt = new GlideDateTime();
current.update();
}
})(current, previous);
3) Scheduled Script Execution (Scheduled Job) - runs say every 5 minutes:
(function() {
var gr = new GlideRecord('incident'); // replace with your table
gr.addQuery('u_rest_retry', true);
gr.query();
while (gr.next()) {
var lastAttempt = new GlideDateTime(gr.u_last_rest_attempt);
var now = new GlideDateTime();
var diff = gs.dateDiff('minute', lastAttempt, now);
if (diff >= 15) { // it's been 15 or more minutes since last attempt
try {
var r = new sn_ws.RESTMessageV2('Send Incident to dev203567', 'POST');
r.setStringParameter('short_description', gr.short_description.toString());
r.setStringParameter('description', gr.description.toString());
r.setStringParameter('category', gr.category.toString());
var response = r.execute();
var httpStatus = response.getStatusCode();
if (httpStatus == 201) {
gr.u_rest_status = '201';
gr.u_rest_retry = false;
gr.u_last_rest_attempt = null;
gr.update();
} else {
gr.u_rest_status = httpStatus.toString();
gr.u_last_rest_attempt = new GlideDateTime();
gr.update();
}
} catch (ex) {
gs.error('Scheduled REST call retry failed: ' + ex.message);
gr.u_last_rest_attempt = new GlideDateTime();
gr.update();
}
}
}
})();
Option 2: Using Flow Designer with a Wait step
Alternatively, you can build a Flow that runs the REST call, and in case of failure, wait for 15 minutes and retry. This is no-code/low-code approach and fits nicely with ServiceNow's modern automation.
Summary
- You cannot simply delay or repeat in a Business Rule directly.
- Use extra fields for retry metadata.
- Use a Scheduled Script that queries records needing retry and retries REST calls.
- Or use Flow Designer with waiting and retry steps.
If you want, I can help you create that Flow Designer flow too.