Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scheduled job Unable to run this code in scoped as getting error

Pranesh_RP
Tera Contributor
(function() {
    var gr = new GlideRecord('x_nexa2_ienergy_incident_table'); //Instance of Table
    gr.addEncodedQuery('active=true^task_assigneeISNOTEMPTY'); //Conditions
    gr.query();

    while (gr.next()) {

        var currentDate = new GlideDate();
        var EtaBeforeDate = new GlideDate();
        EtaBeforeDate.setValue(gr.estimated_eta);
        EtaBeforeDate.addDaysUTC(-1); // Eta's before date
        var daysToWait = gr.priority === 4 ? 5 : gr.priority; // Event fired based on priority

        if ((!gr.estimated_eta && currentDate >= gr.send_date) ||
            (gr.estimated_eta && EtaBeforeDate >= currentDate)) {

            // Check if it is a bussiness days
            if (currentDate.getDayOfWeekUTC() !== 1 &&
                currentDate.getDayOfWeekUTC() !== 7) {
                var assignees = gr.task_assignee.split(',');
                // Stores user IDs from the list of user in that field
                var user = new GlideRecord('sys_user');
                user.addQuery('sys_id', assignees);
                user.addEncodedQuery('active=true');
                user.query();
                for (var i = 0; user.next(); i++) {

                    if (!gr.estimated_eta) { //User only
                        gs.eventQueue('x_nexa2_ienergy.Demo_1', gr, user.email);
                        if (i === assignees.length - 1) { //Change sendDate
                            var date = new GlideDate();
                            date.addDaysUTC(daysToWait);
                            gr.setValue('send_date', date);
                            gr.update();
                        }
                    } else {
                        if (currentDate > gr.estimated_eta) { //both user & manager
                            gs.eventQueue('x_nexa2_ienergy.Demo_1', gr, user.email, user.manager.email);
                        } else { //Only user
                            gs.eventQueue('x_nexa2_ienergy.Demo_1', gr, user.email);
                        }
                    }

                }
            }

        }

    }

})();
 
output is
javascript eval for GlideDate returned 
javascript eval for GlideDate returned
x_nexa2_ienergy: SW1008720
x_nexa2_ienergy: Bob Hardy
Evaluator.evaluateString() problem: java.lang.RuntimeException: failed to coerce com.glide.script.fencing.ScopedGlideElement to desired type int: com.glide.script.fencing.ScopedFunctionObjectTypeHandler.coerceFunctionArgument(ScopedFunctionObjectTypeHandler.java:175) org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:253) org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2652) org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1518) org.mozilla.javascript.Interpreter.interpret(Interpreter.java:830) org.mozilla.javascript.InterpretedFunction.lambda$call$0(InterpretedFunction.java:160) com.glide.caller.gen.null_null_script.call(Unknown Source) com.glide.script.ScriptCaller.call(ScriptCaller.java:18) org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:159) org.mozilla.javascript.ScriptRuntime.doCall2(ScriptRuntime.java:2734) org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2657) org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1518) org.mozilla.javascript.Interpreter.interpret(Interpreter.java:830) org.mozilla.javascript.InterpretedFunction.lambda$call$0(InterpretedFunction.java:160) com.glide.caller.gen.null_null_script.call(Unknown Source) com.glide.script.ScriptCaller.call(ScriptCaller.java:18) org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:159) org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:597) org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3573) org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:172) com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:397) com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:209) com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:137) com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:348) com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:240) com.glide.script.fencing.GlideScopedEvaluator.evaluateScript(GlideScopedEvaluator.java:219) com.glide.processors.ScriptProcessor.evaluateScript0(ScriptProcessor.java:399) com.glide.processors.ScriptProcessor.lambda$evaluateScriptWithRecordingOption$0(ScriptProcessor.java:382) com.glide.rollback.recording.RollbackRecorder.execute(RollbackRecorder.java:67) com.glide.processors.ScriptProcessor.evaluateScriptWithRecordingOption(ScriptProcessor.java:382) com.glide.processors.ScriptProcessor.evaluateScript(ScriptProcessor.java:362) com.glide.processors.ScriptProcessor.runScript(ScriptProcessor.java:261) com.glide.processors.ScriptProcessor.process(ScriptProcessor.java:219) com.glide.processors.AProcessor.runProcessor(AProcessor.java:677) com.glide.processors.AProcessor.processTransaction(AProcessor.java:288) com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:184) com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:172) com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:51) com.glide.sys.Transaction.run(Transaction.java:2501) com.glide.ui.HTTPTransaction.run(HTTPTransaction.java:27) java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) java.base/java.lang.Thread.run(Thread.java:829)
Please do help
1 REPLY 1

Abbas_5
Tera Sage
Tera Sage

Hello @Pranesh_RP,
Please refer to the below link:
https://www.servicenow.com/community/developer-forum/scheduled-job-not-working-for-a-scoped-app/m-p/...

 

If it is helpful, please mark it as helpful and accept the correct solution.

Thanks & Regards,
Abbas Shaik