Query a gliderecord in "for" loop - need some help

rexter1
Tera Expert

Hello guys,

I have narrowed down a very big script which works 95% but there is a part I cannot fixed.

within a for loop I succesfulye change the query for the same GlideRecord, but the result is the same. 

In the script, In an object I have to CI elements sys_ids. First, I query all the Incident assigned to Fred Luddy. (This part is irrevelant here, I get the unique CI-s basically, that part is not in the code)

Then I loop as many times as many CI I have (2) in the example.

In the addEncoded query first I use the 1st CI, then the 2nd. The simplified goal:
I push all he short description of the Incidents assigned to 1st CI into Header object - I do something with it, then

I push all he short description of the Incidents assigned to 2st CI into Header object - I do something with it, then

But something wrong. The Header and CI calue displays always the same, though the Query is different.Certanly the Short descriptions (and the CIs) name are different. 

 

var cmdbItems = ['2fd114e10a0a0bb400f07732a4131e72','281a4d5fc0a8000b00e4ba489a83eedc']; //Peoplesoft and IT services cmdb sys_ids

var Header = [];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('assigned_to=5137153cc611227c000bbd1bd8cd2005'); //Fred L
gr.query();

while (gr.next()) {
    for (var x = 0; x < cmdbItems.length; x++) {
        var cmbdElements = cmdbItems[x];  
        var queryString = 'assigned_to=5137153cc611227c000bbd1bd8cd2005^cmdb_ci=' + cmbdElements;
        gr.addEncodedQuery(queryString);
        gr.query();
        gs.log("The query " + queryString, "TESTQUERY");
        while (gr.next()) {
             Header.push(gr.short_description.getDisplayValue().toString());
        }
        gs.log("CI selected " + gr.cmdb_ci.getDisplayValue(), "TESTQUERY");
		gs.log("Short descriptions " + Header, "TESTQUERY");
        //table_namesVal.length = 0;
    }
}

The script result:

*** Script: THE QUERY: assigned_to=5137153cc611227c000bbd1bd8cd2005^cmdb_ci=2fd114e10a0a0bb400f07732a4131e72
*** Script: CI selected PeopleSoft Reporting
*** Script: Short descriptions: Spam Mail,IP phone direct dial not working
*** Script: THE QUERY: assigned_to=5137153cc611227c000bbd1bd8cd2005^cmdb_ci=281a4d5fc0a8000b00e4ba489a83eedc
*** Script: CI selected PeopleSoft Reporting
*** Script: Short descriptions: Spam Mail,IP phone direct dial not working
 
 
So it runs twice - each log row twice -  which is ok because I have two CI-s  in cmdbItems object.
THE QUERY also looks ok, the cmdb_ci sys_ids are different!
 
But why the CI selected and Short descriptions are the same? 
 
Maybe I wish to reach somethng not possible in this way? Could you help me please?
Thanks a lot!
 
 
4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

The while inside a if inside a while looks odd. Though wat at least is an issue:

Could you move:

var Header = [];

Inside the for loop?

+

Don't get the logic of you query, though works then on my PDI.

So:

var cmdbItems = ['2fd114e10a0a0bb400f07732a4131e72','281a4d5fc0a8000b00e4ba489a83eedc']; //Peoplesoft and IT services cmdb sys_ids


    for (var x = 0; x < cmdbItems.length; x++) {
        var Header = [];
        var cmbdElements = cmdbItems[x];  
        var queryString = 'assigned_to=5137153cc611227c000bbd1bd8cd2005^cmdb_ci=' + cmbdElements;

        var gr = new GlideRecord('incident');
        gr.addEncodedQuery(queryString);
        gr.query();
        gs.log("The query " + queryString, "TESTQUERY");
        while (gr.next()) {
             Header.push(gr.short_description.getDisplayValue().toString());
        }
        gs.log("CI selected " + gr.cmdb_ci.getDisplayValue(), "TESTQUERY");
		gs.log("Short descriptions " + Header, "TESTQUERY");
        //table_namesVal.length = 0;
    }

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

it seems the query is not getting formed correctly and hence overriding the value

the query is getting appended; you can check that by trying to print the encodedQuery being formed

just try this line and check what is happening

gs.log("The query " + gr.getEncodedQuery(), "TESTQUERY");

Now use below updated code for your scenario and it would push the values properly to array

var cmdbItems = ['2fd114e10a0a0bb400f07732a4131e72','281a4d5fc0a8000b00e4ba489a83eedc']; //Peoplesoft and IT services cmdb sys_ids

var Header = [];
var gr = new GlideRecord('incident');
gr.addEncodedQuery('assigned_to=5137153cc611227c000bbd1bd8cd2005'); //Fred L
gr.addQuery('cmdb_ci', 'IN', cmdbItems);
gr.query();

while (gr.next()) {
             Header.push(gr.short_description.getDisplayValue().toString());
 gs.log("CI selected " + gr.cmdb_ci.getDisplayValue(), "TESTQUERY");
	gs.log("Short descriptions " + gr.short_description, "TESTQUERY");
        }
       
    gs.log("Header array is: " + Header);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

thank you, your answer helped me to go forward in the correct way! cheers!

 

Hi

I am glad to hear your goal has been achieved.
Saying this, I will appreciate if you close this thread, as answered.
Have a great rest of the day

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader