- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2024 07:07 AM - edited ‎06-28-2024 07:09 AM
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);
},
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-30-2024 10:11 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-30-2024 09:58 AM
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().
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-30-2024 10:11 AM
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.