How to write a While loop to go through records in a task table and generate the number and task type
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2022 03:00 AM
I'm waiting to create a while loop that will go into the 'task' table and generate the number and task type for observation purposes. This is the While loop that I've written so far and will look for task records created in the last week:
var taskRecord = new GlideRecord('task');
taskRecord.addEncodedQuery = ('stateIN3,4,7,8,5,9,103,600,700^active=true');
taskRecord.addEncodedQuery = ('sys_created_onONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()');
taskRecord.query()
taskRecord.setWorkflow(false);
while(taskRecord.next()) {
gs.info("Affected record is " + taskRecord.number + " " + taskRecord.sys_class_name);
}
I was just wondering if somebody could check if my code looks correct or if anything else needs amending.
What I'm also finding is that it takes forever to load and doesn't execute.
Is it also possible to group the results by the sys_class_name?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2022 03:08 AM
Hi,
You can remove the = sign from queries.
var taskRecord = new GlideRecord('task');
taskRecord.addEncodedQuery = ('stateIN3,4,7,8,5,9,103,600,700^active=true');
taskRecord.addEncodedQuery = ('sys_created_onONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()');
taskRecord.query()
taskRecord.setWorkflow(false);
while(taskRecord.next()) {
gs.info("Affected record is " + taskRecord.number + " " + taskRecord.sys_class_name);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2022 03:09 AM
It should be like below
var taskRecord = new GlideRecord('task');
taskRecord.addEncodedQuery('stateIN3,4,7,8,5,9,103,600,700^active=true');
taskRecord.addEncodedQuery('sys_created_onONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()');
taskRecord.query();
while(taskRecord.next()) {
gs.info("Affected record is " + taskRecord.number + " " + taskRecord.sys_class_name);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2022 03:13 AM
Few things here.
- Try to avoid multiple encoded query, what you have here can easily be clubbed into single encoded query.
- Also, you are just querying the records here, so setWorklfowFalse() is not necessary here.
- Also, while fetching records use getValue().
- Your code is tasking time since task tables are very huge in size, so preferable approach would be to slice down your data into more smaller chunks as in target one task table at a time.
Try below code:
var taskRecord = new GlideRecord('task');
taskRecord.addEncodedQuery = ('stateIN3,4,7,8,5,9,103,600,700^active=true^sys_created_onONLast week@javascript:gs.beginningOfLastWeek()@javascript:gs.endOfLastWeek()');
taskRecord.query()
while(taskRecord.next()) {
gs.info("Affected record is " + taskRecord.getValue("number") + " " + taskRecord.getValue("sys_class_name"));
}
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2022 03:20 AM
Hi,
your query formation is wrong.
I would suggest to first apply that filter condition on table list and then copy the query from that filter condition and then use it as encodedQuery
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
