- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 06:45 AM
Hello,
I was having functionality issues with UI actions on a form. Basically in a change record it was skipping the date/time check and the state was being to move scheduled. I fixed that in that the state is not moving to scheduled and you get an error but the start date gets cleared out now. If you reload the form, it does come back but is there a way to fix that it doesn't clear out. I did that for standard, emergency and normal changes. It is working fine in standard change, the start date field does not get cleared out if you put in the wrong end date duration and save it. But for normal and emergency changes though you get the error when you put the wrong date in, the start date selected gets cleared out..Here is my code below for the business rule:
//For standard change request this prevents the user from bypassing the date/time check if they don't click out of planned end date.
if (current.type == 'standard') {
var endDate = current.end_date;
var startDate = current.start_date;
var durationD = '01-01-1970 03:00:00';
var val1 = gs.dateDiff(startDate, endDate, true);
var val2 = gs.dateDiff('01-01-1970 01:00:00', durationD, true);
var dur = new GlideDuration(val2 * 1000);
var indicator = val1 - val2;
if (indicator > 0) {
current.setValue('end_date', '');
current.setValue('state', '-5');
gs.addErrorMessage(gs.getMessage('Given dates exceeds the record duration value (' + dur.getDisplayValue() + '). Please correct before save.'));
current.setAbortAction(true);
return;
}
}
//For normal and emergency change request this prevents the user from bypassing the date/time check if they don't click out of planned end date.
if (current.type == 'normal' || current.type == 'emergency') {
var endDate = new GlideDateTime();
var startDate = new GlideDateTime();
endDate.setDisplayValue(current.end_date);
startDate.setDisplayValue(current.start_date);
var check = false;
//5 days in milliseconds
var fiveDaysInMs = 5 * 24 * 60 * 60 * 1000;
//Converts the start and end date to milliseconds. Checks if the difference between the end and start date is less than or equal to Five Days in milliseconds.
if (endDate.getNumericValue() - startDate.getNumericValue() <= fiveDaysInMs) {
check = true;
}
if (check == false) {
current.setValue('end_date', '');
current.setValue('start_date', '');
current.setValue('state', '-5');
gs.addErrorMessage(gs.getMessage("The change window should not exceed more than Five days, Please select the start date and end date accordingly"));
current.setAbortAction(true);
return;
}
Many thanks!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 07:00 AM
Hi,
For normal and emergency change, you are resetting the start date inside your script. I have commented and highlighted the line for your reference
if (current.type == 'normal' || current.type == 'emergency') {
var endDate = new GlideDateTime();
var startDate = new GlideDateTime();
endDate.setDisplayValue(current.end_date);
startDate.setDisplayValue(current.start_date);
var check = false;
//5 days in milliseconds
var fiveDaysInMs = 5 * 24 * 60 * 60 * 1000;
//Converts the start and end date to milliseconds. Checks if the difference between the end and start date is less than or equal to Five Days in milliseconds.
if (endDate.getNumericValue() - startDate.getNumericValue() <= fiveDaysInMs) {
check = true;
}
if (check == false) {
current.setValue('end_date', '');
//current.setValue('start_date', '');
current.setValue('state', '-5');
gs.addErrorMessage(gs.getMessage("The change window should not exceed more than Five days, Please select the start date and end date accordingly"));
current.setAbortAction(true);
return;
}
Thank you,
Palani
Palani

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 07:00 AM
Hi,
For normal and emergency change, you are resetting the start date inside your script. I have commented and highlighted the line for your reference
if (current.type == 'normal' || current.type == 'emergency') {
var endDate = new GlideDateTime();
var startDate = new GlideDateTime();
endDate.setDisplayValue(current.end_date);
startDate.setDisplayValue(current.start_date);
var check = false;
//5 days in milliseconds
var fiveDaysInMs = 5 * 24 * 60 * 60 * 1000;
//Converts the start and end date to milliseconds. Checks if the difference between the end and start date is less than or equal to Five Days in milliseconds.
if (endDate.getNumericValue() - startDate.getNumericValue() <= fiveDaysInMs) {
check = true;
}
if (check == false) {
current.setValue('end_date', '');
//current.setValue('start_date', '');
current.setValue('state', '-5');
gs.addErrorMessage(gs.getMessage("The change window should not exceed more than Five days, Please select the start date and end date accordingly"));
current.setAbortAction(true);
return;
}
Thank you,
Palani
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2022 07:24 AM
Hi Fatima,
current.setValue('state', -5);
Shakeel Shaik 🙂