- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 10:36 AM
There is a business ask to automatically populate an end date field for an access request as licences for the request are restricted to use in 2 week intervals. Our request form has a start date and an end date, and either we need to:
Automatically populate the end date as 2 weeks after the user's selected start date.
OR
Restrict the user from selecting an end date that is 2 week past the start date.
I've tried the following scripts:
Automatically setting the start and end date when the form is opened:
function onLoad() {
//Create a new Glide Date for the current date upon the form being opened.
// Apply the new Glide Date to a variable named "startDate".
var startDate = new GlideDate();
// Set the "endDate" variable to "startDate" plus 14 days.
var endDate = startDate.addDays(14);
// Set the field value of start date to the "startDate" glide date.
g_form.setValue("start_date", startDate);
// Set the field value of end date to the "endDate" glide date.
g_form.setValue("end_date", endDate);
}
Automatically setting the end date when the start date changes:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Set the startDate variable to a new Glide Date based on the start_date field value.
var startDate = new GlideDate(g_form.getValue("start_date"));
// Set the endDate variable to a new Glide Date based on the startDate + 14 days.
var endDate = startDate.addDays(14);
// Set the end_date variable on the form to the endDate glide date.
g_form.setValue("end_date", endDate);
}
I haven't found any success with either, and I also can't quite figure out how to potentially go the route of restricting a user from selecting dates too far out in the future. Any help on what I'm doing wrong?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2024 12:39 AM - edited 04-24-2024 01:03 AM
Hi @Maurice Murphy ,
You can try the below code in onchange client script,
var startDate = new Date(newValue);
var endDate = new Date();
var offset = 14;
endDate.setDate(startDate.getDate() + offset);
g_form.setValue('end_date', endDate);
// alert("start date" + startDate);
// alert("end date" + endDate);
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 10:56 AM
The GlideDate API is used in Server side scripts. To use in client side scripts you have to use combo of GlideAjax & Script include. Have a look at below article and probably you can use the script in this article.
https://www.servicenow.com/community/developer-forum/client-script-date-time-functions/m-p/1457091
Regards,
Sharad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 11:17 AM
Hello @Maurice Murphy ,
If I understand the requirement clearly - if it is only the end date validation and it should be after 14 days from the start date then why can't you go and write a ui policy to achieve the same.
Try with the below once, let me know.
Anything after 14 days it should accept.
Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 09:30 PM
Hello @Maurice Murphy ,
I would recommend UI policy over scripting. So go ahead and implement the same.
Please Accept as Solution if it really helps you.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2024 10:01 PM
Hi @Maurice Murphy
Your onchange catalog client script is mostly correct,just need some modifcation. Go for Onchange start date.
var startDate = g_form.getValue('your_start_date_field');
if (startDate) {
// Parse the start date
var start = new GlideDateTime(startDate);
// Add 2 weeks to the start date
start.addDays(14);
// Set the end date field to 2 weeks after the start date
g_form.setValue('your_end_date_field', start.getByFormat('yyyy-MM-dd'));
}
change start_date_field and end_date_field with the actual name of your used field.
Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning
Thanks & Regards
Deepak Sharma