While loop in script include

tsoct
Tera Guru

I'm trying to loop through all of the tasks' start and end dates, however the script below just checks the first task and returns no current bookings, while there are existing tasks with the same start date and should return response = 'false'.

var CheckActiveBooking = Class.create();
CheckActiveBooking.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkActiveBooking: function() {
        var newbookingStart = this.getParameter('sysparm_start');
        var gr = new GlideRecord('sc_task');
        gr.addEncodedQuery('state!=3^state!=4^state!=7');
        gr.query();

        while (gr.next()) {
            var startDateTime = new GlideDateTime(gr.u_start_time.getDisplayValue());
            var endDateTime = new GlideDateTime(gr.u_end_time.getDisplayValue());

            if (newbookingStart >= startDateTime && newbookingStart <= endDateTime) {
                answer = 'false'; //Existing booking found
            } else {
                answer = 'true'; //Allow new booking
            }
            return answer;
        }

    },
    type: 'CheckActiveBooking'

});

 

2 ACCEPTED SOLUTIONS

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

As soon as the code return will be executed the script it come out of the function.

If you run the complete loop and at the end you just want to return answer , that would not work too as it will have the value that was run for the last loop

If i understand correctly, what you need is run this for all records and break as soon as you see 'Existing booking found', if so then use the below

 

var CheckActiveBooking = Class.create();
CheckActiveBooking.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkActiveBooking: function() {
        var newbookingStart = this.getParameter('sysparm_start');
        var gr = new GlideRecord('sc_task');
        gr.addEncodedQuery('state!=3^state!=4^state!=7');
        gr.query();

        while (gr.next()) {
            var startDateTime = new GlideDateTime(gr.u_start_time.getDisplayValue());
            var endDateTime = new GlideDateTime(gr.u_end_time.getDisplayValue());

            if (newbookingStart >= startDateTime && newbookingStart <= endDateTime) {
                answer = 'false'; //Existing booking found
break;  // as soon as it will see this it will come out of the while, untill then it will keep executing.
            } else {
                answer = 'true'; //Allow new booking
            }
}
            return answer;
        

    },
    type: 'CheckActiveBooking'

});

 

-Anurag

View solution in original post

AshishKM
Kilo Patron
Kilo Patron

Hi @tsoct , 

It's returning the first record check, because the return statement is within the while loop.

Keep this return out of while loop, if you need to iterate over all record. 

 

-Thanks,
AshishKM

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

View solution in original post

3 REPLIES 3

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

As soon as the code return will be executed the script it come out of the function.

If you run the complete loop and at the end you just want to return answer , that would not work too as it will have the value that was run for the last loop

If i understand correctly, what you need is run this for all records and break as soon as you see 'Existing booking found', if so then use the below

 

var CheckActiveBooking = Class.create();
CheckActiveBooking.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkActiveBooking: function() {
        var newbookingStart = this.getParameter('sysparm_start');
        var gr = new GlideRecord('sc_task');
        gr.addEncodedQuery('state!=3^state!=4^state!=7');
        gr.query();

        while (gr.next()) {
            var startDateTime = new GlideDateTime(gr.u_start_time.getDisplayValue());
            var endDateTime = new GlideDateTime(gr.u_end_time.getDisplayValue());

            if (newbookingStart >= startDateTime && newbookingStart <= endDateTime) {
                answer = 'false'; //Existing booking found
break;  // as soon as it will see this it will come out of the while, untill then it will keep executing.
            } else {
                answer = 'true'; //Allow new booking
            }
}
            return answer;
        

    },
    type: 'CheckActiveBooking'

});

 

-Anurag

AshishKM
Kilo Patron
Kilo Patron

Hi @tsoct , 

It's returning the first record check, because the return statement is within the while loop.

Keep this return out of while loop, if you need to iterate over all record. 

 

-Thanks,
AshishKM

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

tsoct
Tera Guru

Thank you guys!  @AshishKM @Anurag Tripathi 

I got it works by combining your suggestion!