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

Pranesh072
Mega Sage
Mega Sage

it is due to undefined gliderecord object

 

gs.eventQueue("send.offboarding.remainder", hr_profile, hr_profile.name, remainingdays);

OOPs sorry i forgot to update the script.

even though i mentioned the correct gliderecord object it doesnt loop.

replace it with addquery

 

var tsk = new GlideRecord('sc_task');
tsk.query('request', rq);
tsk.query();

Still the same Pranesh.

Its not looping