Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

service catalog

rognomolta
Tera Contributor

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

rognomolta_0-1726835114315.png

 

 

rognomolta_1-1726835114293.png

 

 

1 REPLY 1

Community Alums
Not applicable

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