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

Thanks @AnveshKumar M 

 

what this client script is doing ,i do not understand 

 

 

 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]);
	}
   }

 

@AnveshKumar M 

 

it is not working

@Saib1 this script will get the roles based on selected environment.

Have you changed your variable names as per your configuration in the catalog item?

 

If it is not working, are you getting any error?

Thanks,
Anvesh

yes @AnveshKumar M please find the below , i am not getting any error, just data is not populated in select box

 

Saib1_4-1699375822760.png

 

 

 

Saib1_2-1699375552782.png

 

 

@Saib1 

 

Please try this code and see, if you are getting any data in alert.

 

function populateRoles(answer){

alert(answer); //Add this line in your client script

 var roles = answer.split(",");

 g_form.clearOptions("roles");

 for(key in roles){

  g_form.addOption("roles", roles[key], roles[key]);

 }

   }

Thanks,
Anvesh