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

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



}


Great!   This will get me closer to what I needed.   One final question and I think I can guess the answer.  



Can the script you posted be done as a VBScript?


You want to pull it to an external source?   You can use VB and REST or SOAP to grab data.   I don't believe there is a way to use VB inside the tool.


That's EXACTLY what I'm trying to do.   It needs to run a quick query, take the results and perform some processing after the data comes back.  



I know it's possible to use SOAP inside VBScript (or more likely, VB.NET) and capture the results.   Just getting over this initial hurtle will get me much closer to my goal.


Try these:


Table API - ServiceNow Wiki //This does not have a VBScript example, but you can see how it is done with 6 languages and make inferences based on that


VBScript REST Example - REST - Universal Devices Forum   //This is a small REST example, not for SN, but, once again, inferences can be made