While loop is not working

Nisha B
Tera Expert

Hello All,

I have below code in schedule job which is written on Demand.

It is working correctly but evertime when I execute it update only 1 record it should update all the record if condition matches.

 

 var gr = new GlideRecord('incident');
 
        gr.addQuery('active',true);
        gr.addQuery('incident_state''-5');
        gr.addQuery('u_sub_status''Customer Action');
        gr.addQuery('assignment_group.company',"!=",'7c56caaadb79a81022d42de74b961945');
        gr.addQuery('sys_domain',"!=",'e534b9ecdb7b72001d83fd651d9619bb');
      gr.query();
        while (gr.next()) {
            gs.log("Number of incident in pending state customer action ",gr.getRowCount());
            diff= (gs.dateDiff(gr.sys_updated_on, gs.nowDateTime(), true));
            gs.log("Number of incident in pending state customer action difference time "+diff);
             if (diff>= 864000){
                 gs.log("Number of incident in pending state customer action test "+gr.getRowCount());
                gs.log('inside123PD test the record' + gr.number + ' age:' + gr.u_age_days);
                gr.incident_state = '6';
                gr.u_sub_status = 'No User Response';
                gr.close_notes = 'Two attempts have been made to reach the requester over email and no response has been received. Hence, incident has been automatically Resolved by the system after 10 days".';
                gr.close_code = 'No User Response';
                gr.u_resolved_by = 'System';
                gr.u_resolved = gs.nowDateTime();
                gr.active = false;
                gr.update();
                 gs.log("Number of incident in pending state customer action resolved today "+gr.number);
               
            }
        }
 
Can anyone please help why this while loop is not working.
 
Thanks
10 REPLIES 10

@Nisha B 

that's correct. but since you added try catch block even if error is thrown for a record the loop can still run.

try adding query to update only 1 record for which the difference condition matches

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur,

Yes tried that , by using sys_id where the diff is less then 864000 , it didn't update that record , 

If we run the same script in background also it will not run the while loop , not sure where it is going wrong as our instance is domain separated, I have written this in IGBP domain only so that it is applicable for all 

Arun_S1
Tera Guru
Tera Guru

@Nisha B Can you please share the log entries as well?

Hello Arun,

 

I have just checked the error log as the code in try catch- I am getting below message

 

Error while recording sample: java.lang.NullPointerException: com.glide.db.stats.interaction.InteractionPattern.<init>(InteractionPattern.java:27)
com.glide.db.stats.interaction.InteractionSample.getPattern(InteractionSample.java:59)
com.glide.db.stats.VolatileMetricStatsListener.addToNursery(VolatileMetricStatsListener.java:117)
com.glide.db.stats.VolatileMetricStatsListener.observeSample(VolatileMetricStatsListener.java:92)
com.glide.db.stats.interaction.InteractionStats.record(InteractionStats.java:28)
com.glide.ux.metrics.stats.SlowInteractionsMonitoringService.submit(SlowInteractionsMonitoringService.java:23)
com.glide.ux.metrics.ClientMetricsService.addInteraction(ClientMetricsService.java:48)
com.glide.ux.metrics.ClientMetricsConfiguration.lambda$getWiring$1(ClientMetricsConfiguration.java:55)
com.glide.graphql.impl.extensions.schema.transformer.NamespacedResolverTransformer.lambda$transform$0(NamespacedResolverTransformer.java:27)
com.glide.graphql.fetchers.GraphQLTransactionCancelledFetcher.get(GraphQLTransactionCancelledFetcher.java:33)
graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation.lambda$instrumentDataFetcher$0(DataLoaderDispatcherInstrumentation.java:90)
graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation.lambda$instrumentDataFetcher$0(DataLoaderDispatcherInstrumentation.java:90)
graphql.execution.ExecutionStrategy.fetchField(ExecutionStrategy.java:292)
graphql.execution.streaming.StreamingExecutionStrategy.fetchField(StreamingExecutionStrategy.java:39)
graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:222)
graphql.execution.ExecutionStrategy.resolveField(ExecutionStrategy.java:194)
graphql.execution.AsyncSerialExecutionStrategy.lambda$execute$1(AsyncSerialExecutionStrategy.java:46)
graphql.execution.Async.eachSequentiallyImpl(Async.java:80)
graphql.execution.Async.eachSequentially(Async.java:69)
graphql.execution.AsyncSerialExecutionStrategy.execute(AsyncSerialExecutionStrategy.java:41)
graphql.execution.ExecutionStrategy.completeValueForObject(ExecutionStrategy.java:774)
graphql.execution.ExecutionStrategy.completeValue(ExecutionStrategy.java:469)
graphql.execution.ExecutionStrategy.completeField(ExecutionStrategy.java:418)
graphql.execution.ExecutionStrategy.lambda$resolveFieldWithInfo$1(ExecutionStrategy.java:224)
java.base/java.util.concurrent.CompletableFuture.uniApplyNow(CompletableFuture.java:680)
java.base/java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:658)
java.base/java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:2094)
graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:223)
graphql.execution.ExecutionStrategy.resolveField(ExecutionStrategy.java:194)
graphql.execution.AsyncSerialExecutionStrategy.lambda$execute$1(AsyncSerialExecutionStrategy.java:46)
graphql.execution.Async.eachSequentiallyImpl(Async.java:80)
graphql.execution.Async.eachSequentially(Async.java:69)
graphql.execution.AsyncSerialExecutionStrategy.execute(AsyncSerialExecutionStrategy.java:41)
graphql.execution.ExecutionStrategy.completeValueForObject(ExecutionStrategy.java:774)
graphql.execution.ExecutionStrategy.completeValue(ExecutionStrategy.java:469)
graphql.execution.ExecutionStrategy.completeField(ExecutionStrategy.java:418)
graphql.execution.ExecutionStrategy.lambda$resolveFieldWithInfo$1(ExecutionStrategy.java:224)
java.base/java.util.concurrent.CompletableFuture.uniApplyNow(CompletableFuture.java:680)
java.base/java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:658)
java.base/java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:2094)
graphql.execution.ExecutionStrategy.resolveFieldWithInfo(ExecutionStrategy.java:223)
graphql.execution.ExecutionStrategy.resolveField(ExecutionStrategy.java:194)
graphql.execution.AsyncSerialExecutionStrategy.lambda$execute$1(AsyncSerialExecutionStrategy.java:46)
graphql.execution.Async.eachSequentiallyImpl(Async.java:80)
graphql.execution.Async.eachSequentially(Async.java:69)
graphql.execution.AsyncSerialExecutionStrategy.execute(AsyncSerialExecutionStrategy.java:41)
graphql.execution.Execution.executeOperation(Execution.java:177)
graphql.execution.Execution.execute(Execution.java:121)
graphql.GraphQL.execute(GraphQL.java:697)
graphql.GraphQL.lambda$parseValidateAndExecute$12(GraphQL.java:616)
java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1106)
java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2235)
graphql.GraphQL.parseValidateAndExecute(GraphQL.java:611)
graphql.GraphQL.executeAsync(GraphQL.java:578)
graphql.GraphQL.executeStreaming(GraphQL.java:487)
com.glide.graphql.impl.GraphQLEngine.executeOperation(GraphQLEngine.java:102)
com.glide.graphql.impl.streaming.GlideGraphQLStreamingOutput.handleRequest(GlideGraphQLStreamingOutput.java:59)
com.glide.graphql.impl.streaming.GlideGraphQLStreamingOutput.write(GlideGraphQLStreamingOutput.java:45)
com.glide.rest.serializer.impl.ResponseSerializerImpl.serializeStream(ResponseSerializerImpl.java:132)
com.glide.rest.handler.impl.ServiceResultHandlerImpl.serialize(ServiceResultHandlerImpl.java:128)
com.glide.rest.handler.impl.ServiceResultHandlerImpl.processServiceResultBody(ServiceResultHandlerImpl.java:96)
com.glide.rest.handler.impl.ServiceResultHandlerImpl.processServiceResult(ServiceResultHandlerImpl.java:40)
com.glide.rest.processors.RESTAPIProcessor.process(RESTAPIProcessor.java:340)
com.glide.processors.AProcessor.runProcessor(AProcessor.java:625)
com.glide.processors.AProcessor.processTransaction(AProcessor.java:277)
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)