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

 

 

 

 

 

View solution in original post

14 REPLIES 14

Saurabh Gupta
Kilo Patron

Hi,
You can use GlideAjax and onChange client script.



As I don't have any experience with GlideAjax, would you mind elaborating?

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

 

 

 

 

 

Change the variable names as per yours.