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 - i did not get any data in alert

 

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

 

 

g_form.addInfoMessage("Hello");
var ga = new GlideAjax("CustomRoleOptionsUtils");
ga.addParam("sysparm_name", "getRoles");
ga.addParam("sysparm_env", newValue); //i am seeing the newValue is not working

 

 

Below code getting alert

 

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

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

 

 

@Saib1 This is strange!

I simulated this in my PDI it's working fine.

 

Try changing the environment values in the form to see if it's working or not for any environment.

 

Please post the screenshot of your Environment variable choice list configuration here.

 

if you are getting empty alert, you need to look into Script Include. Try adding some logs using gs.info(); and check if you are seeing them in system logs.

 

If you are not even getting any alert even empty one you should look into client script only.

Trying adding alert() in client script to debug.

Thanks,
Anvesh

Please find the the environment variables choice

 

Saib1_0-1699380776873.png

 

@Saib1 

If you see the choices, their value is in small case like, test Instead of Test, beta instead of Beta and development instead of Dev.

 

So you should always use the value in comparison conditions or for any operation as the comparison is case sensitive.

 

In the Script Include update the if conditions to compare against the value of choices instead of Text. Like use the code below in your 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");

  } else if(env == 'development'){

   roles = gs.getProperty("custom.beta_environment_roles");

  }

  return roles;

 },

 

    type: 'CustomRoleOptionsUtils'

});

 

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

Thanks,
Anvesh

@AnveshKumar M 

 

Please find the below , its added in select box but seeing someone junk values .

 

Saib1_1-1699433017443.png