Append value using OnChange Client Script

Shraddha17
Tera Contributor

We have a true/false flag called Apply Change Freeze'.

If answer for 'Apply Change Freeze' is 'True' >

Display 2 fields > "Select Dates" - a Date field & "Selected Dates" - a Single Line text 
OOB there is no utility for DATE field to store multiple values. So I am writing an OnChange client script on date field that whenever it changes, you can pick the values and append it to the list of dates in the string field

 

I know this is an incorrect script and that only new values are getting stored in Selected Dates Text field. Can someone assist ?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var appName = g_form.getValue('name');
var select_date = g_form.getValue('u_select_date');
g_form.setValue('u_selected_dates', oldValue + ','+newValue);
}

1 ACCEPTED SOLUTION

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Shraddha17 

 

Please try below code:

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//var appName = g_form.getValue('name');
var select_date = g_form.getDisplayValue('u_select_date');
var selected_date = g_form.getValue('u_selected_dates');
g_form.setValue('u_selected_dates', selected_date.toString() + ','+ select_date.toString());
}

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

3 REPLIES 3

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Shraddha17 

 

Please try below code:

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//var appName = g_form.getValue('name');
var select_date = g_form.getDisplayValue('u_select_date');
var selected_date = g_form.getValue('u_selected_dates');
g_form.setValue('u_selected_dates', selected_date.toString() + ','+ select_date.toString());
}

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Shraddha17
Tera Contributor

Thanks Anubhav, it works!

However, now it shows below string in Selected Dates 

, 2022-11-11 , 2022-11-17 , 2022-12-01

How do we remove 1st comma ?

 
 

 

 

Got this working..

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var select_date = g_form.getValue('u_select_date');
var selected_date = g_form.getValue('u_selected_dates');
if(selected_date == ''){
g_form.setValue('u_selected_dates', selected_date + select_date);
} else {
g_form.setValue('u_selected_dates', selected_date + ' , ' + select_date);
}
}