Populate Date Field Based on Another Field on Catalog form

Sifa Tok
Tera Contributor

Hi, I'm new to ServiceNow and Java . I'm struggling to create a simple logic how I can set the date field based on another field. I need to set "Server Offline until" field should be populated using the formula Opened date + number of days selected "How long should the server be offline before being decommissioned?" fieldMicrosoftTeams-image (45).png

1 ACCEPTED SOLUTION

Samaksh Wani
Giga Sage

Hello @Sifa Tok 

 

You can write a Catalog Client Script :-

 

 

var openedDate = g_form.getValue('opened_date');
var days = g_form.getValue('how_long_server...');
openedDate.addDays(days);
g_form.setValue('server_offline_until',openedDate);

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Regards,

Samaksh

View solution in original post

7 REPLIES 7

Samaksh Wani
Giga Sage

Hello @Sifa Tok 

 

You can write a Catalog Client Script :-

 

 

var openedDate = g_form.getValue('opened_date');
var days = g_form.getValue('how_long_server...');
openedDate.addDays(days);
g_form.setValue('server_offline_until',openedDate);

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Regards,

Samaksh

Thank you for the solution. Another point is "opened date" is the day when users submit the request. So, not sure how to include opened day inside the code?MicrosoftTeams-image (46).png

Hello @Sifa Tok ,

                               You can use current date so when user is submitting form it will pick that date as below

var today_date = new Date();  // use date or date time as per your format
var today_date_str = formatDate(today_date, g_user_date_format);
var open_Date = today_date_str;

Kindly mark correct and helpful if applicable

I am not sure which part is wrong actually. I've just tried but it shows an error as below. I need to set "openedDate" as form's submitted day for example if the users submit the request and if they select for example "5" for "How long should the server be offline before being decommissioned?" question, "Server Offline until" variable should populated automatically "5 + today's date". Can you please explain a a bit clear?

 

Thanks!