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

Need a Client Script for Select Box

Saib1
Tera Guru

Hi All,

 

I need to add the few values in select box by using the Client Script. Please help me

 

Based on the Environment , i need to add the Roles on the below select box

 

For example :

if select 

Environment - Test 

Role - Admin, ITIL, cmdb_read

 

if select 

Environment - Beta

Role - ITIL, cmdb_read

like wise i need to add 100 roles on the client script based on the environment.

 

How to do via the onchange client script?

 

 

 

 

Saib1_0-1699365048488.png

 

 

 

1 ACCEPTED SOLUTION

Hello @Saib1 

 

Are you getting this after updating the ScriptInclude? If yes, Please share your code.

 

And can you also try changing the Client Script to the below one?

 

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

   var ga = new GlideAjax("CustomRoleOptionsUtils");
   ga.addParam("sysparm_name", "getRoles");
   ga.addParam("sysparm_env", newValue);
   ga.getXMLAnswer(populateRoles);

   function populateRoles(answer){
	var roles = answer.split(",");
	g_form.clearOptions("roles");
	var len = roles.length;
	for(var i=0; i < len; i++){
		g_form.addOption("roles", roles[i], roles[i]);
	}
   }
   
}

 

Please share your code (not screenshots, paste script as text) and screenshots of your environment variable choices again.

 

And please confirm, are you testing this in classic view or service portal?

Thanks,
Anvesh

View solution in original post

17 REPLIES 17

AnveshKumar M
Tera Sage
Tera Sage

Hi @Saib1 

Try the following approach,

 

1. Create sys_properties for each environment with all the roles in value as comma separated values.

 

2. Create a Client Callable Script Include to return the roles, which can be called from Client Side using GlideAjax.

 

3. Create onChange client script which calls the Script Include and iterate through the result and populate the values using g_form.addOption() API. (Clear the previous options suing g_form.clearOptions())

 

Please mark my answer helpful and accept as solution if it helped 👍✔️

Thanks,
Anvesh

Hi @AnveshKumar M 

 

I understand the sys_properties to  be created

 

CAn you give me some sample on the 2 and 3

@Saib1 Please see my another response below.

Thanks,
Anvesh

AnveshKumar M
Tera Sage
Tera Sage

@Saib1  You can follow the below screenshots and script.

 

1. Sys Properties.

AnveshKumarM_0-1699366957655.png

 

AnveshKumarM_1-1699366980436.png

 

 

2. Client Callable Script Include

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

	getRoles: function(){
		var env = this.getParameter('sysparm_env');
		var roles;
		if(env == 'Test'){
			roles = gs.getProperty("custom.test_environment_roles");
		} else if(env == 'Beta'){
			roles = gs.getProperty("custom.beta_environment_roles");
		}
		return roles;
	},

    type: 'CustomRoleOptionsUtils'
});

AnveshKumarM_2-1699367015698.png

 

3. Catalog Client Script

 

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

   var ga = new GlideAjax("CustomRoleOptionsUtils");
   ga.addParam("sysparm_name", "getRoles");
   ga.addParam("sysparm_env", newValue);
   ga.getXMLAnswer(populateRoles);

   function populateRoles(answer){
	var roles = answer.split(",");
	g_form.clearOptions("roles");
	for(key in roles){
		g_form.addOption("roles", roles[key], roles[key]);
	}
   }
   
}

AnveshKumarM_3-1699367049196.png

 

 

Please mark my answer helpful and accept as solution if it helped 👍✔️

Thanks,
Anvesh