- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2024 10:22 PM
I need to get ritm related task numbers i tried below script but no luck please check and let me know my mistake
var userName='33eec4bddbe6c090aeb79414db961977';
gs.print('the data'+userName);
var grRim = new GlideRecord('sc_req_item');
grRim.addQuery('requested_for.sys_id', userName);
grRim.addQuery('stateIN-5,1,2,9');
//grRim.addQuery('number', request.queryParams.rim[0]);
grRim.query();
gs.print("Records in incident table: " + grRim.getRowCount());
var test = new GlideRecord('sc_task');
test.addQuery('request_item',grRim.sys_id);
test.addQuery('state', '1');
gs.print("Records in task table: " + test.getRowCount());
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2024 10:43 PM
I do see responses from others with also multiple GlideQueries. Like I mentioned, not nessary.
For example basic code (rest you can surely add yourself) which works (= tested):
var grCatalogTask = new GlideRecord('sc_task');
grCatalogTask.addQuery('request_item.requested_for', userName);
grCatalogTask._query();
while(grCatalogTask._next()) {
gs.info(grCatalogTask.number);
}
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2024 10:43 PM - edited ‎01-29-2024 10:45 PM
Hi @mani55
There are several issues with the updated script you've provided. Let's address them one by one:
1. The `stateIN-5,1,2,9` query is incorrect due to the hyphen. It should be `stateIN5,1,2,9` or better yet, use the correct syntax `stateIN 5,1,2,9`.
2. You are trying to use `grRim.number` in the `test` GlideRecord query, but this will not work because `grRim` is a GlideRecord object that can contain multiple records. You need to iterate over the results of `grRim` and use the `sys_id` of each RITM record to query the `sc_task` table.
3. The `test` GlideRecord is being queried before the `grRim` GlideRecord has been iterated, which means you are not actually querying for tasks related to each RITM.
4. The `test.getRowCount()` is being called before `test.query()`, so it will not return the correct count.
5. The `assignee` field in the `RIM` array is being set to `test.assigned_to`, which is not valid because `test` is a GlideRecord object that can contain multiple records. You need to iterate over the results of `test` to get the `assigned_to` value for each task.
6. The `user.email` field is not defined in the provided script. You need to ensure that the `user` object is properly initialized and contains the `email` property.
Here's a revised version of the script that should work:
var RIM = [];
var userid = request.queryParams.user_id[0];
var utils = new u_CAPApiCatalog(userid);
var userName = utils.user.sys_id;
gs.log('User sys_id: ' + userName);
var grRim = new GlideRecord('sc_req_item');
grRim.addQuery('requested_for', userName);
grRim.addQuery('state', 'IN', '5,1,2,9');
grRim.query();
gs.log("Number of RITMs: " + grRim.getRowCount());
while (grRim.next()) {
var test = new GlideRecord('sc_task');
test.addQuery('request_item', grRim.sys_id);
test.addQuery('state', '1');
test.query();
gs.log("Number of tasks for RITM " + grRim.number + ": " + test.getRowCount());
while (test.next()) {
var assignee = test.assigned_to.getDisplayValue(); // Assuming you want the display value of the assignee
RIM.push({
approval: grRim.approval.getDisplayValue(),
state: grRim.state.getDisplayValue(),
number: grRim.number,
email: utils.user.email, // Assuming utils.user.email is the correct way to get the user's email
assignee: assignee
});
}
}
// Output the RIM array or return it if this is part of a function
gs.log(JSON.stringify(RIM));
This script will:
1. Query the `sc_req_item` table for RITMs requested by the user with the specified `sys_id`.
2. Iterate over each RITM and for each one, query the `sc_task` table for related tasks with a state of '1'.
3. For each task, get the display value of the `assigned_to` field and push the relevant information into the `RIM` array.
4. Log the `RIM` array to the system log.
Make sure to adjust the script according to the actual structure of your `u_CAPApiCatalog` class and the properties of the `utils.user` object.
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2024 10:31 PM
Hi there,
Is this your whole script or only an extract?
Just looking at what you shared, you are performing a .query(), though not a while statement. For example
while(grRim._next()) {
}
Perhaps more mistakes, though that is already the first one.
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2024 10:32 PM
Also your GlideRecord query for "test" is incomplete. You just stopped after the addQuery? Or perhaps you didn't paste the full code here?
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2024 10:33 PM - edited ‎01-29-2024 10:34 PM
I do wonder, do you actually need two queries? Can this not be done in one using dotwalking (yes it can 😀)?
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-29-2024 10:34 PM
And just this to add:
grRim.addQuery('requested_for.sys_id', userName);
Dotwalking to the sys_id is a bad practice. Just remove the .sys_id here.
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field