Req state update

GBS
Tera Contributor

I'm using the below script to update the Request state and RITM stage based on the RITM state. It is working fine for individual RITMs but not for order guide. Can anyone help me on this 

 

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

    var requestNumber = GlideRecord('sc_request');
    requestNumber.addQuery('sys_id', current.request);
    requestNumber.addQuery('active', true);
    requestNumber.query();
    if (requestNumber.next()) {
        var requestItemCount = new GlideAggregate('sc_req_item');
        requestItemCount.addQuery('request', requestNumber.sys_id);
        requestItemCount.addQuery('active', true);
        requestItemCount.addAggregate('COUNT');
        requestItemCount.query();
        if (requestItemCount.next()) {
            gs.info('RITM Count ' + requestItemCount.getAggregate('COUNT'));
            if (requestItemCount.getAggregate('COUNT') == 0) {

                if (current.state == 3) {
                    requestNumber.request_state = 'closed_complete';
                    if (current.stage != 'complete') {
                        current.stage = 'complete';
                        current.update();
                    }
                    gs.info('Updating the RITM stage1 ' + current.stage + current.number);

                } else if (current.state == 4) {
                    requestNumber.request_state = 'closed_incomplete';
                    if (current.stage != 'closed_incomplete') {
                        current.stage = 'closed_incomplete';
                        current.update();
                    }

                    gs.info('Updating the RITM stage2 ' + current.stage + current.number);
                }

                requestNumber.active = false;
                requestNumber.update();

            }

        }
    }




})(current, previous);
2 REPLIES 2

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @GBS 

 

How will the system update the status, considering there are many RITMs under a single order guide, all linked to one REQ? For example, out of 10 RITMs, 2 are in fulfillment, 2 are completely closed, and the remaining 6 are awaiting approval. How will the system determine the exact status of the REQ in this case?

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Renat Akhmedov
Tera Contributor

Hi,

Thank you for sharing your script it helped me to understand! Based on your description, it seems like the issue occurs specifically when handling Order Guides, where multiple RITMs (Requested Items) are created under the same Request (REQ).

I have checked your script and your script checks if the COUNT of active RITMs is 0, but, Order Guides create multiple RITMs under a single REQ - this means the script might not be updating the Request state correctly when all RITMs are completed. In other words, your script only processes the current RITM but does not verify if all RITMs under the same Request have been closed before updating the Request state.


Hope it was somehow helpful,


Also if you need any scripting help - please feel free to text here or me directly I can help you,


Best regards,
Renat Akhmedov