service catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 02:33 AM - edited 09-20-2024 05:15 AM
Hi all,
Below is my requriment i have already create a function in script include and catalog client script but it is not working as expected below i share script and screenshots
Update the oneCatalogUtils Script Include to add a new function verifySMSOptIn that checks if a user has opted into SMS messages by looking at the u_sms_opt_in field in their profile.
Create a Catalog Client Script on the "oneReset" catalog item that calls this function to show or hide the "Send Code To" field based on the SMS opt-in status.
Script Include :
var onePlusCatalogUtils = Class.create();
onePlusCatalogUtils.prototype = Object.extendsObject(AbstractAjaxProcessor,{
verifySMSOptIn: function(userId) {
if (!userId) {
return false; // No user ID provided, return false as default
}
// Query the user record
var userGR = new GlideRecord('sys_user');
if (userGR.get(userId)) {
// Check the value of the 'u_sms_opt_in' field
return userGR.u_sms_opt_in == true; // Assuming u_sms_opt_in is a boolean field
}
return false; // Return false if user not found or any other issue
},
type: 'onePlusCatalogUtils'
};
client script:
function onLoad() {
var userId = g_user.userID;
var ga = new GlideAjax('onePlusCatalogUtils');
ga.addParam('sysparm_name', 'verifySMSOptIn');
ga.addParam('sysparm_userId', userId);
ga.getXMLAnswer(function(response) {
var smsOptIn = response;
if (smsOptIn == 'true') {
g_form.setDisplay('sendcodeto', true);
} else {
g_form.setDisplay('sendcodeto', false);
}
});
}
screenshot for catalog item

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2024 07:01 AM
Hey @rognomolta
It looks like you are missing the line where you get the userId. Since this Script Include is client callable, you have to get the parameter from a client call like so:
this.getParameter('sysparm_userId')
Add that to the beginning of your method and you should be good to go.
~Nick