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

@mani55 

 

Do you need the list of RITMs assigned to you as a response from your scripted REST API or you need the list of tasks under all the RITMs assigned to you ?

 

Initially, you were looking out to fetch tasks so please confirm.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Hi @mani55 

The issue you're encountering is likely due to the fact that you're pushing the `RIM` object inside the `while (test.next())` loop, which iterates over each task related to the Requested Item (RITM). If a single RITM has multiple tasks associated with it, you will get multiple entries for that RITM, one for each task.

To resolve this, you should aggregate the tasks for each RITM into a single object or an array of tasks within that object. Here's how you can modify your script to include an array of tasks for each RITM:

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    // implement resource here
    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', '1,2,9');
    grRim.query();
    gs.log("Number of RITMs: " + grRim.getRowCount());

    while (grRim.next()) {
        var tasks = [];
        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
            tasks.push({
                task_number: test.number,
                assignee: 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
            tasks: tasks // Array of tasks for this RITM
        });
    }

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

 



In this modified script, for each RITM, we create an array called `tasks` to store the tasks related to that RITM. We then push each task into the `tasks` array within the `while (test.next())` loop. Finally, we push the RITM object, which now includes the `tasks` array, into the `RIM` array. This way, each RITM will be represented once in the output, with its associated tasks nested within it.

Please mark this response as correct or helpful if it assisted you with your question.

But still RITM showing one number in multiple times please find below screeenshot

 

mani55_0-1706773349521.png

 

Amit Verma
Kilo Patron
Kilo Patron

Hi @mani55 

 

Try the below script :

 

 

// Get the RITM from the RITM Number
    var grRim = new GlideRecord('sc_req_item');
    grRim.addQuery('number', 'RITM0000001');
    grRim.addQuery('stateIN-5,1,2,9');
    grRim.query();
// Check if the RITM exists for not and then check for SC Tasks
    if(grRim.next()){
    var test = new GlideRecord('sc_task');
    test.addQuery('request_item',grRim.sys_id);
 // Check for SC Tasks where State is Open
    test.addQuery('state=1');
    test.query();
    gs.print("Records in task table: " + test.getRowCount());
}
    

 

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

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