How to send multiple sys_ids from client script to Script Include

Community Alums
Not applicable

Hi Team,

 

I have written below client script in UI page from which I have to send multiple sys_ids to Script Include.

Please see Client script code:

function continueOK() {

    //Get the selected values from the right slushbucket
    alert("You selected Ok");
    var values = slushAccounts.getValues(slushAccounts.getRightSelect());


    if (values == '') {

        alert("At least one group must be selected");

        return;

    } else {
        alert('hello1');


        var ajax = new GlideAjax('GetAccountsAjax');

        for (var i = 0; i < values.length; i++) {

            ajax.addParam('sysparm_name', 'accountAdd');

            ajax.addParam('sysparm_values', values[i]);

            ajax.getXML(addGroupResponse);
        }
 
 
ajax.addParam('sysparm_values', values[i]); -->Here is I am sending the values .Please let me know if this is correct or somethin is wrong.
 
Also in script include how to retrieve this list of sys_ids using this.getParameter('sysparm_values) ?
1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums Instead of calling the addParam in loop, you should send the comma separated list of sys_ids using the normal GlideAjax call.

 

function continueOK() {

    //Get the selected values from the right slushbucket
    alert("You selected Ok");
    var values = slushAccounts.getValues(slushAccounts.getRightSelect());
    if (values == '') {

        alert("At least one group must be selected");

        return;

    } else {
        alert('hello1');
        var ajax = new GlideAjax('GetAccountsAjax');
         ajax.addParam('sysparm_name', 'accountAdd');
         ajax.addParam('sysparm_values', values.toString());
         ajax.getXML(addGroupResponse);
        }
}

 

And use .split(',') method in the script include to extract an array of sys_ids in the server side.

 

e.g. 

//Script include
var accountString = this.getParameter('sysparm_values);
var accountArray = accountString.split(','); //Returns an array by spliting the string with ,

for (var i=0; i<accountArray.length;i++){
//do something here
}

Hope this helps.

View solution in original post

2 REPLIES 2

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums Instead of calling the addParam in loop, you should send the comma separated list of sys_ids using the normal GlideAjax call.

 

function continueOK() {

    //Get the selected values from the right slushbucket
    alert("You selected Ok");
    var values = slushAccounts.getValues(slushAccounts.getRightSelect());
    if (values == '') {

        alert("At least one group must be selected");

        return;

    } else {
        alert('hello1');
        var ajax = new GlideAjax('GetAccountsAjax');
         ajax.addParam('sysparm_name', 'accountAdd');
         ajax.addParam('sysparm_values', values.toString());
         ajax.getXML(addGroupResponse);
        }
}

 

And use .split(',') method in the script include to extract an array of sys_ids in the server side.

 

e.g. 

//Script include
var accountString = this.getParameter('sysparm_values);
var accountArray = accountString.split(','); //Returns an array by spliting the string with ,

for (var i=0; i<accountArray.length;i++){
//do something here
}

Hope this helps.

Brian Lancaster
Tera Sage

Try changing this line var values = slushAccounts.getValues(slushAccounts.getRightSelect()); to 

var values = slushAccounts.getValues(slushAccounts.getRightSelect()).toString().split(',');

 

Or is might be better wo have your script include to accept the array and process everything at one time instead of calling the script include multiple times.