Clear values based on the option selection changes

Community Alums
Not applicable

Hi All, 

 

having a requirement that, 

we have a catalog form with the variable date range field type of multiple choice 

1 day, 1 week, 1 month, i.e,

based on the option selection two fields will get populated 

start date and end date. 

 

when ever the option selection changes from one day to 1 week we need to clear selection in the date fields. 

 

Any suggestions. 

3 REPLIES 3

S Goutham
Tera Guru

Hi @Community Alums 

 You can write onchange client script on the range field and write script clear the value / populate the value on the start date and end date fields

 

I hope this solves your issue
Mark this as Helpful / Accept the Solution if this clears your issue

Yashsvi
Kilo Sage

Hi @Community Alums,

To achieve the described functionality in ServiceNow, follow these steps:

Step 1: Create Catalog Item and Variables
1. Create Catalog Item: Go to `Maintain Items` and create a new catalog item.
2. Add Variables:
- Date Range: Multiple Choice (1 day, 1 week, 1 month).
- Start Date: Date/Time.
- End Date: Date/Time.

Step 2: Add Client Script
1. Navigate to Client Scripts: In the catalog item, go to `Related Links` > `Catalog Client Scripts`.
2. Create New Client Script: Click `New` to create a client script.

Step 3: Define the Client Script
Add the following code to the client script:

```javascript
(function executeRule(current, g_form, g_user, g_scratchpad) {
function setDateFields() {
var dateRange = g_form.getValue('date_range'); // Replace 'date_range' with the actual variable name
g_form.clearValue('start_date'); // Replace 'start_date' with the actual variable name
g_form.clearValue('end_date'); // Replace 'end_date' with the actual variable name

if (dateRange) {
var currentDate = new Date();
var endDate = new Date(currentDate);

if (dateRange === '1_day') {
// Set end date to current date
} else if (dateRange === '1_week') {
endDate.setDate(endDate.getDate() + 7);
} else if (dateRange === '1_month') {
endDate.setMonth(endDate.getMonth() + 1);
}

g_form.setValue('start_date', currentDate.toISOString().split('T')[0]);
g_form.setValue('end_date', endDate.toISOString().split('T')[0]);
}
}

g_form.getControl('date_range').onchange = setDateFields; // Replace 'date_range' with the actual variable name
})(current, g_form, g_user, g_scratchpad);

Thank you, please make helpful if you accept the solution.

Mark Manders
Mega Patron

We are missing information. How can both the start date and end date be set if you only know the duration? You will at least have to provide two variables to calculate on the third. X + Y = 25, means that both  X and Y can be anything between 0 and 25. So at least the Start or the End date need to be set, to calculate the other one.

And because of this, does the second requirement make sense? If you have the start or end date and you select another duration, the other field can be calculated as well.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark