GlideRecord Query Cheat Sheet - ServiceNow Guru (2024)

Table of Contents
49 Comments Related Posts

I doubt if there’s a single concept in Service-now that is more valuable to understand than how to use GlideRecord methods to query, insert, update, and delete records in your system. These methods have a wide variety of uses and are found at the heart of many of the business rules, UI actions, and scheduled job scripts that are essential to tie together your organization’s processes in your Service-now instance.

While the content of this post isn’t new information (additional examples can be found on the Service-now wiki), my aim is to provide a single page of information containing some common examples of these methods as a reference. This is an excellent page to keep bookmarked!

Note: These methods are designed for use in server-side JavaScript (everything EXCEPT client scripts and UI policies). In some rare cases, it may be necessary to perform a query from a client-side javascript (client script or UI policy). The few methods below that can be used in client-side JavaScript have been noted below.

Query

Can also be used in Client scripts and UI policies.

A standard GlideRecord query follows this format.

var gr = new GlideRecord('incident'); //Indicate the table to query from//The 'addQuery' line allows you to restrict the query to the field/value pairs specified (optional)//gr.addQuery('active', true);gr.query(); //Execute the querywhile (gr.next()) { //While the recordset contains records, iterate through them//Do something with the records returnedif(gr.category == 'software'){gs.log('Category is ' + gr.category);}}

UPDATE: This same function applies to client-side GlideRecord queries! If at all possible, you should use an asynchronous query from the client. See this post for details.

var gr = new GlideRecord('sys_user');gr.addQuery('name', 'Joe Employee');gr.query(myCallbackFunction); //Execute the query with callback function//After the server returns the query recordset, continue herefunction myCallbackFunction(gr){while (gr.next()) { //While the recordset contains records, iterate through themalert(gr.user_name);}}

‘Get’ Query Shortcut (used to get a single GlideRecord)

Can also be used in Client scripts and UI policies IF YOU ARE GETTING A RECORD BY SYS_ID.

The ‘get’ method is a great way to return a single record when you know the sys_id of that record.

var gr = new GlideRecord('incident');gr.get(sys_id_of_record_here);//Do something with the record returnedif(gr.category == 'software'){gs.log('Category is ' + gr.category);}

You can also query for a specific field/value pair. The ‘get’ method returns the first record in the result set.

//Find the first active incident recordvar gr = new GlideRecord('incident');if(gr.get('active', true)){//Do something with the record returnedgs.log('Category is ' + gr.category);}

‘getRefRecord’ Query Shortcut (used to get a single GlideRecord referenced in a reference field)
The ‘getRefRecord’ method can be used as a shortcut to query a record populated in a reference field on a record.

var caller = current.caller_id.getRefRecord(); //Returns the GlideRecord for the value populated in the 'caller_id' fieldcaller.email = 'test@test.com';caller.update();

‘OR’ Query
The standard ‘addQuery’ parameter acts like an ‘and’ condition in your query. This example shows how you can add ‘or’ conditions to your query.

//Find all incidents with a priority of 1 or 2var gr = new GlideRecord('incident');var grOR = gr.addQuery('priority', 1);grOR.addOrCondition('priority', 2);gr.query();while (gr.next()) {//Do something with the records returnedif(gr.category == 'software'){gs.log('Category is ' + gr.category);}}

Note that you can also chain your ‘OR’ condition as well, which is usually simpler

//Find all incidents with a priority of 1 or 2var gr = new GlideRecord('incident');gr.addQuery('priority', 1).addOrCondition('priority', 2);gr.query();

Insert
Inserts are performed in the same way as queries except you need to replace the ‘query()’ line with an ‘initialize()’ line as shown here.

//Create a new Incident record and populate the fields with the values belowvar gr = new GlideRecord('incident');gr.initialize();gr.short_description = 'Network problem';gr.category = 'software';gr.caller_id.setDisplayValue('Joe Employee');gr.insert();

Update
You can perform updates on one or many records simply by querying the records, setting the appropriate values on those records, and calling ‘update()’ for each record.

//Find all active incident records and make them inactivevar gr = new GlideRecord('incident');gr.addQuery('active',true);gr.query();while (gr.next()) {gr.active = false;gr.update();}

Delete
Delete records by performing a glideRecord query and then using the ‘deleteRecord’ method.

//Find all inactive incident records and delete them one-by-onevar gr = new GlideRecord('incident');gr.addQuery('active',false);gr.query();while (gr.next()) {//Delete each record in the query result setgr.deleteRecord();}

deleteMultiple Shortcut
If you are deleting multiple records then the ‘deleteMultiple’ method can be used as a shortcut

//Find all inactive incidents and delete them all at oncevar gr = new GlideRecord('incident');gr.addQuery('active', false);gr.deleteMultiple(); //Deletes all records in the record set

addEncodedQuery

CANNOT be used in Client scripts and UI policies! Use ‘addQuery(YOURENCODEDQUERYHERE)’ instead.

An alternative to a standard query is to use an encoded query to create your query string instead of using ‘addQuery’ and ‘addOrCondition’ statements. An easy way to identify the encoded query string to use is to create a filter or a module with the query parameters you want to use, and then hover over the link or breadcrumb and look at the URL. The part of the URL after ‘sysparm_query=’ is the encoded query for that link.
So if I had a URL that looked like this…
https://demo.service-now.com/incident_list.do?sysparm_query=active=true^category=software^ORcategory=hardware

My encoded query string would be this…
active=true^category=software^ORcategory=hardware

I could build that encoded query string and use it in a query like this…

//Find all active incidents where the category is software or hardwarevar gr = new GlideRecord('incident');var strQuery = 'active=true';strQuery = strQuery + '^category=software';strQuery = strQuery + '^ORcategory=hardware';gr.addEncodedQuery(strQuery);gr.query();

GlideAggregate
GlideAggregate is actually an extension of the GlideRecord object. It allows you to perform the following aggregations on query recordsets…
-COUNT
-SUM
-MIN
-MAX
-AVG

//Find all active incidents and log a count of records to the system logvar gr = new GlideAggregate('incident');gr.addQuery('active', true);gr.addAggregate('COUNT');gr.query();var incidents = 0;if (gr.next()){incidents = gr.getAggregate('COUNT');gs.log('Active incident count: ' + incidents);}

orderBy/orderByDesc
You can order the results of your recordset by using ‘orderBy’ and/or ‘orderByDesc’ as shown below.

//Find all active incidents and order the results ascending by category then descending by created datevar gr = new GlideRecord('incident');gr.addQuery('active', true);gr.orderBy('category');gr.orderByDesc('sys_created_on');gr.query();

addNullQuery/addNotNullQuery
‘addNullQuery’ and ‘addNotNullQuery’ can be used to search for empty (or not empty) values

//Find all incidents where the Short Description is emptyvar gr = new GlideRecord('incident');gr.addNullQuery('short_description');gr.query();
//Find all incidents where the Short Description is not emptyvar gr = new GlideRecord('incident');gr.addNotNullQuery('short_description');gr.query();

getRowCount
‘getRowCount’ is used to get the number of results returned

//Log the number of records returned by the queryvar gr = new GlideRecord('incident');gr.addQuery('category', 'software');gr.query();gs.log('Incident count: ' + gr.getRowCount());

Although ‘getRowCount’ isn’t available client-side, you can return the number of results in a client-side GlideRecord query by using ‘rows.length’ as shown here…

//Log the number of records returned by the queryvar gr = new GlideRecord('incident');gr.addQuery('category', 'software');gr.query();alert('Incident count: ' + gr.rows.length);

setLimit
‘setLimit’ can be used to limit the number of results returned

//Find the last 10 incidents createdvar gr = new GlideRecord('incident');gr.orderByDesc('sys_created_on');gr.setLimit(10);gr.query();

chooseWindow
The chooseWindow(first,last) method lets you set the first and last row number that you want to retrieve and is typical for chunking-type operations. The rows for any given query result are numbered 0..(n-1), where there are n rows. The first parameter is the row number of the first result you’ll get. The second parameter is the number of the row after the last row to be returned. In the example below, the parameters (10, 20) will cause 10 rows to be returned: rows 10..19, inclusive.

//Find the last 10 incidents createdvar gr = new GlideRecord('incident');gr.orderByDesc('sys_created_on');gr.chooseWindow(10, 20);gr.query();

setWorkflow
‘setWorkflow’ is used to enable/disable the running of any business rules that may be triggered by a particular update.

//Change the category of all 'software' incidents to 'hardware' without triggering business rules on updated recordsvar gr = new GlideRecord('incident');gr.addQuery('category', 'software');gr.query();while(gr.next()){gr.category = 'hardware';gr.setWorkflow(false);gr.update();}

autoSysFields
‘autoSysFields’ is used to disable the update of ‘sys’ fields (Updated, Created, etc.) for a particular update. This really is only used in special situations. The primary example is when you need to perform a mass update of records to true up some of the data but want to retain the original update timestamps, etc.

//Change the category of all 'software' incidents to 'hardware' without updating sys fieldsvar gr = new GlideRecord('incident');gr.addQuery('category', 'software');gr.query();while(gr.next()){gr.category = 'hardware';gr.autoSysFields(false);gr.update();}

setForceUpdate
‘setForceUpdate’ is used to update records without having to change a value on that record to get the update to execute. ‘setForceUpdate’ is particularly useful in situations where you need to force the recalculation of a calculated field for all records in a table or when you need to run business rules against all records in a table but don’t want to have to change a value on the records.
This method is often used with ‘setWorkflow’ and ‘autoSysFields’ as shown below.

//Force an update to all User records without changing field valuesvar gr = new GlideRecord('sys_user');gr.query();while (gr.next()) {gr.setWorkflow(false); //Do not run business rulesgr.autoSysFields(false); //Do not update system fieldsgr.setForceUpdate(true); //Force the updategr.update();}

JavaScript Operators
The following operators can be used in addition to the standard field/value query searching shown above…

OperatorDescriptionCode
=Field value must be equal to the value supplied.addQuery('priority', '=', 3);
>Field must be greater than the value supplied.addQuery('priority', '>', 3);
<Field must be less than the value supplied.addQuery('priority', '<', 3);
>=Field must be equal to or greater than the value supplied.addQuery('priority', '>=', 3);
<=Field must be equal to or less than the value supplied.addQuery('priority', '<=', 3);
!=Field must not equal the value supplied.addQuery('priority', '!=', 3);
STARTSWITHField must start with the value supplied. The example shown on the right will get all records where the short_description field starts with the text 'Error'.addQuery('short_description', 'STARTSWITH', 'Error');
ENDSWITHField must end with the value supplied. The example shown on the right will get all records where the short_description field ends with text 'Error'.addQuery('short_description', 'ENDSWITH', 'Error');
CONTAINSField must contain the value supplied anywhere in the field. The example shown on the right will get all records where the short_description field contains the text 'Error' anywhere in the field.addQuery('short_description', 'CONTAINS', 'Error');
DOES NOT CONTAINField must not contain the value supplied anywhere in the field. The example shown on the right will get all records where the short_description field does not contain the text 'Error' anywhere in the field.addQuery('short_description', 'DOES NOT CONTAIN', 'Error');
INField must contain the value supplied anywhere in the string provided.addQuery('sys_id', 'IN', '0331ddb40a0a3c0e40c83e9f7520f860,032ebb5a0a0a3c0e2e2204a495526dce');
INSTANCEOFRetrieves only records of a specified class for tables which are extended. For example, to search for configuration items (cmdb_ci table) you many want to retrieve all configuration items that are have are classified as computers. The code uses the INSTANCEOF operator to query for those records.addQuery('sys_class_name', 'INSTANCEOF', 'cmdb_ci_computer');

GlideRecord Query Cheat Sheet - ServiceNow Guru (1)

Mark Stanger

Date Posted:

May 20, 2021

Share This:

49 Comments

  1. GlideRecord Query Cheat Sheet - ServiceNow Guru (2)

    ToddFebruary 23, 2010 at 4:05 am

    Excellent Cheat Sheet! More more…..

  2. GlideRecord Query Cheat Sheet - ServiceNow Guru (3)

    CesarFebruary 23, 2010 at 3:43 pm

    Thanks Mark! this is helpful.

  3. GlideRecord Query Cheat Sheet - ServiceNow Guru (4)

    Ivan MartezApril 15, 2010 at 1:54 pm

    Great Cheat Sheet and an excellent website!!

  4. GlideRecord Query Cheat Sheet - ServiceNow Guru (5)

    ChrisSeptember 9, 2010 at 12:22 am

    Is there a way to query for a date ? Such as current.addQuery(‘opened_at’, ‘<=', new Date()-7); Although this does not work is there a way to do something like this to query for a given date range ?

    • GlideRecord Query Cheat Sheet - ServiceNow Guru (6)

      Mark StangerSeptember 9, 2010 at 12:24 am

      There is. I usually use the ‘addEncocdedQuery’ method when dealing with date queries. You can build the query you want in a module or filter definition to see what the encoded query should look like.

  5. GlideRecord Query Cheat Sheet - ServiceNow Guru (7)

    BennyNovember 4, 2010 at 4:22 pm

    Thanks Mark!

    I’ve scoured the SN wiki and this is a better summary of their glide record pages. I’m a newbie to SN and I must admit I’ve found the learning curve a bit steep for customising SN, having come from other SaaS systems as Salesforce & Rightnow.

    These systems let you use GUI’s to do most things and for more advanced customisation there is a scripting option. SN seems to be all scripting from the get go with limited use of GUIs for configuration. It would be nice if their wiki included a clearer explanation on how Client Scripts, UI Policies, UI Actions, Business Rules & Access Control all fits together…

    • GlideRecord Query Cheat Sheet - ServiceNow Guru (8)

      Mark StangerNovember 4, 2010 at 4:33 pm

      Thanks for the comment. I think as you become more familiar with Service-now you’ll see that the majority of configurations are GUI-based or require some pretty light scripting. I’m not that familiar with Salesforce & Rightnow so I couldn’t say how Service-now compares. Hopefully some of the content here helps you to get going a little bit faster. I know that the SNC documentation team is really focusing right now on scripting documentation so what you see on the wiki should be getting better by the day.

      You might check out these forum links for some more information about SNC scripting basics. It gives some information from presentations I’ve given in the past about how some of this stuff connects. http://community.service-now.com/forum/3480 http://community.service-now.com/forum/3613

  6. GlideRecord Query Cheat Sheet - ServiceNow Guru (9)

    GabeFebruary 11, 2011 at 3:10 pm

    Here’s one for MultipleDelete, should be a good addition to this post.

    //Find all non-active incident records and delete themvar md = new Packages.com.glide.db.MultipleDelete('incident');md.addQuery('active', false);md.setAllowCascadeDelete(); // optional: Allow cascading to records in other tables related to records being deletedmd.execute();
  7. GlideRecord Query Cheat Sheet - ServiceNow Guru (10)

    bdr529March 31, 2011 at 8:56 am

    *Fantastic* posting, Mark! Great to have all of these listed together – thanks!

  8. GlideRecord Query Cheat Sheet - ServiceNow Guru (11)

    priscilla.yuenAugust 5, 2011 at 1:02 am

    In reference to the Or query – is there any documentation about what a QueryCondition object is, and what its other functions are?

    • GlideRecord Query Cheat Sheet - ServiceNow Guru (12)

      Mark StangerAugust 5, 2011 at 6:35 am

      @priscilla, there isn’t any other documentation about QueryCondition, but there’s really not much more to it either. The only real purpose of it is to enable you to add an ‘Or’ condition to a GlideRecord query. There are a couple more examples that I could probably share though. I’ll see if I can get something out next week.

  9. GlideRecord Query Cheat Sheet - ServiceNow Guru (13)

    Jim CoyneSeptember 10, 2011 at 1:33 pm

    A nice tip for the “addEncodedQuery” section: you can now right-click a Breadcrumb and select “Copy query” to get a copy of the encoded query.

  10. GlideRecord Query Cheat Sheet - ServiceNow Guru (14)

    MikeOctober 10, 2011 at 2:15 pm

    Is there a way to get the display value from a SYS ID returned in a query?

    example: I would like the below code to result in the display name for the requested_by and not the sys_id used to reference the user table.

    var Req;var gr = new GlideRecord("change_request");gr.addQuery('number', current.sysapproval);gr.query();while(gr.next()) { Req = gr.requested_by; }
    • GlideRecord Query Cheat Sheet - ServiceNow Guru (15)

      Mark StangerOctober 10, 2011 at 4:52 pm

      Since you’re dealing with a reference field you should be able to do something like this inside your while loop…

      Req = gr.requested_by.getDisplayValue();

      You can also just dot-walk to the field that contains the display value

      Req = gr.requested_by.name;
      • GlideRecord Query Cheat Sheet - ServiceNow Guru (16)

        MikeOctober 11, 2011 at 9:39 am

        Thanks for the suggestions,
        I’ve tried both and have listed the results of each below.

        Req = gr.requested_by.name;

        Returned the value “undefined”

        Req = gr.requested_by.getDisplayValue();

        Resulted in a script error “Object doesn’t support this property or method”

        • GlideRecord Query Cheat Sheet - ServiceNow Guru (17)

          Mark StangerOctober 12, 2011 at 9:18 am

          You’ve got another problem in your code…

          gr.addQuery('number', current.sysapproval);should be...gr.addQuery('sys_id', current.sysapproval);
          • GlideRecord Query Cheat Sheet - ServiceNow Guru (18)

            MikeOctober 12, 2011 at 9:45 am

            Ah, you are correct. I have corrected the query. Still, very odd when I use the following:

            Req = gr.requested_by;

            I get the sys_id that is in that reference field

            When using

            Req = gr.requested_by.name;

            I still get “undefined”.

            I will keep working at it. Thanks for your time and help.

  11. GlideRecord Query Cheat Sheet - ServiceNow Guru (19)

    AndreasJune 28, 2012 at 8:44 am

    Another nice addition to this list would be applyEncodedQuery
    see: http://community.servicenow.com/forum/5356

  12. GlideRecord Query Cheat Sheet - ServiceNow Guru (20)

    MicheleAugust 3, 2012 at 9:16 am

    I find the encodedquery to be extremely helpful especially when my query includes things like created this week or created before a specific date. Then I know for sure I have the right query string.

    Question: If I run a GlideRecord query and then want to add another condition to the query and rerun it, is that possible?
    Not that I would do the following but just as an example. The real code I’m doing has quite a few query conditions and I’m rebuilding the same query multiple times just to add one more condition and it just seems inefficient.

    EX:
    var newArray = new Array();
    var newArray2 = new Array();
    var gr = new GlideRecord(‘incident’);
    gr.addQuery(‘active’, true);
    gr.query();

    while(gr.next()){
    if (gr.severity = ‘1’){
    newArray.push(gr.number);
    }

    //I want to add to the above query that incident state = 6…

    gr.addQuery(‘incident_state’, ‘6’);
    gr.query();

    while (gr.next()){
    newArray2.push(gr.number);
    }

  13. GlideRecord Query Cheat Sheet - ServiceNow Guru (21)

    Amado SierraSeptember 21, 2012 at 7:31 am

    One thing to note about updating a reference field to null is that it has to be done as described here:

    Example 1

    var gr1 = new GlideRecord(‘incident’);
    gr1.query();
    while(gr1.next()) {
    gr1.priority = “NULL”;
    gr1.update();
    }

    Source: http://wiki.servicenow.com/index.php?title=Setting_a_GlideRecord_Variable_to_Null

  14. GlideRecord Query Cheat Sheet - ServiceNow Guru (22)

    Kari SweeneyNovember 28, 2012 at 8:55 am

    Thanks so much for posting this info -it is really helpful!. We are struggling with using AddQuery and AddORCondition to create an advanced query. Here is an example of what we wre trying to accomplish..

    (Where Priority is 1
    AND IncidentState =6)

    OR

    (Where Priority = 2
    AND Category = Software)

    Is there a way to accomplish this?

    thanks,
    Kari

    • GlideRecord Query Cheat Sheet - ServiceNow Guru (23)

      Mark StangerNovember 28, 2012 at 4:09 pm

      I struggle with ‘AddOrCondition’ sometimes too. I’ve found it helpful to use ‘AddEncodedQuery’ to manage those types of complex queries more easily. By using that method, you can simply build the query filter in a standard list so that you can see exactly what you want, then right-click the breadcrumb and select ‘Copy query’. Then you can just add that as an encoded query and not have to worry about the correct ‘AddOrCondition’ setup.

    • GlideRecord Query Cheat Sheet - ServiceNow Guru (24)

      MikaMay 8, 2014 at 11:56 am

      I’m also interested in doing nested AND conditions. Did you ever determine a way to do it. I’d prefer using an encoded query if possible. The only other way I can think of doing it is to create two separate queries and then combine the results (not very pretty but easy enough I suppose).

  15. GlideRecord Query Cheat Sheet - ServiceNow Guru (25)

    Martin RobinsonOctober 7, 2013 at 6:45 am

    Nice one Mark, thanks for sharing. Much appreciated.

  16. GlideRecord Query Cheat Sheet - ServiceNow Guru (26)

    JoeOctober 24, 2014 at 6:44 am

    I’m having trouble with setWorkflow. I’ve used it successfully before, but now it seems to be cancelling the update that it precedes. in a script action (parm2 = sys_id of an inc and parm1 = display value of an assignment group), I have:

    var outage = new GlideRecord(‘incident’);
    outage.get(event.parm2);

    outage.work_notes = “Outage originally assigned to ” + event.parm1;
    outage.setWorkflow(false);
    outage.update();

    If I comment out “outage.setWorkflow(false),” I will see the update to the work notes. However, if i leave the line as is, i wont see the update. I definitely do not want to trigger other business rules after this update, so im not sure what’s happening here. Anyone have any thoughts?

  17. GlideRecord Query Cheat Sheet - ServiceNow Guru (27)

    Jef De CosterMarch 20, 2015 at 5:01 am

    Hi Joe,

    Don’t know if it’s still relevant, but I had the same issue.
    Apparantly this is normal behaviour when using setWorkflow(false), the work_notes won’t be updated.

    I’ve found a nice script include and a way to set the work notes alltogether even if setWorkflow(false) is applied.

    I found it on the SNBlog, the author is Stefan Bohncke.

    You can find it using this url:
    http://www.snc-blog.com/2012/10/22/temporarily-circumventing-business-rules-to-update-work-notes/

    Hopefully this is helping you out?
    It worked for me ;-)

    KR,
    Jef

  18. GlideRecord Query Cheat Sheet - ServiceNow Guru (28)

    JMOJuly 15, 2015 at 9:09 am

    I use this page quite a bit and just recently found out ServiceNow also offers a “NOT IN” operator, which has saved me several times.

  19. GlideRecord Query Cheat Sheet - ServiceNow Guru (29)

    Rajan MurkuteNovember 3, 2015 at 9:41 pm

    This cheat sheet covers the most frequently used GlideRecord operations. All explanations and examples are easy to follow. It saved me huge amount of time which I may have spent looking through the Service-Now Wiki pages.

  20. GlideRecord Query Cheat Sheet - ServiceNow Guru (30)

    CodeHackerMarch 22, 2016 at 6:51 am

    Thanks.. Very helpfull

  21. GlideRecord Query Cheat Sheet - ServiceNow Guru (31)

    DougAugust 24, 2016 at 3:25 pm

    One thing i would like to see added Mark

    would be how to gs.print/alert the current query.

    • GlideRecord Query Cheat Sheet - ServiceNow Guru (32)

      Mark StangerAugust 24, 2016 at 4:13 pm

      Hey Doug, I’m not sure exactly what you’re asking for here. Can you describe the scenario or area of the tool where this would be used?

    • GlideRecord Query Cheat Sheet - ServiceNow Guru (33)

      Steve McDonaldJune 1, 2018 at 6:44 am

      I know this was asked a long time ago but here is how you print the current query:
      gs.print(gr.getEncodedQuery());

      Returned:
      numberSTARTSWITHINC^stateIN1,2^sys_updated_on<=2018-06-08 06:59:59

      My query is:

      var gr = new GlideRecord('task');
      gr.addQuery('number', 'STARTSWITH', 'INC');
      gr.addQuery('state', 'IN', '1,2');
      gr.addQuery('sys_updated_on', '<=', endDate);
      gr.query();

  22. GlideRecord Query Cheat Sheet - ServiceNow Guru (34)

    Peter ForemanAugust 31, 2016 at 11:38 pm

    HI Mark,
    I want to build a a Before Display Query and wanted:
    the conditions to be (A and B) or (C and D) or (E and F) and found
    current.addQuery(A)
    var qc = current.addQuery(B)
    qc.addOrCondition(C)
    ….
    with lots of variations on the mix of .addQuery() .addOrCondition() would not retrieve the correct data.
    So I created an addEncodedQuery() and it produced the correct data on the display list but any further filtering on the list is ignored. Is there a solution to these nested conditions, I am currently on Fuji and look to upgrade if required?
    Peter

    • GlideRecord Query Cheat Sheet - ServiceNow Guru (35)

      Mark StangerSeptember 1, 2016 at 7:45 am

      Hey Peter, this question is probably better suited for ServiceNow support or the ServiceNow community. I would generally use ‘addEncodedQuery’ for these types of complex queries and I’ve had good success with that in the past. I’m not sure why exactly it’s not working as expected in your case.

  23. GlideRecord Query Cheat Sheet - ServiceNow Guru (36)

    TejuApril 10, 2017 at 12:58 am

    Lovely Cheat Sheet. Thanks Mark.

  24. GlideRecord Query Cheat Sheet - ServiceNow Guru (37)

    Nicole McCrayApril 21, 2017 at 6:38 am

    I need a script that will designate a Manager (‘u_l4_manager_id’) as the approver in my workflow, based on the person the request is for (request.requested_for).

    I’ve tried:

    var approver = new GlideRecord(‘sys_user’);

    approver.addQuery(‘sys_id’, current.requested_for);

    approver.query();

    if (approver.next()){

    answer = approver.u_l4_manager_id;

    }

    But this is not working. How can I reference the manager id to return the Manager name as the Approver?

  25. GlideRecord Query Cheat Sheet - ServiceNow Guru (38)

    SSJune 20, 2017 at 7:56 am

    ‘get’ was exactly what I needed. Thanx!

  26. GlideRecord Query Cheat Sheet - ServiceNow Guru (39)

    SimonJune 29, 2017 at 2:21 am

    There are some queries that doesnt seem to be in this post which is very nice to have.
    With Service portal – async GR is very wanted

    var gr = new GlideRecord('table');gr.addQuery('active', true);gr.query(function(gr){//Parse GR to an async callwhile(gr.next){//do something}});

    Theres also this

    var gr = new GlideRecord('table');gr.query('sys_id', , function(gr){if(gr.next(){//Do something async here}});
    • GlideRecord Query Cheat Sheet - ServiceNow Guru (40)

      Jim PiselloJuly 13, 2017 at 8:21 am

      Thanks for the suggestions Simon!

  27. GlideRecord Query Cheat Sheet - ServiceNow Guru (41)

    Victor OlufowobiJuly 19, 2017 at 1:41 pm

    Hi Simon,

    Thanks very much for your suggestions – I am very much intrigued with the capabilities of these constructions. Could you please demonstrate how they could be used?

    Regards,

    Victor Olufowobi

    • GlideRecord Query Cheat Sheet - ServiceNow Guru (42)

      SimonJanuary 17, 2018 at 11:42 pm

      Hi Victor

      This kinda GlideRecords should only be used Client side as the new Service portal doesnt allow sync calls.
      On the other hand side – best practice is using GlideAjax for Client -> Server -> Client calls.

  28. GlideRecord Query Cheat Sheet - ServiceNow Guru (43)

    SHILPA JAINAugust 25, 2017 at 8:21 pm

    Hi Mark,

    I tried your code to insert an incident record, it is inserting some 100 record when creating one record. something happening when it’s calling insert.

    • GlideRecord Query Cheat Sheet - ServiceNow Guru (44)

      Mila MoralesAugust 30, 2017 at 9:57 am

      Hi Shilpa,
      Could you show us the script you used so we could take a look at it.

  29. GlideRecord Query Cheat Sheet - ServiceNow Guru (45)

    Daniel OderbolzOctober 4, 2017 at 3:01 am

    Hi

    Thanks for this great resource}
    One thing I think this lacks is the

    updateWithReferences()

    http://wiki.servicenow.com/index.php?title=Inserting/Updating_GlideRecord_with_References

    Method or in general, discussion of how to update reference fields (or insert new references).
    Could this be added?

  30. GlideRecord Query Cheat Sheet - ServiceNow Guru (46)

    JithinOctober 31, 2017 at 10:49 pm

    Very Helpful Thanks Mark..

  31. GlideRecord Query Cheat Sheet - ServiceNow Guru (47)

    SimonJanuary 17, 2018 at 11:38 pm

    Hi Mark

    Together with setWorkflow(), autoSysFields() and setForceUpdate() theres actually 1 more “hidden” method.
    gr.setUseEngines(false); //Do not evaluate data policies

  32. GlideRecord Query Cheat Sheet - ServiceNow Guru (48)

    Jawad FIKRI BENBRAHIMdNovember 22, 2018 at 8:08 am

    Hell guys,

    Your document is awesome as it gathers a load of informations about server side queries together.

    Best regards
    FIKRI BENBRAHIM Mohamed Jawad

  33. GlideRecord Query Cheat Sheet - ServiceNow Guru (49)

    Jing YinOctober 24, 2019 at 8:30 am

    Hi Mark, very helpful. Thanks for sharing!

  34. GlideRecord Query Cheat Sheet - ServiceNow Guru (50)

    Suzanne SwansonSeptember 1, 2020 at 4:26 pm

    It’s 2020 and I’m still using this resource!!
    Thanks!!!

Comments are closed.

  • GlideRecord Query Cheat Sheet

    May 20th, 2021

  • User Object Cheat Sheet

    June 23rd, 2021

  • May 20th, 2021

Categories

  • Announcements
  • Architecture
  • Authors
  • Business rules
  • Client scripts
  • CMDB
  • Content management
  • Design
  • Email Notifications
  • General knowledge
  • Generative AI
  • Graphical workflow
  • Imports
  • Integration
  • Knowledge Management
  • Miscellaneous
  • Performance
  • Relationships
  • Releases
  • Reporting
  • Script includes
  • Scripting
  • Service Portal
  • Single Sign-on
  • System Definition
  • System UI
  • UI actions
  • UI macros
  • UI pages
  • UI scripts
  • Web Services

Tags

ACLsAJAXApprovalsAttachmentsBusiness rulesCatalog client scriptsChange managementClient scriptsCMDBConfiguration managementDatesDictionaryEmail notificationsExportGlideDialogWindowGlideRecordGraphical workflowhighlightsHomepagesIncident managementIntegrationKnowledge BaseList collectorModulePopupProblem managementReference fieldRelated listsReportingScript includesScriptingSecurityService catalogSlushbucketSystem SecurityTrendingUI actionsUI macrosUI pagesUI policyUI scriptsUpdate SetsVariablesViewsWidgets

Related Posts

GlideRecord Query Cheat Sheet - ServiceNow Guru (55)

Leveraging User Criteria in your custom applications

June 3rd, 2024|6 Comments

GlideRecord Query Cheat Sheet - ServiceNow Guru (56)

User Object Cheat Sheet

June 23rd, 2021

GlideRecord Query Cheat Sheet - ServiceNow Guru (57)

Generate a GlideRecord Query for a List

August 14th, 2014

Fresh Content
Direct to Your Inbox

Just add your email and hit subscribe to stay informed.

GlideRecord Query Cheat Sheet - ServiceNow Guru (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 5958

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.