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

ankitshaw625
Tera Expert

Can you try using getValue instead of getDisplayValue for getting the start_date field value from the form? Also use setValue at the end for updating the field.

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

@Nur 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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

@Nur 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

 

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

Shakeel Shaik
Giga Sage
Giga Sage

Hi @Nur 

 

 

If my response is helpful, then Please mark as Correct Answer/Helpful.

Please check and let us know.

Thanks šŸ™‚

Shakeel Shaik.

 

Thanks,
Shakeel Shaik šŸ™‚