-
Notifications
You must be signed in to change notification settings - Fork 913
Return date validation for a catalog item using onChange script #1924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Soumyadeep10
wants to merge
2
commits into
ServiceNowDevProgram:main
from
Soumyadeep10:Hacktober-2025-2nd-Pull_returnDate_validation
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
Client-Side Components/Catalog Client Script/Return Date Validation/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| This piece of 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: | ||
|
|
||
| 1. A start date(u_start_date) is entered before setting a return date(u_return_date). | ||
| 2. The return date is within 6 months after the start date. | ||
| 3. Return date is not the start date or before that. | ||
|
|
||
| Let’s say with an example: | ||
Soumyadeep10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| u_start_date = 2025-10-01 | ||
| You enter u_return_date = 2026-04-15 | ||
|
|
||
| Steps: | ||
| a)Difference = 196 days → More than 180 days | ||
| b)Result: u_return_date is cleared and error shown: “Select Return Date within 6 months from Start Date” | ||
|
|
||
23 changes: 23 additions & 0 deletions
23
Client-Side Components/Catalog Client Script/Return Date Validation/validateReturndate.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| function onChange(control, oldValue, newValue, isLoading) { | ||
| if (isLoading || newValue == '') { | ||
| return; | ||
| } | ||
| var u_start_date = g_form.getValue('u_start_date'); //start date validation to check to see whether filled or not | ||
| if (!u_start_date) { | ||
| g_form.clearValue('u_return_date'); | ||
| g_form.showFieldMsg('u_return_date', 'Please enter start date', 'error'); | ||
| } else { | ||
| var startTime = getDateFromFormat(u_start_date, g_user_date_format); //converting to js date object | ||
| var returnTime = getDateFromFormat(newValue, g_user_date_format); | ||
| var selectedStartDate = new Date(startTime); | ||
| var returnDate = new Date(returnTime); | ||
| var returnDateDifference = (returnDate - selectedStartDate) / 86400000; //converting the diff between the dates to days by dividing by 86400000 | ||
| if (returnDateDifference > 180) { | ||
| g_form.clearValue('u_return_date'); | ||
| g_form.showFieldMsg('u_return_date', 'Select Return Date within 6 months from Start Date', 'error'); | ||
| } else if (returnDateDifference < 1) { | ||
Soumyadeep10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| g_form.clearValue('u_return_date'); | ||
| g_form.showFieldMsg('u_return_date', 'Select Return Date in future than Start Date', 'error'); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.