- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2021 09:08 AM
Here is what I am trying to do.
Get a list of CI's from CMDB_CI table.
Use the returned CI's sys_id as condition (Example: sys_idIN'123,456,678)on the second table
//Get list of CI's
var cmdbCIList = [];
var g = new GlideRecord('cmdb_ci');
g.addQuery('name','10.10.10.10.abc'); \\This returns 5 CI's which matches the name
g.query();
while (g.next())
cmdbCIList.push(cmdbCI);
gs.info("Records: " + g.getRowCount() + " CI Name: " + g.sys_id);
//Create TAG Lable
if (g.getRowCount() <10)
var lab = new GlideRecord('label_table');
//lab.addQuery('table','cmdb_ci')
lab.query();
lab.initialize();
lab.title.setDisplayValue('Application Name');
lab.table.setDisplayValue('cmdb_ci');
lab.conditions.setDisplayValue("sys_idIN" + cmdbCIList[i].sys_id); \\Here is where I am struggling. I need to list the sys_id's separated by a comma.
lab.insert();
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2021 09:45 AM
Ok so first of all you need to post your code formatted to make it easier to read, use the "{:}" button in the editor. Second I can only assume you did not post all of it because you are missing "{" and "}" in a number of places that absolutely need them or it will only run one line of code and based on what you posted that will not work.
You are making what I would say is a common mistake with your array, while loop and the the line "cmdbCIList.push(cmdbCI);"
So I'm assuming that cmdbCI should have been "g" since that is what you are using in your loop. If that is true then pushing an object onto an array multiple times will just make that array contain the same object multiple times. The "g.sys_id" is a GlideElement object that will change each time thru the loop to what ever row the loop is on so you will have every element be the very last row of the loop. You need to do a getValue so you have the string. So basically "g.getValue('sys_id')" or you can do "g.sys_id.toString()". I avoid the later because I have run into cases where the field will be null so you will get a null pointer error so I just avoid it as a rule.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2021 09:14 AM
change lab.conditions.setDisplayValue("sys_idIN" + cmdbCIList[i].sys_id);
to
lab.conditions = cmdbCIList;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2021 09:17 AM
also you don't need these two lines. if inserting a record then why query?
lab.addQuery('table','cmdb_ci')
lab.query();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2021 09:36 AM
Hi Samir - cmdbCIList is returning "object GlideRecord".
gs.log("cmdbCIList " + cmdbCIList);
cmdbCIList [object GlideRecord]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2021 09:45 AM
sorry, I overlooked your code, try below
//Get list of CI's
var cmdbCIList = [];
var g = new GlideRecord('cmdb_ci');
g.addQuery('name','10.10.10.10.abc'); \\This returns 5 CI's which matches the name
g.query();
while (g.next()){
cmdbCIList.push(g.sys_id);
}
gs.info("Records: " + g.getRowCount() + " CI Name: " + g.sys_id);
//Create TAG Lable
if (g.getRowCount() <10)
var lab = new GlideRecord('label_table');
lab.initialize();
lab.title.setDisplayValue('Application Name');
lab.table.setDisplayValue('cmdb_ci');
lab.conditions = cmdbCIList;
lab.insert();