Adding 30 days to variable based on change of the first variable

mattgr
Kilo Guru

Hi Guys,

 

What is the best way to fulfil below scenario:

On the Service Portal form, I have two fields (variables)

- req_received_date

- req_due_date

 

If I choose on the form while filling it out a date on 'req_received_date' field, for example, 1.12.22, I would like the 'req_due_date' field to be populated with a date 30 days ahead from 'req_received_date', which in this case would be 31.12.22? Can this be done by Catalog UI Policy?

 

I would very much appreciate your help

1 ACCEPTED SOLUTION

Sure.
onChange client script on first date field

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
	   g_form.clearValue('date2');
      return;
   }
alert(newValue)
   var ga = new GlideAjax('MyDateAjax'); 
ga.addParam('sysparm_name','mydatefunction'); 
ga.addParam('sysparm_date',newValue); 
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"); 
    g_form.setValue('date2',answer);
}
   
}

SaurabhGupta_0-1669307970643.png

Script Include

 

var MyDateAjax = Class.create();
MyDateAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
mydatefunction:function()
	{
		var dt=this.getParameter('sysparm_date');
		var dtob=new GlideDateTime(dt);
		dtob.addDaysUTC(30);
		return dtob.getDate();
	},
    type: 'MyDateAjax'
});

 

SaurabhGupta_1-1669308012852.png

 

 

 

 

 


Thanks and Regards,

Saurabh Gupta

View solution in original post

14 REPLIES 14

Many thanks, Saurabh,

 

That works perfectly, I just removed the alert as it is not necessary for me

Hi Saurabh,

 

Something strange happened. When I passed this year date it mess up the calculation:

 

 

2022-11-25_09-17-07.pngAny idea why it might miscalculate?

try changing the date?

This is Stange.

As we are using an API.

 

Can you share few more tests with different dates.

 

 

 

 


Thanks and Regards,

Saurabh Gupta

it looks like if I choose the first 12 days on the date it reads it as a month, then from 13th-31st, all works fine,  somewhere during the calculation  ServiceNow tries recognize this as a US format, which is rather bizzare

use another function as addDaysLocalTime()




Thanks and Regards,

Saurabh Gupta