No. of Ritms generated

servicenow14710
Tera Expert

hello developers,

 I need to calculate how many ritms have been generated by a particular requester/user in a catalog item. Any help is appreciated. 

Thanks!

8 REPLIES 8

Zach Koch
Giga Sage
Giga Sage

Is the requester/user you want to look at in a variable or a field on the sc_req_item table? If it's a variable, you can run a simple script to get the number (which I can help you with if that is the case), if it's a field you can just use the filter on the sc_req_item table.

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

Maddysunil
Kilo Sage

@servicenow14710 

You can write before insert business rule on sc_req_item table , please check the below script:

 

(function executeRule(current, previous /*null when async*/) {

    // Define the catalog item ID and requester/user ID
    var catalogItemId = current.cat_item.toString();
    var userId = current.requested_for.toString();

    // Create a GlideRecord query to count RITMs
    var ritmGr = new GlideRecord('sc_req_item');
    ritmGr.addQuery('cat_item', catalogItemId);
    ritmGr.addQuery('requested_for', userId);
    ritmGr.addQuery('active', true); // Optionally, filter for active RITMs
    ritmGr.query();

    // Count the number of RITMs
    var ritmCount = ritmGr.getRowCount();

 
   gs.info(ritmCount); // you can set it to some field as per requirement

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Muhammed Medni
Tera Guru

Hi @servicenow14710,

 

Try ru this script in Background script

 

var itemSysID = 'catalog item sys_id';
var userSysID = 'requester/user sys_id';

var agg = new GlideAggregate('sc_req_item');
agg.addQuery('cat_item', itemSysID);
agg.addQuery('requested_for', userSysID);
agg.addAggregate('COUNT');
agg.query();
var count = 0;
if (agg.next()) {
    recordCount = parseInt(agg.getAggregate('COUNT'), 10);
}
gs.info('Number of records generated by the requester/user: ' + count);

 

Please mark my answer as helpful if it worked for you.

 

SunilKumar_P
Giga Sage

Hi @servicenow14710, Do you want to show the count of requests submitted by users for an particular catalog item? If yes, you can try creating a report of type Multi-level Pivot Table on sc_req_item table and configure Quantity in Column and Requested for in Row and have the condition as Item is 'Your catalog item'.

 

SunilKumar_P_0-1708698493876.png

 

 

Or

 

If you want to show the count of catalog items submitted by that particular user on the catalog item then you can try with GlideAjax and Onchange catalog client script on catalog item.

 

Regards,

Sunil