Track RITM count submitted on a particular catalog item

MayrelCerero
Tera Expert

Hello, I have been struggling to develop a script that will give me the total RITM count where the user has been the "who is this request for" for a replacement card. Then, I need to store that count into a variable (Replacement Card Count) that is not  visible on the submission form. I also don't want to include RITMS whose state is Closed Skipped or Closed Incomplete. I added the script to a Run Script activity within the workflow since the RITM count will not be visible on the form. Here is what I have so far, but I have yet to be successful...

replacementCardCount();

var currentUser = current.variables.who_is_this_request_for;
var catalogItemSysID = gs.getProperty("x_g_nel2_pivi.request.replacement.pivi.request");
var ritmCount = 0;

function replacementCardCount() {
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('sc_cat_item', catalogItemSysID);
ritmGR.addQuery('who_is_this_request_for', currentUser);
ritmGR.addQuery('state', 'NOT IN', '7,8');
ritmGR.query();

while (ritmGR.next()) {
ritmCount++;
ritmGR.setValue('replacement_card_count', ritmCount);
}
ritmGR.update();
}

 

MayrelCerero_0-1710794652786.png

 

1 ACCEPTED SOLUTION

MayrelCerero
Tera Expert

This is the code that I ended up using for the requirement. I hope this can help anyone who had a similar issue:      var catalogItemSysID = gs.getProperty("x_g_nel2_pivi.request.replacement.pivi.request");
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('cat_item', catalogItemSysID);
ritmGR.addQuery('request.requested_for', current.variables.who_is_this_request_for);
ritmGR.query();
if (ritmGR.next()) {
current.variables.replacement_card_count = ritmGR.getRowCount();
ritmGR.update();
}

View solution in original post

3 REPLIES 3

Harsh Vardhan
Giga Patron

@MayrelCerero  I would suggest you to add logs in script to validate how many count its giving you.

Also addQuery for item column is wrong, change it to cat_item

ritmGR.addQuery('cat_item', catalogItemSysID);

  

 

Thanks,

Harsh

James Chun
Kilo Patron

Hey @MayrelCerero,

 

Shouldn't you be updating the 'replacement_card_count' of the current record?

If so, try the following script:

 

replacementCardCount();

function replacementCardCount() {

    var currentUser = current.variables.who_is_this_request_for;
    var catalogItemSysID = gs.getProperty("x_g_nel2_pivi.request.replacement.pivi.request");

    var ritmGR = new GlideAggregate('sc_req_item');
	ritmGR.addAggregate('COUNT');
    ritmGR.addQuery('cat_item', catalogItemSysID);
    ritmGR.addQuery('variables.who_is_this_request_for', currentUser);
    ritmGR.addQuery('state', 'NOT IN', '7,8');
    ritmGR.query();

    if (ritmGR.next()) {
		current.variables.replacement_card_count = ritmGR.getAggregate('COUNT');
	}
}

 

 

Cheers

MayrelCerero
Tera Expert

This is the code that I ended up using for the requirement. I hope this can help anyone who had a similar issue:      var catalogItemSysID = gs.getProperty("x_g_nel2_pivi.request.replacement.pivi.request");
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('cat_item', catalogItemSysID);
ritmGR.addQuery('request.requested_for', current.variables.who_is_this_request_for);
ritmGR.query();
if (ritmGR.next()) {
current.variables.replacement_card_count = ritmGR.getRowCount();
ritmGR.update();
}