Is it possible to only choose certain fields in a gliderecord query?

lonesoac01
Giga Guru

I was attempting to do some research on how to optimize a GlideRecord query.  ChatGPT said that when I run:

grIncident.query();
while (grIncident.next()) {}

then all of the record fields are returned in the query and all of those data points are loaded into memory.  That makes sense to me.

 

What does not make sense to me is the setFields method ChatGPT suggested I use to only select the desired fields.  I looked at the GlideRecord documentation and did not see any reference to the setFields method, so it is very likely GPT just hallucinated that method.

    var gr = new GlideRecord('incident');

    // Only retrieve a specific set of fields to reduce load
    gr.setFields(['number', 'short_description', 'priority', 'assigned_to', 'state']);

    // Optionally add filters
    gr.addQuery('active', true);
    gr.setLimit(10); // limit results to 10 records
    gr.orderByDesc('sys_updated_on');

    gr.query();
    while (gr.next()) {
        gs.info('Number: ' + gr.getValue('number'));
        gs.info('Short Description: ' + gr.getValue('short_description'));
        gs.info('Priority: ' + gr.getDisplayValue('priority'));
        gs.info('Assigned To: ' + gr.getDisplayValue('assigned_to'));
        gs.info('State: ' + gr.getDisplayValue('state'));
        gs.info('---');
    }

 

Anyone know if selecting certain fields in a GlideRecord query is even possible?

1 REPLY 1

yesubabu Jonnal
Tera Contributor

HI Lonesoac01, 

 
choose to retrieve only specific fields using the  .setlimit() or .addQuery() options.

but you cannot limit the fields returned directly in the query like DB -MySQL- SQL Or mongo DB. we can manually access only the fields you need after retrieving the record.