Skip to content

Commit ad444be

Browse files
authored
Return date validation for a catalog item using onChange script (#1926)
* Create README.md * Create validateReturndate.js * Update README.md Changes to the file: 1. Use case description given. 2. One more example related to another part of the use-case added
1 parent a6d1a65 commit ad444be

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
This piece of code was written as a part of an usecase where the return date value validation was expected to be after start date as well as within 6 months from the start date. This code runs in an OnChange catalog client script for the field 'u_return_date' and validates the return date(u_return_date) in a ServiceNow Catalog item form to ensure:
2+
3+
1. A start date(u_start_date) is entered before setting a return date(u_return_date).
4+
2. The return date is within 6 months after the start date.
5+
3. Return date must be after the start date, it can't be same as it or before it.
6+
7+
Let’s say with an example:
8+
9+
a)
10+
u_start_date = 2025-10-01
11+
You enter u_return_date = 2026-04-15
12+
13+
Steps:
14+
a)Difference = 196 days → More than 180 days
15+
b)Result: u_return_date is cleared and error shown: “Select Return Date within 6 months from Start Date”
16+
17+
b)
18+
u_start_date = 2025-10-02
19+
You enter u_return_date = 2025-10-01
20+
21+
Steps:
22+
a)Difference = -1 day → Return date put as 1 day before start date
23+
b)Result: u_return_date is cleared and error shown: “Select Return Date in future than Start Date”
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function onChange(control, oldValue, newValue, isLoading) {
2+
if (isLoading || newValue == '') {
3+
return;
4+
}
5+
var u_start_date = g_form.getValue('u_start_date'); //start date validation to check to see whether filled or not
6+
if (!u_start_date) {
7+
g_form.clearValue('u_return_date');
8+
g_form.showFieldMsg('u_return_date', 'Please enter start date', 'error');
9+
} else {
10+
var startTime = getDateFromFormat(u_start_date, g_user_date_format); //converting to js date object
11+
var returnTime = getDateFromFormat(newValue, g_user_date_format);
12+
var selectedStartDate = new Date(startTime);
13+
var returnDate = new Date(returnTime);
14+
var returnDateDifference = (returnDate - selectedStartDate) / 86400000; //converting the diff between the dates to days by dividing by 86400000
15+
if (returnDateDifference > 180) {
16+
g_form.clearValue('u_return_date');
17+
g_form.showFieldMsg('u_return_date', 'Select Return Date within 6 months from Start Date', 'error');
18+
} else if (returnDateDifference < 1) {
19+
g_form.clearValue('u_return_date');
20+
g_form.showFieldMsg('u_return_date', 'Select Return Date in future than Start Date', 'error');
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)