Scheduled job is creating a request without any RITM or SCTASK

Mallika Bhupath
Tera Contributor

All,

 

I have a scheduled job that is currently running every day just past midnight. This job is creating an empty request without any RITM or SCTASK associated. The requestor too is currently an inactive user. 

 

Is there a way to ensure no request gets triggered when this scheduled job is running? 

Please find the Script include below.

MallikaBhupath_0-1699372261636.png

 

 

Script:

var RequestTimeCardUtils = Class.create();
RequestTimeCardUtils.prototype = {
    initialize: function() {
    },
 
    //When a timecard against a SCTask is processed, update REQ field "Approved Time Card Sum"
    updateRequestTotalTimeWorked: function(){
 
        var sum, timeCardsToSum, grReq, grTimeCards;
        var reqArr = [];
        
        //First, find all REQ records with timecards against their SCTASKS
        grTimeCards = new GlideRecord("time_card");
        grTimeCards.addEncodedQuery('stateINApproved,Processed^task.sys_class_name=sc_task');
        grTimeCards.query();
 
        while(grTimeCards.next()){
            if(reqArr.indexOf(grTimeCards.task.request.sys_id.toString()) == -1){
                reqArr.push(grTimeCards.task.request.sys_id.toString());
            }
        }
 
        //GlideAggregate to SUM all time cards for the found REQs
        for(i=0;i<reqArr.length;i++){
            timeCardsToSum = new GlideAggregate("time_card");
            timeCardsToSum.addEncodedQuery("state=Approved^ORstate=Processed^task.sys_class_name=sc_task^task.ref_sc_task.request.sys_id=" + reqArr[i]);
            timeCardsToSum.addAggregate('SUM', 'total');
            timeCardsToSum.setGroup(false);
            timeCardsToSum.query();    
 
            if(timeCardsToSum.next()){
                sum = timeCardsToSum.getAggregate('SUM', 'total');
 
                if(sum !== null){
                    grReq = new GlideRecord("sc_request");
                    grReq.get(reqArr[i]);
 
                    grReq.u_approved_time_card_sum = sum;
                    grReq.setWorkflow(false);
                    grReq.autoSysFields(false);                
                    grReq.update();
                }
            }
        }
 
        //Next, find REQs with time card totals but the time cards have since been deleted
        grReq = new GlideRecord("sc_request");
        grReq.addEncodedQuery("u_approved_time_card_sum>0");
        grReq.query();
 
        while(grReq.next()){
//gs.log("Arr is: " + reqArr,"KZ");
//gs.log("REQ is: " + grReq.sys_id.toString(),"KZ");
 
            if(reqArr.indexOf(grReq.sys_id.toString()) == -1){
                grReq.u_approved_time_card_sum = 0;
                grReq.setWorkflow(false);
                grReq.autoSysFields(false);                
                grReq.update();
            }
        }
    },
};

Scheduled job

MallikaBhupath_1-1699372375291.png

Script for Scheduled job :

calculateApprovedTime();
 
function calculateApprovedTime(){
new global.RequestTimeCardUtils().updateRequestTotalTimeWorked();
}
 
Requests triggering every day past midnight
MallikaBhupath_2-1699372580422.png


Any help would be appreciated.

 

Thanks,

Mallika



1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

Can you re-create this by running the scheduled job manually and seeing an empty REQ?  There's nothing in this script that is doing a insert() on a GlideRecord of the sc_request table, so perhaps it's a different job, or a Business Rule that is triggered by an update in this script, even though you have setWorkflow(false)?