Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Need to update scripted if statement from '!=' to does not contain

LRhodes
Tera Guru

Hi all,

I have a script I'm using to hide a variable field on a catalog item. Currently I have the below client script which triggers the following script include which essentially looks are the current users u_partner_role field and if it is not 'Principal' then it hides the field from them.

 

I need to update it so that it does not contain 'Principal' as we can have data such as 'Principal Director' etc. I know how to do this in a query but not so sure with an IF statement. Any assistance appreciated.

 

Client Script

function onLoad() {
   //Type appropriate comment here, and begin script below
	
	var ga = new GlideAjax('userUtils');
	ga.addParam('sysparm_name','getPartnerRole');
	ga.getXML(checkPartnerRole);
	
	function checkPartnerRole(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");
		//If the users Partner Role contains 'Principal', hide the Principle Partner field
		if(answer != 'Principal')
			g_form.setDisplay('principal_partner_to_join',false);
			g_form.setMandatory('principal_partner_to_join',false);
	}
	
}

Script Include

var userUtils = Class.create();
userUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getPartnerRole: function() {
		
		var currentUser = new GlideRecord('sys_user');
		currentUser.get(gs.getUserID());
		
		if (currentUser.u_partner_role){
			return currentUser.getDisplayValue('u_partner_role');
		}
		else {
			return "No department";
		}
	},

    type: 'userUtils'
});

 

1 ACCEPTED SOLUTION

Elijah Aromola
Mega Sage

You need to see if the string contains that string. 

function onLoad() {
	//Type appropriate comment here, and begin script below

	var ga = new GlideAjax('userUtils');
	ga.addParam('sysparm_name', 'getPartnerRole');
	ga.getXML(checkPartnerRole);

	function checkPartnerRole(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		//If the users Partner Role contains 'Principal', hide the Principle Partner field
		if (answer.toLowerCase().indexOf('principal') > -1) {
			g_form.setDisplay('principal_partner_to_join', false);
			g_form.setMandatory('principal_partner_to_join', false);
		}
	}
}

 indexOf() returns the first index that the string can be found. If it's -1, it wasn't found. If it's 0 or greater, it has been found.

View solution in original post

2 REPLIES 2

Elijah Aromola
Mega Sage

You need to see if the string contains that string. 

function onLoad() {
	//Type appropriate comment here, and begin script below

	var ga = new GlideAjax('userUtils');
	ga.addParam('sysparm_name', 'getPartnerRole');
	ga.getXML(checkPartnerRole);

	function checkPartnerRole(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		//If the users Partner Role contains 'Principal', hide the Principle Partner field
		if (answer.toLowerCase().indexOf('principal') > -1) {
			g_form.setDisplay('principal_partner_to_join', false);
			g_form.setMandatory('principal_partner_to_join', false);
		}
	}
}

 indexOf() returns the first index that the string can be found. If it's -1, it wasn't found. If it's 0 or greater, it has been found.

Thank you Elijah - that's brilliant, and worked a treat. Thank you for the explanation also, useful for future usage.