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

Can you try to call this function in background script? It will throw the errors.

Also you have the users those end date is 14 days from now?

Yes Pranesh i do have 10users with 14 days..

even in background ground script i dont see any errors

can you add some logs and try?

 

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

		enddate = user.u_end_date;
		gs.log(enddate);

		var currentdate = new GlideDate();
		var diffSeconds = gs.dateDiff(currentdate, enddate, false);
		var remainingdays = parseInt(diffSeconds.replace("00:00:00", ""));
		if (remainingdays == 14) {
			gs.log(user.name);
			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();
		gs.log(current.name+"sys id"+rc.sys_id);
		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.addQuery('request', rq);
			tsk.query();
			while (tsk.next()) {
				tsk.variables.LastWorkingDay = current.u_end_date;
				tsk.short_description = desc;
				tsk.update();
			}

		},

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Prem,

no where you are passing the hr_profile GlideRecord object to the offboard function

what is that hr_profile

Regads
Ankur

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

Hi Ankur,

Actually its not hr_profile its "user" sorry for the wrong code, i have updated the code now.