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.

Need to push array values to List Collector

Pallavi65
Tera Contributor

Hello all,

 

I have a reference variable in a catalog item and a list collector variable.

Reference field type variable: (u_field1)

List collector type variable: (u_field2)

 

Based on reference field selection, its corresponding values has to be populated or shown in list collector for selection.

I have written a script include and Reference qualifier.

But the list e=collector is displaying only one value but not all the related ones. 

I have out log messages and observed, "Array is" log is giving two sys_ids, but why the list collector is displaying only one?

 

My code screenshot:

 
 
 

Script:

var Cat_scripts = Class.create();

Cat_scripts.prototype = {

    initialize: function() {},

    getActiveContracts: function(cn) {

    gs.log("CN is "+cn);

        var ar = [];

        var gr = new GlideRecord('ast_contract');

       

        gr.addQuery('account', cn);

        gr.query();

        while (gr.next()) {

            ar.push(gr.sys_id);

            gs.log("Array is "+ar);

        }

      

        return ar;

       

    },

    type: 'Cat_scripts'

};

 

 
 

 

 

1 ACCEPTED SOLUTION

By the way, the reason its not pushing multiple values is because your array push is incorrect.

Replace:

ar.push(gr.sys_id);

With:

ar.push(gr.sys_id.toString());

View solution in original post

9 REPLIES 9

AnirudhKumar
Mega Sage

Why do you need a reference field to pick values and pass it to a list collector field?

The List Collector also does the job of a reference field. 

You can simply remove the reference field in this case.

By the way, the reason its not pushing multiple values is because your array push is incorrect.

Replace:

ar.push(gr.sys_id);

With:

ar.push(gr.sys_id.toString());

Hello Anirudh,

 

It's working.

But why.toString() ?

 

 

Regards,

Pallavi

I'll tell you if you mark my answer correct...😉

ok kidding, not an expert at how javascript works... but it seems push overwrites existing element inside the array when in a while loop unless there is a toString()