Push data in 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.

 

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

2 REPLIES 2

ayan_murshad
Tera Guru

Try below Updated 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()) {
var keyValue = {
key: cmdbKeyGr.key.getDisplayValue(),
value: cmdbKeyGr.value.getDisplayValue()
};
keyValues.push(keyValue);
}

return keyValues;
},

 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

Regards,

Ayan Murshad

@ayan_murshad 

 

We got below output when run in background script:

[object Object],[object Object],[object Object],[object Object]

 

We need values in place of object.

 

Expected Output:

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

 

Would you please help with this?