Error Handling in ServiceNow for a OUTBOUND Rest message

Naveen87
Tera Guru

Hello Developers,

 

We are doing e-bonding via OUTBOUND Rest Message.

We are X company & OUTBOINDING to Y company.

There is a BR at place which will trigger the OUTBOUND with necessary Data from X to Y & a ticket will be created on Y company end.

Ticket number will be sent back as Acknowledgement & stored on Correlation ID on X company.

I want to set-up Error Handling, Just in case If ticket is not created on Y company, Network failure etc., could be reasons.

Where I could re-trigger it & new ticket is created on Y company.

 

Not sure, If this is something which can be achieved.

 

Please suggest. 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Naveen87 

in the same BR you can handle the retry mechanism.

Something like if you don't get response then try for 2-3 times with a gap of 1min or so and if no response after all the try then create a email or ticket in instance X

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar ,

Can you please help me with line of codes to be written on BR.?

(function executeRule(current, previous /*null when async*/ ) {


    try {
        var r = new sn_ws.RESTMessageV2('global.EUW Ebonding', 'POST Incident');
      
        r.setStringParameterNoEscape('u_customer_ticket_id', current.number);
        r.setStringParameterNoEscape('u_shr_priority', current.priority);
        r.setStringParameterNoEscape('u_shr_assignment_group', current.getDisplayValue('assignment_group'));
        r.setStringParameterNoEscape('u_pass_to_servicenow', 'Yes');
        r.setStringParameterNoEscape('u_ticket_type', 'Incident');
        r.setStringParameterNoEscape('u_transaction_type', '2');

        var response = r.execute();
        var responseBody = response.getBody();
        gs.info("responseBody " + responseBody);

        var jsonObj = JSON.parse(responseBody)
        var ticketid = jsonObj.result.u_partner_ticket_id;
        current.correlation_id = 'euwbond:' + ticketid;

        var correlation_id = current.correlation_id;
        if (!correlation_id) {
            // Field is empty, proceed with your desired action
            gs.addInfoMessage("Correlation ID is empty");
        } else if (correlation_id.includes("euwbond:INC")) {
            // Field contains "EUW bond", do nothing
            gs.addInfoMessage("Correlation ID contains 'euwbond'");
        } else {
            // Field contains something else, show error message
            gs.addErrorMessage("Correlation ID already set with 'euwbond'");
        }

        var httpStatus = response.getStatusCode();
    } catch (ex) {
        var message = ex.message;



    }

})(current, previous);

@Ankur Bawiskar ,

 

Can you suggest the line of codes for this error handling.

I have shared my current BR configurations.

Naveen87
Tera Guru

I created a Scheduled Job. Running Periodically.

Please let me know if below script if fine or need modifications.

What else would be required apart from Scheduled Job?

Naveen87_0-1711605923967.png

 

// Find incidents where correlation ID is not received and retry flag is set, //create a custom field called retry flag
var grIncident = new GlideRecord('incident');
grIncident.addQuery('correlation_id', '');
grIncident.addQuery('retry_flag', true); // Assuming you have a field to indicate retry is needed
grIncident.query();

while (grIncident.next()) {
   // Check if the last update time is more than 1 or 2 minutes ago
   var lastUpdated = new GlideDateTime(grIncident.sys_updated_on); // Assuming sys_updated_on field stores the last update time
   var currentTime = new GlideDateTime();
   var minutesSinceLastUpdate = GlideDateTime.subtract(currentTime, lastUpdated).getNumericValue() / (1000 * 60);
   if (minutesSinceLastUpdate >= 1 && minutesSinceLastUpdate <= 2) {
	
       // Trigger the outbound REST message again
       var restMessage = new sn_ws.RESTMessageV2('global.EUW Ebonding', 'POST Incident');
       restMessage.setStringParameter('incident_number', grIncident.number); // Pass incident number or any other necessary parameters
       var response = restMessage.execute();
       // Check response and update incident accordingly
       if (response.getStatusCode() === 200) {
           // Update incident with correlation ID from response
           var responseBody = response.getBody(); // Assuming response body contains correlation ID
           grIncident.correlation_id = responseBody;
           grIncident.retry_flag = false; // Reset retry flag
           grIncident.update();
       } else {
           gs.error('Failed to trigger REST message for incident: ' + grIncident.number);
           // Handle error scenario - you can log error, update retry count, etc.
       }
   } else {
       // Update incident with appropriate status or log that retry time window has not passed
       gs.info('Retry time window has not passed for incident: ' + grIncident.number);
   }
}