- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 04:10 AM
Hello,
There is requirement where I need to auto populate the date after 90 days in a catalog item using catalog client script.
A field A (Drop down) has an option 90 days. When user selects this option then field B (Date field) should be visible on the form, and it should auto populate the date after 90 days comparing the current date.
Tried the below script which is not populating the correct date and the output is 2001-12-30,
This needs to be implemented in priority.
Thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 08:28 AM - edited 12-01-2022 08:51 AM
Hey @Community Alums
Please try the below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == 90) {
//alert(newValue);
var current_date = new Date();
//alert('Current Date'+current_date);
current_date.setDate(current_date.getDate() + 90);
var added_date = formatDate(current_date, g_user_date_time_format);
var getAddedDate = added_date.split(' ');
//alert('Output Date '+getAddedDate[0]);
g_form.setValue('output_date', getAddedDate[0]+'');
}
}
Mark this as Helpful / Accept the Solution if this clears your issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 05:55 AM
@Community Alums Please create a on-change client script on FIELD A (which is a dropdown field) and write below code in it.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var d = new Date();
d.setDate(Number(g_form.getValue("<FIELD A HERE>")));
var dformat = [d.getFullYear(),
('0' + (d.getMonth() + 1)).slice(-2),
('0' + d.getDate()).slice(-2)
].join('-') + ' ' + [('0' + d.getHours()).slice(-2),
('0' + d.getMinutes()).slice(-2),
('0' + d.getSeconds()).slice(-2)
].join(':');
g_form.setValue("<FIELD B HERE>", dformat.toString());
}
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 05:03 AM - edited 12-01-2022 05:11 AM
Hello @Community Alums ,
You can try following code in Onchnage client script of 'A' field.
var date = new Date();
date.setDate(date.getDate()+parseInt(newValue)) // newValue take the integer value from A field
var dd=date.getDate()
if(dd<10)
dd='0'+dd;
var mm=date.getMonth()+1
if(mm<10)
mm='0'+mm;
var new_date=dd+"-"+mm+"-"+date.getFullYear();
g_form.setValue('B',new_date);
Let me know if you have any further question.
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 05:19 AM
Hi Sunny,
Thank you so much for the response!
Here in my requirement the field A is not a date field. It's a dropdown field which contain only two options "None" and "90 days". So when user selects the option 90days in A field, B field should populate the date adding current date + 90days.
Therefore, I cannot compare the A field as a start date to populate B field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 07:56 AM - edited 12-01-2022 07:57 AM
Hello @Community Alums ,
You can try following code Which will help you to achieve your requirement.
if(newValue!='none' && newValue !='' && newValue!=undefined){
var date = new Date();
date.setDate(date.getDate()+90) // newValue take the integer value from A field
var dd=date.getDate()
if(dd<10)
dd='0'+dd;
var mm=date.getMonth()+1
if(mm<10)
mm='0'+mm;
var new_date=dd+"-"+mm+"-"+date.getFullYear();
g_form.setValue('B',new_date);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2022 08:28 AM - edited 12-01-2022 08:51 AM
Hey @Community Alums
Please try the below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == 90) {
//alert(newValue);
var current_date = new Date();
//alert('Current Date'+current_date);
current_date.setDate(current_date.getDate() + 90);
var added_date = formatDate(current_date, g_user_date_time_format);
var getAddedDate = added_date.split(' ');
//alert('Output Date '+getAddedDate[0]);
g_form.setValue('output_date', getAddedDate[0]+'');
}
}
Mark this as Helpful / Accept the Solution if this clears your issue