While loop runs only once if it calls the function - script include

Prem13
Tera Contributor

Im trying to loop the sys_user record but it runs only once.

if the end date is nearing im creating a request for the user .

the problem which im facing is the while loop runs only once.

if i remove the function which is being called, it runs for all the records.

 offboard: function() {
        var user= new GlideRecord("sys_user");
        user.addEncodedQuery("active=true^u_end_dateISNOTEMPTY");
        user.query();
        while (user.next()) {
            var enddate;
           
                enddate = user.u_end_date;

            var currentdate = new GlideDate();
            var diffSeconds = gs.dateDiff(currentdate, enddate, false);
            var remainingdays = parseInt(diffSeconds.replace("00:00:00", ""));
           if (remainingdays == 14) {
                gs.eventQueue("send.offboarding.remainder", user, user.name, remainingdays);
                this.generate_request(user);
            }
        }
    },

   generate_request: function(current) {
 var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);
        var item = cart.addItem('0acdec651315ea80db7b745fd144b0a1');
        cart.setVariable(item, 'name', current.sys_id);
        cart.setVariable(item, 'manager', current.manager);
        cart.setVariable(item, 'Location', current.location);
        cart.setVariable(item, 'Enddate', current.u_enddate);
        var rc = cart.placeOrder();
      
        this.REQupdate(rc.sys_id, current);

        
  },

  REQupdate: function(rq, current) {
        

        var req = new GlideRecord('sc_request');
        req.get(rq);
        req.requested_for = current.manager;
        req.opened_by = current.manager;
        req.update();



        var tsk = new GlideRecord('sc_task');
        tsk.query('request', rq);
        tsk.query();
        while (tsk.next()) {
tsk.variables.LastWorkingDay = current.u_end_date;
            tsk.short_description = desc;
            tsk.update();
        }

    },
1 ACCEPTED SOLUTION

Glad that it worked.

There must be some exception thrown when the script runs and that might be causing the loop to break.

Please mark my response as correct and helpful and close the question.

Regards
Ankur

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

View solution in original post

12 REPLIES 12

Hi,

It means it is breaking for the seconds loop onwards

Try to add try catch block to check the exception

try this script where I have added the try catch and logs

 offboard: function() {
	 try{
        var user= new GlideRecord("sys_user");
        user.addEncodedQuery("active=true^u_end_dateISNOTEMPTY");
        user.query();
        while (user.next()) {
            var enddate;
            enddate = user.u_end_date;

            var currentdate = new GlideDate();
            var diffSeconds = gs.dateDiff(currentdate, enddate, false);
            var remainingdays = parseInt(diffSeconds.replace("00:00:00", ""));
			gs.info('offboard remainingdays for ' + user.name + ' ' + remainingdays);
           if (remainingdays == 14) {
                gs.eventQueue("send.offboarding.remainder", user, user.name, remainingdays);
                this.generate_request(user);
            }
        }
	 }
	 catch(ex){
		 gs.info('Exception offboard'+ex);
	 }
    },

   generate_request: function(current) {
	   try{
		   gs.info('generate_request for' + current.getDisplayValue());
 var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);
        var item = cart.addItem('0acdec651315ea80db7b745fd144b0a1');
        cart.setVariable(item, 'name', current.sys_id);
        cart.setVariable(item, 'manager', current.manager);
        cart.setVariable(item, 'Location', current.location);
        cart.setVariable(item, 'Enddate', current.u_enddate);
        var rc = cart.placeOrder();
        this.REQupdate(rc.sys_id, current);
	   }
	   catch(ex){
		   gs.info('generate_request exception'+ex);
	   }
  },

  REQupdate: function(rq, current) {
        
try{
	gs.info('REQupdate for' + current.getDisplayValue());
        var req = new GlideRecord('sc_request');
        req.get(rq);
        req.requested_for = current.manager;
        req.opened_by = current.manager;
        req.update();

        var tsk = new GlideRecord('sc_task');
        tsk.query('request', rq);
        tsk.query();
        while (tsk.next()) {
            tsk.variables.LastWorkingDay = current.u_end_date;
            tsk.short_description = desc;
            tsk.update();
        }
}
catch(ex){
	gs.info('REQupdate exception'+ex);
}

    },

Regards
Ankur

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

 

When i added the try catch block the loop worked . 

can you explain me how thats possible

Glad that it worked.

There must be some exception thrown when the script runs and that might be causing the loop to break.

Please mark my response as correct and helpful and close the question.

Regards
Ankur

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