- 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 06:09 AM
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 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 06:14 AM
I understand the sys_properties to be created
CAn you give me some sample on the 2 and 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 07:10 AM
@Saib1 Please see my another response below.
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 06:24 AM
@Saib1 You can follow the below screenshots and script.
1. Sys Properties.
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'
});
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]);
}
}
}
Please mark my answer helpful and accept as solution if it helped 👍✔️
Anvesh