Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Translate GlideQuery into GlideRecord

Ankita9793
Tera Contributor

Hi All,

 

I want to replace GlideQuery with Gliderecord, can someone please help me with the translation. Specially, with the 'select' query part

 

GlideQuery/ existing script : 

var obj = {}
new GlideQuery('sc_req_item')
    .where('active', true)
    .where('cat_item.name', 'XYZ)
    .where('variables.recertid_new', 'ABC') //this is a catalog field
    .where('approval', 'approved')
    .where('state', 'IN', [1, 2])
    .select(['u_beneficiary.u_country', 'number'])
    .forEach(function(reqItem) {
        gs.info("tetst" + reqItem.number);
        var brambleSource = new GlideQuery('u_country_to_request_type')
            .where('u_bramble_source', 'IN', ['EMEA', 'APAC', 'TURKEY', 'Germany'])
            .where('u_country_code.u_lookup_country', reqItem.u_beneficiary.u_country)
            .selectOne('u_bramble_source')
            .orElse({
                u_bramble_source: null
            });
 
 GlideRecord/ new version: 

Something similar as below 

var obj = {}
var screq = new GlideRecord('sc_req_item');
screq.addQuery('active', true);
screq.addQuery('cat_item.name', 'XYZ')
screq.addQuery('variables.recertid_new', 'ABC') //this is a catalog field
screq.addQuery('approval', 'approved')
screq.addQuery('state', 'IN', [1, 2])
   
    .select(['u_beneficiary.u_country', 'number'])
    .forEach(function(reqItem) {
        gs.info("tetst" + reqItem.number);
        var brambleSource = new GlideRecord('u_country_to_request_type')
            brambleSource.addQuery('u_bramble_source', 'IN', ['EMEA', 'APAC', 'TURKEY', 'Germany'])
             brambleSource.addQuery('u_country_code.u_lookup_country', reqItem.u_beneficiary.u_country)
            .selectOne('u_bramble_source')
            .orElse({
                u_bramble_source: null
            });
2 REPLIES 2

Sebastian R_
Kilo Sage

There is nothing similar as "select" for GlideRecord. With GlideRecord you retrieve the whole record so you can just loop through it (while (screq.next())) and use screq.getValue("<field name>") to access the fields you would get from the select

sangrulkaradvai
Tera Expert
var obj = {};

var screq = new GlideRecord('sc_req_item');
screq.addQuery('active', true);
screq.addQuery('cat_item.name', 'XYZ');
screq.addQuery('variables.recertid_new', 'ABC'); // catalog variable
screq.addQuery('approval', 'approved');
screq.addQuery('state', 'IN', [1, 2]);
screq.query();

while (screq.next()) {

    // Access the fields directly – no .select() in GlideRecord
    var number = screq.getValue('number');
    var country = screq.u_beneficiary.u_country + "";   // dot-walked reference

    gs.info("test " + number);

    // Second query (equivalent to selectOne)
    var brambleSource = new GlideRecord('u_country_to_request_type');
    brambleSource.addQuery('u_bramble_source', 'IN', ['EMEA', 'APAC', 'TURKEY', 'Germany']);
    brambleSource.addQuery('u_country_code.u_lookup_country', country);
    brambleSource.query();

    var result = null;
    if (brambleSource.next()) {
        result = brambleSource.getValue('u_bramble_source');
    }

    // If no match found, result stays null (same as orElse)
    obj[number] = result;
}

GlideRecord can’t use .select() or .selectOne(), so you query normally with while (gr.next()) and access the needed fields directly, then manually check the second table with another GlideRecord query.