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

country: function() {
	var bu = this.getParameter('sysparm_bu');
	gs.log("I am Business" + bu);
	var arr = [];
	var gr = new GlideAggregate('u_oracle_financial_approvals');
	gr.addQuery('u_business_unit', bu);
	gr.groupBy('u_country');
	gr.query();
	gs.log("Tejas" + gr);
	while (gr.next()) {
		var obj = gr.getValue('u_country');
		arr.push(obj);
		gs.log("Result", +arr);
	}
	
	return arr.join(',');
},

 

OnChange Catalog Client Script - 
var rf = g_form.getDisplayValue('u_business_unit');
alert('Hi, I am ' + rf);
var ga = new GlideAjax('Oracle_Business_Units');
ga.addParam('sysparm_name', 'country');
ga.addParam('sysparm_bu', rf);
ga.getXML(setAnswer);

function setAnswer(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.setValue('country', answer);
}

Community Alums
Not applicable

Hi @mike_allgire ,

 

I am getting output as below - 

Tejas12_0-1678172536337.png

 

It is giving all values of country plus expected countries in single line as one option with comma separated values

I want highlighted values as options in that field

 

Are you trying to select more than a single value in a Lookup Select Box? If so, that is your issue. Lookup Select Box is a single value entry. If you use a list, then you can select multiple values.

Community Alums
Not applicable

Hi @mike_allgire ,

 

I am not looking for multiple values to select, I am getting the whole array like below as a single option in that variable

Australia, Argentina, India, USA, Vietnam

 

I want to separate the above values as single option to be selected, like:

Australia

Argentina

India

USA

Vietnam

So you are wanting to filter the Country available values based on the BU selected in another Lookup Select Box? If so, can you share your Lookup Select Box configuration for each?