How to make end date auto populate to a year after based on start date?

Nur1
Tera Contributor

Hi Community,

How to make end date auto populate to a year after based on start date?

find_real_file.png

For example, when I select 19-May-2022 in start date, the end date should auto populate to 19-May-2023.

How can I achieve this ? Kindly, please help.

Start and End Date is using Date type. not Date/Time.

Script Include:

var StartEndDate = Class.create();
CIMBStartEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    calculateDate: function() {

        var date = this.getParameter('sysparm_date');
        var year = 1;
        var gdt = new GlideDate(date);
        gdt.addYearsUTC(year);
        return gdt.getNowDate();
    },

    type: 'StartEndDate'
});

Client Script:

OnChange Start Date

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var ga = new GlideAjax('StartEndDate');
    ga.addParam('sysparm_name', "calculateDate");
    ga.addParam('sysparm_date', g_form.getDisplayValue('start_date')); // give here date variable name
    ga.getXMLAnswer(function(answer){
        if(answer != ''){
            g_form.setDisplayValue('end_date', answer); // give proper variable name here
        }
    });

   //Type appropriate comment here, and begin script below
   
}

I've tried this code but it's not working.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Nur 

try this

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

	if(oldValue != newValue){
		var dateMS = getDateFromFormat(newValue, g_user_date_format);
		dateMS += 365 * 24 * 60 * 60 * 1000; // 365 days means 1 year
		var newDT = new Date();
		newDT.setTime(dateMS);
		g_form.setValue('end_date', formatDate(newDT, g_user_date_format));
	}
	//Type appropriate comment here, and begin script below

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

14 REPLIES 14

Jaspal Singh
Mega Patron
Mega Patron

Did you try something? Where are you stuck? I believe there are many community threads over this.

If you have not yet started check for link.

Hi,

I've followed this thread, but the end date is still null.

Kartik Sethi
Tera Guru
Tera Guru

Hi @Nur 

 

It seems you need to achieve this via Client Script. You can follow the community thread to accomplish your requirement.

 

OR

You can opt for Calculated Field in case you have the fields on the table (not the variables). For this you can follow the below-provided approach:

  1. Go Dictionary for End Date, change view to Advanced View.
  2. Go to the Calculated Value Tab:
    find_real_file.png
  3. Write the below-provided script:
    (function calculatedFieldValue(current) {
    	var dateTime = new GlideDateTime();
    	dateTime.setValue(current.u_start_date + ' 00:00:00');
    	dateTime.addYearsLocalTime(1);
    	
    	return dateTime.getDate();
    })(current);​

This will ensure the End Field is automatically populated (on save/submit/update) based on the value in Start Date.

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

Hi Kartik,

I need to make the end date auto populate to next one year ONCE I've entered the start date.

How can I achieve this ?

I've tried this link. But the end date still null.

How to achieve this ? Thanks