run a query from a script

cba1
Kilo Explorer

I need to create a stand-alone script that can be manually run.   When executed, the script will perform a query of all non-resolved tickets for the current day, in a particular location.   I'm new to scripting Service-Now, but not programming in general.  

I have been searching through the examples, but I have not found anything like this.

1 ACCEPTED SOLUTION

Mike Allen
Mega Sage

The best way to do this is get the results that you want in a list view, then copy the query.



For example, let's say we want to work off the task table to grab all tickets regardless of type.   Go to a task list like so:


find_real_file.png


find_real_file.png


find_real_file.png


find_real_file.png



This gives me: active=true^location=ce3e85f037d0200044e0bfc8bcbe5d13



Now, go to scheduled job or fix script and craft your query:



var task = new GlideRecord('task');


task.addEncodedQuery('active=true^location=ce3e85f037d0200044e0bfc8bcbe5d13');


task.query();


while(task.next()){



//Do something



}


View solution in original post

9 REPLIES 9

eisenbathb
Kilo Contributor

You will probably want either a fix script or scheduled job. Depending on if you want it to run by it self and manual or just manually. What do you mean on the particular location? So i can help you with a draft of some code.


eisenbathb
Kilo Contributor

var gr = new GlideRecord('incident');


gr.addEncodedQuery('sys_created_onONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)');


gr.addActiveQuery();


gr.addQuery('u_user_location','Mississippi');


gr.query();


while(gr.next) {


        //dostuff


        //


        //


        gr.update();


}



Here is a pretty generic version of what it would look like!


Interesting.   So I would need to run this as a javascript program?


eisenbathb
Kilo Contributor

Yep! The filter set up below is also a very helpful tool. I just wanted to show you a few different types of queries since you said you are new to service now and it will be helpful to know! But going Mike's way will deffinently be easier for your problem right now.



Thanks