How can I add 27 months to a date field in a task

John H1
Tera Guru

I currently have a need to populate a date field within a SCTask.

Issue Date - manually entered

Expire Date - Issue Date + 27 months

1 ACCEPTED SOLUTION

Hi,

Below is the script include code (attached the xml as well)

var myDateUtil = Class.create();
myDateUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getEndDate:function()
	{
		var dt1=this.getParameter('sys_parm_issue_dt');
		var how_many_months=this.getParameter('sys_parm_how_many_months');
		var dt=new GlideDateTime(dt1);
		dt.addMonthsLocalTime(parseInt(how_many_months));
		return dt.getDate();
	},

    type: 'myDateUtil'
});

Below is the onChange client script on the variable issue date

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading) {
		return;
	}
	if(newValue === '')
	{
		g_form.clearValue('end_date');
	}
	var ga = new GlideAjax('myDateUtil'); 
	ga.addParam('sysparm_name','getEndDate'); 
	ga.addParam('sys_parm_issue_dt',newValue); 
	ga.addParam('sys_parm_how_many_months',27); 
	ga.getXML(cb);  

	// the callback function for returning the result from the server-side code
	function cb(response) {  
		var answer = response.responseXML.documentElement.getAttribute("answer"); 
		if(answer)
		g_form.setValue('end_date',answer);
	}


	//Type appropriate comment here, and begin script below

}

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Regards,
Saurabh


Thanks and Regards,

Saurabh Gupta

View solution in original post

7 REPLIES 7

Tony Chatfield1
Kilo Patron

Hi, you have a few options

if the 'Expire date' does not have to be immediately visible on the form, IE it is ok for the field to be blank until the new 'Issue date' is saved/submitted, then you can use a before BR to set the Expire date utilising glideDateTime addMonths

https://developer.servicenow.com/dev.do#!/reference/api/quebec/server_legacy/c_GlideDateTimeAPI#r_GD...

If you require the 'Expire date' field to be immediately populated\updated when the 'Issue date' is changed?
The you will need an onChange Client script running against the 'Issue date' field.
This script will need to use GlideAjax to recalculate the date value from your Issue date, again using glideDateTime addMonths, the new date will then be returned by GlideAjax and populated into the Expire Date by your client script.

https://developer.servicenow.com/dev.do#!/reference/api/quebec/client/c_GlideAjaxAPI?navFilter=glide...

Another option would be to utilize javascript to calculate the new date in the onChange client script,
utilizing the javascript data\time functions.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

 

Do you have any clearly defined standards\processes\preferences in your environment, as these should dictate the approach you take.

I'm still new to scripting so please understand the notes you gave me don't help. Not that an experienced person would not find them helpful, but in my case no to much. To answer your question option 2 will suit our needs.....Regards

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,

Please use below script

var gdt = new GlideDateTime("2011-08-31 08:00:00");

gdt.addMonthsLocalTime(27);

gs.info(gdt.getDate());
gs.info(gdt.getDisplayValue());

Link for Docs : GlideDateTime | ServiceNow Developers

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Regards,
Saurabh


Thanks and Regards,

Saurabh Gupta

I'm trying to run this in a client script and getting this error:
The object "gs" should not be used in client scripts.