Populate the date based on the option selection

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2024 10:30 PM
Hi All,
Having a requirement, on the catalog form there are two fields one is choice field and the other field is date field.
the choice field having options called - 1day , 1 week, 1month, 6months.
if the choice field is selected as 'one week'.
then in the date field it should select the date range auto population from current date to one week. vice versa the same for one month and 6months.
any suggestions to achieve this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2024 10:50 PM - edited 05-27-2024 10:50 PM
Hi @Community Alums ,
You can achieve the above requirement by writing a onChange Catalog Client Script below:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Type: onChange
// Field: duration_choice
// Description: Set the end date based on the duration choice field
if (isLoading || newValue === '') {
return;
}
// Get the current date
var currentDate = new Date();
var endDate = new Date(currentDate); // Create a new date object to manipulate
// Add duration based on the selected value
switch (newValue) {
case '1 day':
endDate.setDate(currentDate.getDate() + 1);
break;
case '1 week':
endDate.setDate(currentDate.getDate() + 7);
break;
case '1 month':
endDate.setMonth(currentDate.getMonth() + 1);
break;
case '6 months':
endDate.setMonth(currentDate.getMonth() + 6);
break;
default:
return;
}
// Format the date to 'yyyy-MM-dd' to set the value in the date field
var formattedDate = endDate.getFullYear() + '-' +
('0' + (endDate.getMonth() + 1)).slice(-2) + '-' +
('0' + endDate.getDate()).slice(-2);
g_form.setValue('end_date', formattedDate);
}
If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.
Thanks,
Amitoj Wadhera

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2024 10:27 PM
Thanks for the swift response.
It working the suggested script above one.
But here i have an additional validation where we need to check one more condition.
On the form we have three fields as i said above.
1- Multiple choice one
2- start date
3- End date
On the first field multiple choice if the user selects the choice as 1 week then in the start date user can select the start date from where ever he need access and from there we need to set the end date in to calculate the one week.
any suggestion.