Get the data in different Rows and push into an array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 10:13 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 10:20 PM - edited 12-12-2023 10:23 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2023 10:59 PM
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.
Would you please help with any further solution?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 01:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-13-2023 09:52 PM
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?