Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to add array elements into a selectbox as options?

simran3
Tera Contributor

I have an onChange client script that collects an array of values (groups) that the user selected is in. How can I add these values in a select box as options? 

find_real_file.png

I want those values in the "Existing HLA Access in Group(s):" field to be options in remove from group. For example: "Domain RTR Admins", "Domain CyberArk Users", and "Domain SMS Users-WAN" as options in the "Remove from Group" Select Box. The text in that field will change whenever the requested for is changed, so I need to create a functionality that will populate the selectbox options with that.

Here is the client script to populate the first text field:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('getHLAGroups');
ga.addParam('sysparm_name', 'groupHLA');
ga.addParam('sysparm_userSelect', g_form.getValue('u_user_hla'));
ga.getXML(updateGroup);

}
function updateGroup(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
// alert('Answer is: '+answer);
answer = answer.slice(0, -1);
var inAnswer = answer.split(';');
var outData = '';
for (var i=0; i < inAnswer.length; i++) {
outData = outData+'\n'+inAnswer[i].toString();
}
g_form.setValue('u_group_hla', outData);
}

 

I tried this: 

myArray = answer.split(",");
var a = myArray[0];
alert(a);
g_form.addOption('remove_from_group', 'myArray[0]','myArray');

 

but it keeps adding "MyArray" as the only option in the select box.

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

try below

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('getHLAGroups');
    ga.addParam('sysparm_name', 'groupHLA');
    ga.addParam('sysparm_userSelect', g_form.getValue('u_user_hla'));
    ga.getXML(updateGroup);

}

function updateGroup(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    // alert('Answer is: '+answer);
    answer = answer.slice(0, -1);
    var inAnswer = answer.split(';');
    var outData = '';
    for (var i = 0; i < inAnswer.length; i++) {
        outData = outData + '\n' + inAnswer[i].toString();
        g_form.addOption('u_group_hla', inAnswer[i].toString(), inAnswer[i].toString());
    }
}

View solution in original post

12 REPLIES 12

Community Alums
Not applicable

Yes correct,

 

Both the fields are Lookup select box

1) BU

Tejas12_0-1678205064802.png

 

2) Country

Tejas12_1-1678205103198.png

 

 

Can you also give a sample of your referenced table, so I can try to recreate what you are attempting to do? I think you should be able to use the ref_qual_elements attribute to do this, and not have to use a client script.
Variable Attributes 

mike_allgire
Giga Guru

The reason "MyArray" kept displaying, is due to quotes around the myArray[0] (value) and myArray (label) in your g_form. You set the value and label to a string value and not the variable value. When you use the g_form.addOption method, the only way to make it change each time is to clear the options prior to rebuilding them based on the variable value.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Clear any options in the variable everytime the script runs
    g_form.clearOptions('u_group_hla');

    var ga = new GlideAjax('getHLAGroups');
    ga.addParam('sysparm_name', 'groupHLA');
    ga.addParam('sysparm_userSelect', g_form.getValue('u_user_hla'));
    ga.getXML(updateGroup);

}

function updateGroup(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    // alert('Answer is: '+answer);
    answer = answer.slice(0, -1);
    var inAnswer = answer.split(';');
    var outData = '';
    for (var i = 0; i < inAnswer.length; i++) {
        outData = outData + '\n' + inAnswer[i].toString();
        g_form.addOption('u_group_hla', inAnswer[i].toString(), inAnswer[i].toString());
    }
}