Auto-populate Expiration date based on the Start date and Duration of months selected

Nagashree5
Tera Contributor

Hi All,

 

Below are the fields on the form level.

Nagashree5_0-1697640605501.png

Based on the start date and duration of months selected (it is a dropdown of 1 to 12 numbers) - Expiration date should be auto populated.

suppose if the start date is selected as 10-18-2023 and duration of months is selected as 3 months, the Expiration date needs to be auto populated as 10-18-2023 + 90 days.

Can anyone guide me on this.

 

TIA.

15 REPLIES 15

Hi  @Vanderlei,

Thanks for the response. Can you please have a look at the alerts below. It is setting the value of the expiration date like below.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   var date_number = getDateFromFormat(g_form.getValue('poa_start_date'), g_user_date_time_format);
   alert(date_number); // The alert is 0 everytime
var my_date = new Date(date_number);
alert(my_date);//The alert is the date selected in the Poa start date - "Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)""
my_date.setMonth(my_date.getMonth() + 1 + newValue);
alert(my_date.toLocaleDateString());//The alert is setting the dates in 1970's-for ex "01/02/1971"

g_form.setValue("poa_expiration_date",my_date.toLocaleDateString());
}

Hi @Nagashree5 

Ok, I'll explain what each alert means

 

 

 

 alert(date_number); // The alert is 0 everytime

 

 

 

is the value in milliseconds of that date(Are you changing the value of the poa_start_date field before changing the POA Duration value?)

 

 

 

alert(my_date);//The alert is the date selected in the Poa start date - "Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)""

 

 

 

This is the date value, as the previous value field is 0, the date will be 01/01/1970

 

 

 

alert(my_date.toLocaleDateString());//The alert is setting the dates in 1970's-for ex "01/02/1971"

 

 

 

my_date.toLocaleDateString() only converts the date to, mm/dd/yyyy

 

if you want dd/mm/yyyy, use this my_date.toLocaleDateString("en-GB") 

Please explain to me how you are testing and try changing the POA Start Date value before changing the Duration field value

 

If my answer helped you, please mark my answer as helpful.

 

Vanderlei Catione Junior | LinkedIn

Senior ServicePortal Developer / TechLead at The Cloud People



References:

Date: Javascript
Client Side Dates in ServiceNow 

Hi @Vanderlei,

 

Thanks for the detailed exp.

Is the value in milliseconds of that date(Are you changing the value of the poa_start_date field before changing the POA Duration value?) - Yes, the order of selecting the fields is POA Start date, Duration in Months and POA Expiration date.

The client script is written on change of "Duration in Months".

Hi @Nagashree5 
Replace 

 

var date_number = getDateFromFormat(g_form.getValue('poa_start_date'), g_user_date_time_format);

 

 

for 

 

var date_number = g_form.getValue('poa_start_date')

 

Can you send the technical names of the fields?

Try control + / tn (SN Utils)

 

Hi @Vanderlei, Tried the above and the first alert is the date selected and next two alerts are Invalid.

Can you send the technical names of the fields? - when i do this i get the backend names of the client script fields.