The CreatorCon Call for Content is officially open! Get started here.

Populate date after 90days using catalog client script

Community Alums
Not applicable

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.

Rakshandha_0-1669895769592.png

 

Tried the below script which is not populating the correct date and the output is 2001-12-30,

Rakshandha_1-1669896345536.png

 

 

This needs to be implemented in priority.

 

Thanks in advance!

1 ACCEPTED SOLUTION

S Goutham
Tera Guru

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]+'');
    }
}

SGoutham_0-1669913467377.png

 

 

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

View solution in original post

10 REPLIES 10

Aman Kumar S
Kilo Patron

Hi @Community Alums ,

Follow below article to achieve the same:

https://www.servicenow.com/community/in-other-news/auto-populating-and-validating-date-fields/ba-p/2268163

 

Best Regards
Aman Kumar

Community Alums
Not applicable

Hi Aman,

Thank you so much for the response!

Tried few of the scripts in the link which will match my requirement. But it was not working because 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. And it must be on change client script.

 

Therefore, I cannot compare the A field as a start date to populate B field.

jaheerhattiwale
Mega Sage

@Community Alums Write the below script in client script

 

function onLoad() {
//Type appropriate comment here, and begin script below
var d = new Date();
d.setDate(90);

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("u_start_date", dformat.toString());
g_form.addInfoMessage(dformat);
}

 

Mark as correct answer if this solves your issue

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Community Alums
Not applicable

Hi Jaheerhattiwale 

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. And it must be on change client script.

 

Therefore, I cannot compare the A field as a start date to populate B field.