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

Still the same, 

 

I am wondering if  'sysparm_date' is messing it up

give me some time. I will get back.



Thanks and Regards,

Saurabh Gupta

Hi,

PFA screenshot from my PDI.

 

SaurabhGupta_0-1669372947902.png

 

SaurabhGupta_1-1669372966590.png

 

 

SaurabhGupta_2-1669372991172.png

 

 

SaurabhGupta_3-1669373008756.png

 

 

SaurabhGupta_4-1669373028088.png

 

SaurabhGupta_5-1669373045085.png

 

 

 

 


Thanks and Regards,

Saurabh Gupta

Your PDI  shows format yyyy-mm-dd,

 

if you look at my screenshot is dd-mm-yyyy, 

 

I'm struggling to figure out what might be wrong,  But it looks like in Script Include somewhere at this stage:

 

mattgr_0-1669373541304.png

it gets messed up. I am wondering if I could format it correctly after this line

Hi,

Please refer below API doc link

GlideDateTime | ServiceNow Developers


 


Thanks and Regards,

Saurabh Gupta