The CreatorCon Call for Content is officially open! Get started here.

Set visible or set display false of some variables on SC

babarat1
Tera Contributor

Hi All,

I have the below "On change" client script.

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

		return;
	}	

	var prod = g_form.getReference('product', myFunc);
	//alert(JSON.stringify(prod));
	//alert(prod.u_application_name);
	function myFunc(prod){
		var ga = new GlideAjax('abcCheckDevProdCIenv');
		ga.addParam("sysparm_name", "getCI");
		ga.addParam("sysparm_app_name", prod.u_application_name);
		ga.addParam("sysparm_sysid", newValue);
		//alert(newValue);
		//alert(newValue.getDisplayValue());
		ga.getXML(getResponse);

		function getResponse(response) {
			var res = response.responseXML.documentElement.getAttribute("answer");
			alert(res);		
			if(newValue != ''){
				//var res1 = res.toString();
				if(res.includes('Development')){	
					g_form.setVisible('column1',true);
					g_form.setVisible('column2',true);
					g_form.setVisible('column3',true);
				}
				if(res.includes('Production')){		
					g_form.setVisible('column2',true);
					g_form.setVisible('column3',true);
					
				}
			}			

		}

	}
}

The script include returns output : "Development , Production"

Script include

var getAppname = this.getParameter('sysparm_app_name');
		var getsysid = this.getParameter('sysparm_sysid');
		var grgetCI = new GlideRecord('cmdb_ci_service');
		grgetCI.addEncodedQuery('u_application_name!=NULL');
		grgetCI.addQuery('u_application_name',getAppname);			
		grgetCI.query();
		var CIlist = "";
		while(grgetCI.next()){				
			if(CIlist == ''){
				CIlist = grgetCI.used_for.toString();
			}
			else
			{
				CIlist = CIlist + ", "+ grgetCI.used_for.toString();
			}
		}
		var answer = CIlist;	
		//JSON.stringify(answer);
		return JSON.stringify(answer);
		//return answer;

I have the following issues and need help with the same

1. It works as expected on Native UI but not on Portal 

2. The hide/show functionality works however when the field is changed the older variables still appears irrespective of the condition. if the form is refreshed it works as expected. but not without loading

 

@Jaspal Singh @Sandeep Rajput @Tai Vu @Samaksh Wani 

 

 

6 REPLIES 6

babarat1
Tera Contributor

Hi Tai Vu,

 

I was able to make it work on Portal. Thanks for your help.

However I am still stuck with #2 which is to hide the variable when the value of  Product changes.

For example the User selects any value, it shows up the respective columns.

When the value of the Product is changed by the user (the form is not saved) the old variables/columns still appear.

it does not take the oldvalue as it will be empty as the form is not saved. How to fix this.

Hey @babarat1 

 

It's because the script still missing the case Else => what if the response not contains Development & Production.

Sample below.

if (response.indexOf('Development') >= 0) {
    g_form.setVisible('column1', true);
    g_form.setVisible('column2', true);
    g_form.setVisible('column3', true);
} else {
    g_form.clearValue('column1');
    g_form.setVisible('column1', false);
    g_form.clearValue('column2');
    g_form.setVisible('column2', false);
    g_form.clearValue('column3');
    g_form.setVisible('column3', false);
}

 

You may also need to handle the case empty.

if (isLoading) {
	return;
}

if(newValue === ''){
	g_form.clearValue('column1');
	g_form.setVisible('column1', true);
	g_form.clearValue('column2');
	g_form.setVisible('column2', true);
	g_form.clearValue('column3');
	g_form.setVisible('column3', true);
}

 

Cheers,

Tai Vu