Help Needed: Notifications Not Triggering from Script Include in ServiceNow

DipinVasu
Tera Contributor

Hi Everyone,
I'm relatively new to ServiceNow and currently facing an issue with triggering notifications from a Script Include. I'm working on a feature where it's crucial to notify users about certain responses, even if they have navigated away from the original page or form. The idea is to ensure that users can refer back to these notifications at their convenience.

 

var AsyncStatusScriptInclude = Class.create();
AsyncStatusScriptInclude.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    callAsyncStatusOutboundCall: function() {
        try {
            var jsonBody = this.getParameter('jsonData');
            var resourceId = this.getParameter('id');
            
            var r = new sn_ws.RESTMessageV2('DEVICE', 'GetDeviceAsync');
            r.setRequestBody(jsonBody);
            r.setStringParameter('resourceId', resourceId);
            
            var response = r.execute();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();

            var result = {
                status: httpStatus,
                body: responseBody
            };
            // Example: Notify user based on the response status
            if (httpStatus === 200) {
                this.addNotification("Device status updated successfully");
            } else {
                this.addNotification("Failed to update device status");
            }

            return JSON.stringify(result);
        } catch (ex) {
            return JSON.stringify({
                status: "error",
                message: ex.message
            });
        }
    },
    
    addNotification: function(message) {
         gs.addInfoMessage(message);
    },
});

 





1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

Example:

Script Include:

var AsyncStatusScriptInclude = Class.create();

AsyncStatusScriptInclude.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	callAsyncStatusOutboundCall: function () {
		try {
			var jsonBody = this.getParameter('jsonData');
			var resourceId = this.getParameter('id');
			var result = {};

			var r = new sn_ws.RESTMessageV2('DEVICE', 'GetDeviceAsync');
			r.setRequestBody(jsonBody);
			r.setStringParameter('resourceId', resourceId);

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

			result.status = httpStatus;
			result.body = responseBody;

			// Example: Notify user based on the response status
			if (httpStatus === 200) {
				result.infoMessage = "Device status updated successfully";
			}
			else {
				result.errorMessage = "Failed to update device status";
			}

			return JSON.stringify(result);
		}
		catch (ex) {
			return JSON.stringify({
				status: "error",
				errorMessage: ex.message
			});
		}
	},

});

Client Script:

var gx = new GlideAjax('global.AsyncStatusScriptInclude');

gx.addParam('sysparm_name', 'callAsyncStatusOutboundCall');
gx.addParam('jsonData', ...);
gx.addParam('id', ...);

gx.getXMLAnswer(listener);

function listener (payload) {
	var data = JSON.parse(payload);

	if (data.infoMessage)
		g_form.addInfoMessage(data.infoMessage);

	if (data.errorMessage)
		g_form.addErrorMessage(data.errorMessage);

	// ...
}

 

I believe you should also name your parameters so that the sysparm_ prefix is retained.

If I remember correctly parameters not starting with that prefix will actually not be available in the Script Include.

So sysparm_jsonData and sysparm_id not jsonData and id.

View solution in original post

2 REPLIES 2

-O-
Kilo Patron
Kilo Patron

That is not a notification, that is an info message.

In SN notifications are a well defined notion, that of e-mails sent out by the system.

 

Information (or error messages for that matter) created using gs/GlideSystem are displayed when a form or list is displayed.

GlideAjax calls by definition are executed after a form or a list has been displayed.

If you want to display some info or error message as a result of a GlideAjax call, you need to make the message part of the payload returned by the GlideAjax call and show it using the appropriate client side API e.g. g_form.addInfoMessage().

-O-
Kilo Patron
Kilo Patron

Example:

Script Include:

var AsyncStatusScriptInclude = Class.create();

AsyncStatusScriptInclude.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	callAsyncStatusOutboundCall: function () {
		try {
			var jsonBody = this.getParameter('jsonData');
			var resourceId = this.getParameter('id');
			var result = {};

			var r = new sn_ws.RESTMessageV2('DEVICE', 'GetDeviceAsync');
			r.setRequestBody(jsonBody);
			r.setStringParameter('resourceId', resourceId);

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

			result.status = httpStatus;
			result.body = responseBody;

			// Example: Notify user based on the response status
			if (httpStatus === 200) {
				result.infoMessage = "Device status updated successfully";
			}
			else {
				result.errorMessage = "Failed to update device status";
			}

			return JSON.stringify(result);
		}
		catch (ex) {
			return JSON.stringify({
				status: "error",
				errorMessage: ex.message
			});
		}
	},

});

Client Script:

var gx = new GlideAjax('global.AsyncStatusScriptInclude');

gx.addParam('sysparm_name', 'callAsyncStatusOutboundCall');
gx.addParam('jsonData', ...);
gx.addParam('id', ...);

gx.getXMLAnswer(listener);

function listener (payload) {
	var data = JSON.parse(payload);

	if (data.infoMessage)
		g_form.addInfoMessage(data.infoMessage);

	if (data.errorMessage)
		g_form.addErrorMessage(data.errorMessage);

	// ...
}

 

I believe you should also name your parameters so that the sysparm_ prefix is retained.

If I remember correctly parameters not starting with that prefix will actually not be available in the Script Include.

So sysparm_jsonData and sysparm_id not jsonData and id.