Count number of items on a Request

Mickey_Cegon
Tera Expert

I want to try something, and not sure what the best approach would be. Today, we have all approvals for catalog items on the item level. But, we have complaints on the fact that approvers get many approvals for one Request.The current process auto-approves all requests, and requires the individual items to go to our Asset area first, to select the appropriate approver per item, and validate the cost.

 

So, my thought is this:

 

If a catalog request is submitted, and there is only one item on the request, then it just auto-approves the Request, and the approval goes to the item level(current process). If the number of items on the request is greater than one, then the request workflow handles the approval. So, I wanted a way to count the # of items, and put it as an integer on the Request form, so that the Request workflow can use this in an If statement. If count is greater than 1, then the Request moves to our asset team to determine the Request Approver, and get the cost of the individual items documented. Once the Request approval is processed, then the approvals cascade down to the item level, and our item workflow can continue.

 

Does this sound do-able? Do I have a script on the cart, or on Insert of the Request?

 

Thanks!

 

Mickey Cegon

FBL Financial Group, Inc.

6 REPLIES 6

ohhgr
Kilo Sage
Kilo Sage

Hi Mickey,



I'm not aware of any field on the request table.However you can always query the records and get count of items from server side scripts.


If done in a workflow you could simply use workflow.scratchpad variable to store the value.



You could write something like below code snippet in Request workflow,



var reqItems = new GlideRecord("sc_req_item");


reqItems.addQuery("request", current.sys_id);


reqItems.query();


workflow.scratchpad.itemCount = reqItems.getRowCount();



Hope that helps.



Thanks,


Mandar


That worked, thanks!



Mickey


tltoulson
Kilo Sage

Hi Mickey,



I second Mandar's approach with the caveat that the best practice is to avoid using getRowCount on GlideRecords.   It may not matter on this scenario as much since the number of returned items is likely to be small but here is the GlideAggregate alternative:



var reqItems = new GlideAggregate('sc_req_item');


reqItems.addQuery('request', current.sys_id);


reqItems.addAggregate('COUNT');


reqItems.query();


reqItems.next();


workflow.scratchpad.itemCount = reqItems.getAggregate('COUNT');



Edit:   Edited the misspelled getAggregate, thank you for pointing it out so I could correct it!


Thanks, Travis! I did change the script to your version (fixed the miss-spell on "getAggregate" on line 6, in case anyone copies your code). I added a field on my sc_request form that has the count results in it, so I can use it for my If statement. Works great so far, but I'm still struggling to get the approvals to cascade correctly down to the item. There's the BR out there that does it, but in order for the workflows on my Requested Item to kick off, they have to have the rule say that the approval state is "Requested". If I cascade to "Approved", then the individual workflows I have for each item fail. But, with your help, I'm one step closer to getting the results I need!



Mickey Cegon