When user selects the Start date, then automatically End data as after 30 days

Jyo090
Tera Contributor

Hi All,

 

I am developing a catalog form where user selects the start data as today, then end date field should automcatlly popuate after 30 days.

 

We have to write client script when on change. anyone script. Please suggest .

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @Jyo090 

You can try following onChange script on start_date variable.

 

function onChange(control, oldValue, new value, isLoading){

   if(isLoading || newValue === ''){

   }

   var start_date_number = getDateFromFormat(g_form.getValue('start_date'), g_user_date_format);

   var start_date = new Date(start_date_number);

var end_date = new Date();

end_date.setDate(start_date.getDate() + 30);

var end_date_str = formatDate(end_date, g_user_date_format);

var date_arr = end_date_str.split(' ');

g_form.setValue('end_date', date_arr[0]);

}

 

 

Please mark my answer helpful and accept as solution if it helped 👍

Thanks,
Anvesh

View solution in original post

3 REPLIES 3

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Jyo090 

 

There are 2 ways, as I think:

 

Try to use property. ( check the knowledge base property , which add 365 days in review date)

 

or 

via script. I am not a coder but this link will help you:

 

https://www.servicenow.com/community/developer-forum/add-30-days-to-expected-date/m-p/1476187

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Jyo090 ,

 

Can u try below code :

 

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

  var endDate = new Date(g_form.getValue(start_date)); 
endDate.setDate(endDate.getDate() + 30); // Format the end date and set it in the end date field

g_form.setValue(end_date,endDate);

}

 

Thanks,

Danish

 

AnveshKumar M
Tera Sage
Tera Sage

Hi @Jyo090 

You can try following onChange script on start_date variable.

 

function onChange(control, oldValue, new value, isLoading){

   if(isLoading || newValue === ''){

   }

   var start_date_number = getDateFromFormat(g_form.getValue('start_date'), g_user_date_format);

   var start_date = new Date(start_date_number);

var end_date = new Date();

end_date.setDate(start_date.getDate() + 30);

var end_date_str = formatDate(end_date, g_user_date_format);

var date_arr = end_date_str.split(' ');

g_form.setValue('end_date', date_arr[0]);

}

 

 

Please mark my answer helpful and accept as solution if it helped 👍

Thanks,
Anvesh