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

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

Thanks @AnveshKumar M - it is working now

Madankumar N1
Tera Contributor

Hi @Saib1 
You can write onchange client on variable
1.create variable with choices for roles
2 In Client Script first clear all options using clearoptions method
3.if check enrivoronment then required roles as choices using add option method