Get the data in different Rows and push into an array

Rakshanda Kunte
Tera Contributor

Hi All,

 

We have a Use case where I need to get the data in 3 rows as below.

Note: Column 1 will always have same value

 

Column 1                              Key                                           Value

ABC                                       cost                                           Value 12

ABC                                     price                                           Value

ABC                                     money                                         Value56

 

 

We have written a script include and client script to get values from column 2 and 3.

Requirement is to get all details in columns 'Key' and 'Value'.

 

Script Include:

 

Tags: function() {

        var keyValues = [];

        var cmdbKeyGr = new GlideRecord('cmdb_key_value');

        cmdbKeyGr.addQuery('configuration_item.sys_id', this.getParameter('sysparm_vmname'));

        cmdbKeyGr.query();

 

        while (cmdbKeyGr.next()) {

           return  cmdbKeyGr.key +  cmdbKeyGr.value; 

    },

 

With this I am only getting 1st row. I need all 3 rows as output. 

Please provide your inputs.

 

Expected Output:

Key: cost, Value: Value 12
Key: price, Value: value
Key: money, Value: Value56

7 REPLIES 7

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,
You are writing the return statement within the loop.
Once the return execute means its end of the function.
You can store the data in a variable within while loop and return outside the loop.
Sample Code

function() {
        var keyValues = [];
        var cmdbKeyGr = new GlideRecord('cmdb_key_value');
        cmdbKeyGr.addQuery('configuration_item.sys_id', this.getParameter('sysparm_vmname'));
        cmdbKeyGr.query();
        while (cmdbKeyGr.next()) {
var obj={}
           obj.key=cmdbKeyGr.key;
obj.value=cmdbKeyGr.value; 
keyValues .push(obj)
    }
gs.info(JSON.stringify(keyValues ))
return keyValues;
},

 






Thanks and Regards,

Saurabh Gupta

@Saurabh Gupta  ,

 

We tried this script as below but did not get expected output:

 

Tags: function() {

        var keyValues = [];

        var cmdbKeyGr = new GlideRecord('cmdb_key_value');

        cmdbKeyGr.addQuery('configuration_item.sys_id', this.getParameter('sysparm_vmname'));

        cmdbKeyGr.query();

        while (cmdbKeyGr.next()) {

            var obj = {};

            obj = cmdbKeyGr.key + cmdbKeyGr.value;

            keyValues.push(obj);

        }

        gs.info('please'+ JSON.stringify(keyValues));

        return keyValues;

 

 

    },

 

getting below output on the form. And a very long log message in system logs. 

 

RakshandaKunte_0-1702450633093.jpeg

 

 

Would you please help with any further solution?

Hi 
Can you change your code as below

 

Tags: function() {

        var keyValues = [];

        var cmdbKeyGr = new GlideRecord('cmdb_key_value');

        cmdbKeyGr.addQuery('configuration_item.sys_id', this.getParameter('sysparm_vmname'));

        cmdbKeyGr.query();

        while (cmdbKeyGr.next()) {

            var obj = {};

            obj = cmdbKeyGr.key + cmdbKeyGr.value;

            keyValues.push(obj);

        }

        gs.info('please'+ JSON.stringify(keyValues));

        return JSON.stringify(keyValues);

 

 

    },

Thanks and Regards,

Saurabh Gupta

@Saurabh Gupta  

 

We got output as below:

 

please[{"key":"Cost","value":"125365"},{"key":"price","value":"3468"},{"key":"price","value":"48788"},{"key":"Cost","value":"4556"},{"key":"price","value":"465767"},{"key":"price","value":"341"},{"key":"Cost","value":"7866"},{"key":"price","value":"2135"},{"key":"price","value":"865"},

 

 

Issue here is, it is giving all the key and values present in the table. 

 

Is it possible to fix it?