Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

GlideRecord

Sofvrg
Tera Contributor

Is it right? Here is a GlideRecord script. Find the error, correct it, and explain:Screenshot 2024-12-09 alle 11.03.33.png

 

The script is incorrect because you need to add gr.query(). Here is the correct script:

var gr = new GlideRecord('task');
gr.addQuery('active', true);
gr.query();
while (gr.next()) {
gs.print(gr.getValue('description'));
}

Explanation:

  • var gr = new GlideRecord('task'); defines the variable gr, which represents a record from the "Task" table.
  • gr.addQuery('active', true); filters the records to only include those where the "Active" field is set to true.
  • gr.query(); retrieves the records that match the filter criteria.
  • while (gr.next()) { iterates through each record returned by the query.
  • gr.next() is necessary to move to the next record each time the loop is executed.
  • Finally, gs.print(gr.getValue('description')); prints the value of the "description" field for each record.
1 REPLY 1

Nikhil Bajaj9
Tera Sage

Hi @Sofvrg ,

 

It is correct as gr.query() is the method to run the filter/ query which is used in Line 2. gr.query is equivalent to Run button on Filter/ condition builder on any list view.

 

Regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj

ServiceNow Rising Star-2025