The CreatorCon Call for Content is officially open! Get started here.

Populate Multiple Values

APV Babu
Tera Contributor
I am trying to populate multiple values in string field, I am getting only one value which is last selected one
Please find below script.
 
 
 
 
(function executeRule(current, previous /*null when async*/ ) {
   
var plan = current.getValue('plan_s').split(',');

    for (var i = 0; i < plan.length; i++) {
        gs.info(plan[i] + 'Result');
        var client = new GlideRecord("u_table");
        client.addQuery('sys_id', plan[i]);
        client.query();

        while (client.next()) {

            current.client_name = client.u_client_name.u_name;
            gs.info(client.u_client_name.u_name + 'clientname');
            current.client_number = client.u_client_name.u_client_number;
            gs.info(client.u_client_name.u_client_number + 'cliennumber');

        }
    }

})(current, previous);
 
 
Regards,
APV Babu
1 ACCEPTED SOLUTION

Aniket Chavan
Tera Sage
Tera Sage

Hello @APV Babu ,

 

Please give a try to the code below and let me know how it works for you.

(function executeRule(current, previous /*null when async*/ ) {
    var plan = current.getValue('plan_s').split(',');
    var clientNames = [];
    var clientNumbers = [];

    for (var i = 0; i < plan.length; i++) {
        gs.info(plan[i] + ' Result');
        var client = new GlideRecord("u_table");
        client.addQuery('sys_id', plan[i]);
        client.query();

        while (client.next()) {
            clientNames.push(client.u_client_name.u_name);
            gs.info(client.u_client_name.u_name + ' clientname');
            clientNumbers.push(client.u_client_name.u_client_number);
            gs.info(client.u_client_name.u_client_number + ' cliennumber');
        }
    }

    current.client_name = clientNames.join(', ');
    current.client_number = clientNumbers.join(', ');

})(current, previous);

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

View solution in original post

11 REPLIES 11

@APV Babu 

 

oh there is the issue then. 

 

Could u try something like below. Also plz tell me "plan_s" is of what type (reference/List Collector).

 

var plan = current.plan_s.split(',');

 

Thanks,

Danish

 

Its List Collector

Hi @APV Babu ,

 

Did u try the above method?

 

Thanks,

Danish

 

RikV
Tera Expert

If you would like to append values to an existing value you should use :

variabel += valuetoadd ( current.client_name += client.u_client_name.u_name; )

 

You are currently doing:

variable = valuetoadd ( current.client_name = client.u_client_name.u_name; )

This will overwrite the current value, making it only hold the last one (since that is the last one that has overwritten the value).

 

Extra info:

using += is actually the same as writing:

variable = variable + valuetoadd

Only its considered a bit more cleaner way of doing so.

String example:

 

var allwords = "";
var word1 = "Hello "
var word2 = "World!"   
allwords += word1
allwords += word2; // allwords is now "Hello World!"

 

 

This method also works for numbers/integers to aggregate a total.  For example:

 

var total = 0;
var value1 = 10
var value2 = 5

total += value1 // total = 10
total += value2 // total = 15

 

 

Aniket Chavan
Tera Sage
Tera Sage

Hello @APV Babu ,

 

Please give a try to the code below and let me know how it works for you.

(function executeRule(current, previous /*null when async*/ ) {
    var plan = current.getValue('plan_s').split(',');
    var clientNames = [];
    var clientNumbers = [];

    for (var i = 0; i < plan.length; i++) {
        gs.info(plan[i] + ' Result');
        var client = new GlideRecord("u_table");
        client.addQuery('sys_id', plan[i]);
        client.query();

        while (client.next()) {
            clientNames.push(client.u_client_name.u_name);
            gs.info(client.u_client_name.u_name + ' clientname');
            clientNumbers.push(client.u_client_name.u_client_number);
            gs.info(client.u_client_name.u_client_number + ' cliennumber');
        }
    }

    current.client_name = clientNames.join(', ');
    current.client_number = clientNumbers.join(', ');

})(current, previous);

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket