How to call script include from client script with example
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 06:58 AM - edited 12-12-2023 10:06 PM
Hello Community,
Hope you all are well!
Here is the example of the onLoad client script and client callable script include, to check if the current user belongs to any of the IOT groups and display an appropriate info message in the Incident form.
Logic: We have to check whether the current user's group name starts with "IOT" or not.
A. Client Script:
function onLoad() {
var ga = new GlideAjax('global.SNOW_CheckGroupScript');
ga.addParam('sysparm_name', 'checkUserInIOTGroup');
ga.getXML(function(response) {
var result = response.responseXML.documentElement.getAttribute('answer');
g_form.addInfoMessage(result);
if (result === 'true') {
g_form.addInfoMessage('User belongs to one of the IOT group.');
} else {
g_form.addInfoMessage('User does not belong to one of the IOT group.');
}
});
}
Here's a simplified explanation of the code:
- The onLoad function is triggered when a form loads in ServiceNow.
- It creates a GlideAjax object, specifying the target Script Include named "SNOW_CheckGroupScript" for the server-side call.
- It adds a parameter sysparm_name with the value checkUserInIOTGroup to indicate the function to be called in the Script Include.
- It makes an asynchronous call to the server-side function using ga.getXML.
- When the response is received from the server-side function, it extracts the result from the XML using response.responseXML.documentElement.getAttribute('answer').
- It displays an info message based on whether the result is 'true' or 'false':
- If the result is 'true', it adds an info message "User belongs to IOT group."
- If the result is 'false', it adds an info message "User does not belong to IOT group."
B. Script Include:
var SNOW_CheckGroupScript = Class.create();
SNOW_CheckGroupScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Function in the script include to be called from the client script
checkUserInIOTGroup: function() {
var currentUserID = gs.getUser().getID();
var userInIOTGroup = 'false';
// Query user record to get the departments
var grUser = new GlideRecord('sys_user_grmember');
grUser.addQuery('user', currentUserID);
grUser.query();
while (grUser.next()) {
var groupName = grUser.group.getDisplayValue();
//gs.info(currentUserID + groupName);
// Check if the user's department matches any IOT department
if (groupName.startsWith("IOT")) {
userInIOTGroup = 'true';
} else
var userInIOTGroup = 'false';
}
return userInIOTGroup;
},
type: 'SNOW_CheckGroupScript'
});
Here's a simplified explanation of the code:
- The code defines a Script Include named "SNOW_CheckGroupScript" in ServiceNow.
- Inside the Script Include, there is a function named checkUserInIOTGroup, which will be called from a client script.
- The function starts by getting the ID of the currently logged-in user using gs.getUser().getID().
- It initializes a variable userInIOTGroup with the value 'false'. This variable will be used to determine if the user belongs to the "IOT" group.
- The function queries the "sys_user_grmember" table to retrieve the user's group memberships.
- For each group membership found, it checks the group's name (groupName) to see if it starts with the letters "IOT".
- If any group membership has a name starting with "IOT", it sets userInIOTGroup to 'true', indicating that the user belongs to the "IOT" group.
- If no group membership is found with a name starting with "IOT", userInIOTGroup remains 'false', indicating that the user does not belong to the "IOT" group.
- Finally, the function returns the value of userInIOTGroup, which will be 'true' if the user belongs to the "IOT" group or 'false' otherwise.
- 7,760 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 10:45 PM
This helps me a lot Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 09:24 AM
Glad to know that!