How to hide/display field based on the user location in Record producer?

Deepika61
Tera Contributor

Hi All,

I have a requirement Actually in record producer,  i am using a Variable set , in that , three fields, " Selected user(Reference field),  type of leave and time of period ", So here the field 'leave type' was only visible , when the user(Selected user field )with location( particularly state  is one of WA or SA)?

 

Please help me to solve this

 

Thanks

Deepika

1 ACCEPTED SOLUTION

@Deepika61 

your script include is not client callable

Script include

var Locationbased = Class.create();
Locationbased.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getLocation: function() {
		var userID = this.getParameter('sysparm_user'); 
		var returnID = '';
		var user = new GlideRecord('sys_user');
		if(user.get(userID)){
			return user.location.state; 
		}
	},

	type: 'Locationbased'
});

AnkurBawiskar_0-1696341278051.png

Client script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	if(oldValue != newValue){
		//Type appropriate comment here, and begin script below
		var ga = new GlideAjax('Locationbased');
		ga.addParam('sysparm_name', 'getLocation'); 
		ga.addParam('sysparm_user', newValue); 
		ga.getXMLAnswer(function(answer){
			alert(answer);
			answer = answer.toString();
			if (answer == 'WA' || answer == 'SA')
				g_form.setDisplay('type_of _period', true);
			else {
				g_form.setDisplay('type_of _period', false);
			}
		});
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

@Deepika61 

your script include is not client callable

Script include

var Locationbased = Class.create();
Locationbased.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getLocation: function() {
		var userID = this.getParameter('sysparm_user'); 
		var returnID = '';
		var user = new GlideRecord('sys_user');
		if(user.get(userID)){
			return user.location.state; 
		}
	},

	type: 'Locationbased'
});

AnkurBawiskar_0-1696341278051.png

Client script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	if(oldValue != newValue){
		//Type appropriate comment here, and begin script below
		var ga = new GlideAjax('Locationbased');
		ga.addParam('sysparm_name', 'getLocation'); 
		ga.addParam('sysparm_user', newValue); 
		ga.getXMLAnswer(function(answer){
			alert(answer);
			answer = answer.toString();
			if (answer == 'WA' || answer == 'SA')
				g_form.setDisplay('type_of _period', true);
			else {
				g_form.setDisplay('type_of _period', false);
			}
		});
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks @Ankur Bawiskar , its working for me

Brad Bowman
Kilo Patron
Kilo Patron

Use an onChange Catalog Client Script when Selected user changes, sending the newValue to a Script Include with a GlideAjax call.

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

 

The Script Include needs to query the user table for the selected user, then you can dot-walk to the State on the Location and return this value to the client, where it is evaluated and controls the visibility of the leave type variable.  If you also want leave type to be hidden before a Selected user is selected, create a Catalog UI Policy or onLoad Catalog Client Script.  If by 'variable set' you mean a multi-row, as opposed to single row, the variable will be hidden in the Add Row dialog window only, but shown on the display of the MRVS contents on the request form, RITM, ...