Catalog client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2023 08:58 PM
Hi,
Can someone please help me with on change catalog client script?
I have a reference variable ( name_of_the_user) which is referenced to the user table.
When the user is selected and the user does not have an ITIL role, I want a message to appear on the variable " This is not an ITIL user", however, the end user should still be able to select the user and continue with the form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2023 09:17 PM - edited 10-18-2023 12:26 AM
Hi @Thomas99 ,
Here's an example of how you can implement it:
1. **Create a Script Include:**
First, create a Script Include that contains the server-side logic to check if the selected user has the ITIL role. Let's name it `UserUtils`. It should be client callable.
// UserUtils Script Include
var UserUtils = Class.create();
UserUtils.prototype = {
initialize: function() {},
hasITILRole: function(userId) {
var user = new GlideRecord('sys_user');
user.get(userId);
return user.isValidRecord() && user.hasRole('itil');
},
type: 'UserUtils'
};
2. **Create the Client Script with GlideAjax:**
Next, create the client script that uses GlideAjax to call the server-side script include and show the message if the user doesn't have the ITIL role.
// Client Script using GlideAjax
function onChangeClientScript() {
var fieldName = 'name_of_the_user'; // Replace with the actual variable name
var userSysId = g_form.getValue(fieldName);
if (userSysId) {
var ga = new GlideAjax('UserUtils');
ga.addParam('sysparm_name', 'hasITILRole');
ga.addParam('sysparm_user_id', userSysId);
ga.getXMLAnswer(function(answer) {
if (answer === 'false') {
g_form.showFieldMsg(fieldName, 'This is not an ITIL user', 'error');
} else {
g_form.hideFieldMsg(fieldName);
}
});
} else {
g_form.hideFieldMsg(fieldName);
}
}
In this script, `GlideAjax` is used to call the `hasITILRole` method from the `UserUtils` Script Include. If the answer from the server-side call is `'false'`, it displays an error message.
Make sure to replace `'name_of_the_user'` with the actual variable name you are working with.
3. **Usage:**
- Create a new client script of type "OnChange" in the catalog item.
- Paste the client script code into the script field.
- Save the client script.
This approach ensures that the server-side logic is separated into a Script Include, promoting cleaner and more maintainable code. The client script uses GlideAjax to interact with the server-side logic and displays the appropriate message based on the user's ITIL role status.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2023 12:07 AM
Hi @Thomas99 ,
Create a Client Callable Script Include
Let's say UserUtils and insert the below function in script include update the itil role sys_id based on your instance
hasITILRole: function() {
var userId = this.getParameter('sysparm_user_id'); // to fetch the user variable id passed from client script
var user = new GlideRecord('sys_user_has_role');
user.addQuery('user',userId);
user.addQuery('role','282bf1fac6112285017366cb5f867469');//place sys_id of itil role
user.query();
if(user.next())
{
return true;
}
return false;
},
And create a on Change Catalog client on your user variable like below screenshot
and update the below code in your client script and update the variable name in your script which you given for user variable in catalog item
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var fieldName = 'user'; // Replace with the actual variable name
var userSysId = g_form.getValue(fieldName);
if (userSysId) {
var ga = new GlideAjax('CatalogTest');
ga.addParam('sysparm_name', 'hasITILRole');
ga.addParam('sysparm_user_id', userSysId);
ga.getXMLAnswer(function(answer) {
if (answer === 'false') {
g_form.showFieldMsg(fieldName, 'This is not an ITIL user', 'error');
} else {
g_form.hideFieldMsg(fieldName);
}
});
}
}
Let me know if it works for you
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Uday