- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 09:57 AM
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'
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 10:14 AM - edited ‎03-20-2024 10:15 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 10:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 10:14 AM - edited ‎03-20-2024 10:15 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 10:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 10:31 AM
Thank you guys! @AshishKM @Anurag Tripathi
I got it works by combining your suggestion!