set date/time - specific time in the past

matthew_magee1
Giga Guru

Hi all,

I know various flavors of this question have been asked before; yet I can't seem to find a simple solution.

I am running Geneva.

On the CHG form, I have 2 date/time fields:

  1. Start Date (start_date)
  2. Notification Date (u_notification_date)

If the user selects 5/8/17 as the start date, I'd like the Notification date to automatically default to the date before @ 14:30:00. In this example, the Notification Date would default to 5/7/17 @ 14:30:00

Any help is greatly appreciated

1 ACCEPTED SOLUTION

oharel
Kilo Sage

Hi Matthew,



If both are date/time, this is what works for me:


onChange client script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue === '') {


  return;


  }



  //Type appropriate comment here, and begin script below


  g_form.clearValue('u_notification_date');


  var start = g_form.getValue('start_date');


 


  //var ga = new GlideAjax('')


  var ga = new GlideAjax('setMyDate');


  ga.addParam('sysparm_name','getDateTime');


  ga.addParam('sysparm_start',start);


  ga.getXML(ParseTheTime);


   


  function ParseTheTime(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  g_form.setValue('u_notification_date',answer);



  }


}




script include:


var setMyDate = Class.create();


setMyDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getDateTime : function() {



  var start = this.getParameter('sysparm_start');


  var days = 1;



  var strDate = start.split(' ')[0];


  var vDate =   new GlideDateTime(strDate);


  vDate.addDays(-days);


  var vDateStr = vDate.getDate() + ' 14:30:00';


  return vDateStr;



  },


      type: 'setMyDate'


});



Based on amlanpal 's script in another post.


harel


View solution in original post

10 REPLIES 10

amlanpal
Kilo Sage

Hi Matthew,



To achieve this requirement you need to write one onChange Client Script on Change request (change_request) table. In the Client Script you need to call the Script include where the calculation will be done. Please find the Client Script and Script Include below. It is working properly in my instance. Request you to give it a try and let me know.



Client Script:


find_real_file.png



Script Include:


find_real_file.png



I hope this helps.Please mark correct/helpful based on impact


Amlan-



This works but I was getting some weird results when I spit the results out in the log file. The date/time would be flipped and it wouldn't subtract 1 day. Not sure why. But this is a good start. Going to play w/ this some more



Matthew


oharel
Kilo Sage

Hi Matthew,



If both are date/time, this is what works for me:


onChange client script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue === '') {


  return;


  }



  //Type appropriate comment here, and begin script below


  g_form.clearValue('u_notification_date');


  var start = g_form.getValue('start_date');


 


  //var ga = new GlideAjax('')


  var ga = new GlideAjax('setMyDate');


  ga.addParam('sysparm_name','getDateTime');


  ga.addParam('sysparm_start',start);


  ga.getXML(ParseTheTime);


   


  function ParseTheTime(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  g_form.setValue('u_notification_date',answer);



  }


}




script include:


var setMyDate = Class.create();


setMyDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getDateTime : function() {



  var start = this.getParameter('sysparm_start');


  var days = 1;



  var strDate = start.split(' ')[0];


  var vDate =   new GlideDateTime(strDate);


  vDate.addDays(-days);


  var vDateStr = vDate.getDate() + ' 14:30:00';


  return vDateStr;



  },


      type: 'setMyDate'


});



Based on amlanpal 's script in another post.


harel


oharel



This worked great. Between the two responses, it works great.



Thanks so much guys-



Matthew