- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 05:53 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 06:38 AM
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?
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 09:21 AM - edited 11-07-2023 09:37 AM
@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]);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 09:35 AM
@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.
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 10:13 AM
Please find the the environment variables choice
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 06:30 PM
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 👍✅
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2023 12:43 AM