Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

Ankur Bawiskar
Tera Patron

@Nisha B 

did you try adding try catch block to handle exceptions?

are you sure the difference is more than 864000 for those records

try{
	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);
		}
	}
}
catch(ex){
	gs.info(ex);
}
Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur,

Thanks for reply,

I tried adding the try and catch but still not working,
almost 700 records are there I have set the limit to 50 in that almost 30 records have diff>864000 that should update , but it is not happening don't know why

@Nisha B 

any other mandatory field you are forgetting to update while closing?

Did you check data policy is making any other fields as mandatory?

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

that job always update the 1 record only and if data policy is blocking then that 1 record should also be not updated then correct,