Did My Query Return GlideRecord?

  • getRowCount() method

    var myObj = new GlideRecord('incident');
    myObj.addQuery('caller_id', current.caller_id);
    myObj.query();
    var retRows = myObj.getRowCount();
    gs.log("Returned number of rows = " + retRows);
    
  • GlideAggregate object and methods

    var count = new GlideAggregate('incident');
    count.addQuery('caller_id', current.caller_id);
    count.addAggregate('COUNT');
    count.query();
    var incidents = 0;
    if (count.next()) {
    incidents = count.getAggregate('COUNT');
    }
    gs.log("Returned number of rows = " + incidents);
    

Both of these options return the number of rows meeting the query criteria.

The first option, getRowCount(), is a lot less code to write but has a signaficantly larger performance impact and is not recommended for use with tables with large amounts of data. This strategy retrieves the rows one by one and increments a counter.

The second method, GlideAggregate() is more work for the developer but results in a fast execution. This strategy executes a true COUNT select statement: select COUNT(*) from table_name; This strategy is always recommended for cases where the returned number of rows is greater than 100 and for tables that grow continuously. If in doubt, use this strategy.

See the ServiceNow Developer site for more information on the GlideAggregate API.

results matching ""

    No results matching ""