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.

Control variable visibility based on the selected Location's type (reference to cmn_location)

Dale Bersane
Tera Contributor

I am building a catalog item and I need to control the visibility of the form variable "A" based on the selection of variable "B".  NOTE: variable "B" is a reference to the cmn_location table. The tricky part is that I need to dot walk to a custom column on the location table to get the appropriate condition.

 

Goal: Show variable "A" only if variable "B" has a type of "C".

 

Example:  Show the "Desk Number" variable on the catalog form if the "Location" selected (on the form) has a type designated as "Desk"(custom reference column on the location table that is referencing another table).

 

Any help would be greatly appreciated.  Thank you in advance.

2 ACCEPTED SOLUTIONS

SAI VENKATESH
Kilo Patron
Kilo Patron

Hi @Dale Bersane 

 

You can try the below script:

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    var requestedFor = g_form.getValue('prefered_location');
	alert(requestedFor);

    var ga = new GlideAjax('GetManagerDetailsScript');//script include name
    ga.addParam('sysparm_name', 'getlocation'); // function name used in script include 
    ga.addParam('sysparm_requested', requestedFor);
    ga.getXMLAnswer(callback);
	function callback(response){
		var answer=response;
		alert(answer);
	}
   //Type appropriate comment here, and begin script below
   
}

 

script include:

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

	getlocation: function(){
        var requestedFor = this.getParameter('sysparm_requested');
        var grcountry = new GlideRecord('cmn_location');
        grcountry.addQuery('sys_id', requestedFor);
        grcountry.query();
        if (grcountry.next()) {
            return  grcountry.country;
        }

	},
   



});

 

Thanks and Regards

Sai Venkatesh

View solution in original post

Hi @Dale Bersane 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    var requestedFor = g_form.getValue('prefered_location');
	alert(requestedFor);

    var ga = new GlideAjax('GetManagerDetailsScript');//script include name
    ga.addParam('sysparm_name', 'getlocation'); // function name used in script include 
    ga.addParam('sysparm_requested', requestedFor);
    ga.getXMLAnswer(callback);
	function callback(response){
		var answer=response;
		if(answer=="USA"){
                     g_form.setVisble('prefered_location',true);
	}
   //Type appropriate comment here, and begin script below
   
}

View solution in original post

6 REPLIES 6

Hari Prasath
Tera Guru

Hi @Dale Bersane ,

 

You can achieve this by creating onChange catalog client script for the variable B. Copy paste the below code and do the mentioned changes.

 

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

    //Type appropriate comment here, and begin script below
    var locationValue = g_form.getReference('variable B', LocationInfo);
    function LocationInfo(locationValue) {
        if (locationValue.type == "C") {
            g_form.setReadOnly('Variable A', true); //Control visibility
        } else {
            g_form.setReadOnly('Variable A ', false); //Control visibility
        }
    }

}

 Please let me know if you've any questions.

 

Please mark the answer helpful, if it helped you accordingly.

 

Regards,

Hari

@Hari Prasath Thank you for writing this up, unfortunately this does not work. It is not setting the visibility for anything i select in the location variable. (NOTE: i changed the control visibilitiy lines form setReadOnly to setDisplay).

Community Alums
Not applicable

Hi @Dale Bersane ,

Please use below script 

    var locVal = g_form.getReference('location_variable', getRefLocation);
    function getRefLocation(locVal) {
        if (locVal.type.includes(variable_a)) {
            g_form.setDisplay('Variable_a', true); //Show
        } else {
            g_form.setDisplay('Variable_a', false); //Hide
        }
    }

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

SAI VENKATESH
Kilo Patron
Kilo Patron

Hi @Dale Bersane 

 

You can try the below script:

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    var requestedFor = g_form.getValue('prefered_location');
	alert(requestedFor);

    var ga = new GlideAjax('GetManagerDetailsScript');//script include name
    ga.addParam('sysparm_name', 'getlocation'); // function name used in script include 
    ga.addParam('sysparm_requested', requestedFor);
    ga.getXMLAnswer(callback);
	function callback(response){
		var answer=response;
		alert(answer);
	}
   //Type appropriate comment here, and begin script below
   
}

 

script include:

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

	getlocation: function(){
        var requestedFor = this.getParameter('sysparm_requested');
        var grcountry = new GlideRecord('cmn_location');
        grcountry.addQuery('sys_id', requestedFor);
        grcountry.query();
        if (grcountry.next()) {
            return  grcountry.country;
        }

	},
   



});

 

Thanks and Regards

Sai Venkatesh