I need to get ritm related task numbers i tried below script but no luck please check

mani55
Tera Contributor

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());
2 ACCEPTED SOLUTIONS

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn

View solution in original post

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.

View solution in original post

15 REPLIES 15

Hi Mark

 

i am using below code everything is working but state query is not working properly please let me know the mistake

 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 grCatalogTask = new GlideRecord('sc_task');
grCatalogTask.addQuery('request_item.requested_for', userName);
grCatalogTask.addQuery('request_item.state','IN','1,2,9');
//grCatalogTask.addQuery('state','1');
grCatalogTask._query();

while(grCatalogTask._next()) {
    gs.log(grCatalogTask.number);


        RIM.push({
            approval: grCatalogTask.request_item.approval.getDisplayValue(),
            state: grCatalogTask.request_item.state.getDisplayValue(),
            number: grCatalogTask.request_item.getDisplayValue(),
            email: utils.user.email, // Assuming utils.user.email is the correct way to get the user's email
            assignee: grCatalogTask.assigned_to.getDisplayValue() // Array of tasks for this RITM
        });
    }

    return {
        rim: request.queryParams.user_id[0],
        state: RIM
    };