servicenow 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-1726834738836.png

 

rognomolta_1-1726834937546.png

 

 

1 ACCEPTED SOLUTION

sujitha16
Kilo Patron

@rognomolta  Please find the steps below for your use case.

 

Note: I have used "VIP" field similar to your "SMS Opt-In"

 

Script Include : onePlusCatalogUtils

Client callable : true

Accessible from : All application scopes

 

var onePlusCatalogUtils = Class.create();
onePlusCatalogUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    verifySMSOptIn: function() {
		var userId = this.getParameter('sysparm_userId');
		gs.log("User Id:" +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 'VIP' field
			var optedValue = userGR.getValue('vip');
			return optedValue; 
        }
		
    },
    type: 'onePlusCatalogUtils'
});

 

Catalog client script : 

Type : onLoad

function onLoad() {
   //Type appropriate comment here, and begin script below
var userId = g_user.userID;
alert("Client User ID" +userId);

var ga = new GlideAjax('onePlusCatalogUtils');
ga.addParam('sysparm_name', 'verifySMSOptIn');
ga.addParam('sysparm_userId', userId);
ga.getXML(sendthecode);
}

function sendthecode(response){
var smsOptIn = response.responseXML.documentElement.getAttribute("answer");
alert("Response :" +smsOptIn);
if (smsOptIn == '1') { //True
g_form.setDisplay('send_the_code_to', true); 
} else {
g_form.setDisplay('send_the_code_to', false); 
}
}

 

Results : 

 

1. If the user profile VIP is true, then display the variable "Send the Code To"

SujathaVM_0-1726940120080.png

2. If the user profile VIP is false, then hide the variable "Send the Code To"

 

SujathaVM_1-1726940198118.png

Note: Incase. if the "Response" alert return null, then check the role provided to script include on creation to avoid restriction. Impersonate different users and have it validated. 

 

 

View solution in original post

1 REPLY 1

sujitha16
Kilo Patron

@rognomolta  Please find the steps below for your use case.

 

Note: I have used "VIP" field similar to your "SMS Opt-In"

 

Script Include : onePlusCatalogUtils

Client callable : true

Accessible from : All application scopes

 

var onePlusCatalogUtils = Class.create();
onePlusCatalogUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    verifySMSOptIn: function() {
		var userId = this.getParameter('sysparm_userId');
		gs.log("User Id:" +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 'VIP' field
			var optedValue = userGR.getValue('vip');
			return optedValue; 
        }
		
    },
    type: 'onePlusCatalogUtils'
});

 

Catalog client script : 

Type : onLoad

function onLoad() {
   //Type appropriate comment here, and begin script below
var userId = g_user.userID;
alert("Client User ID" +userId);

var ga = new GlideAjax('onePlusCatalogUtils');
ga.addParam('sysparm_name', 'verifySMSOptIn');
ga.addParam('sysparm_userId', userId);
ga.getXML(sendthecode);
}

function sendthecode(response){
var smsOptIn = response.responseXML.documentElement.getAttribute("answer");
alert("Response :" +smsOptIn);
if (smsOptIn == '1') { //True
g_form.setDisplay('send_the_code_to', true); 
} else {
g_form.setDisplay('send_the_code_to', false); 
}
}

 

Results : 

 

1. If the user profile VIP is true, then display the variable "Send the Code To"

SujathaVM_0-1726940120080.png

2. If the user profile VIP is false, then hide the variable "Send the Code To"

 

SujathaVM_1-1726940198118.png

Note: Incase. if the "Response" alert return null, then check the role provided to script include on creation to avoid restriction. Impersonate different users and have it validated.