The CreatorCon Call for Content is officially open! Get started here.

auto populate current date

SD29
Tera Expert

Hi All,

I have a requirement where i have to auto populate a date field based on the field ('information' - field type is checkbox). If the checkbox is checked then populate today date in the date field.

How can i achieve this, please help.

Thanks in Advance,

SD.

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Please check this as well.



Name: Set Current Date/Time In Field


Type: Client Script


Table:


Description: You can use the following two lines to set the current date and time in a date/time field. This bypasses the problem of getting the value into the proper format and proper timezone:


Parameters:


Script: For more information on running Server Side Script with the client please refer to GlideAjax.


var ajax = new GlideAjax('MyDateTimeAjax'); 
ajax.addParam('sysparm_name', 'nowDateTime');
ajax.getXML(function () { g_form.setValue('put your field name here', ajax.getAnswer()); });

System Script Include

// Be sure the "Client callable" checkbox is checked   var MyDateTimeAjax = Class.create(); 
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { nowDateTime: function () { return gs.nowDateTime(); }

View solution in original post

9 REPLIES 9

Shishir Srivast
Mega Sage

you can achieve it by onChnage cleint script on that check box.



if it's checked then set the today's date.


Hi Shishir,



Could you please give me sample script?



Thanks,


SD


let me get that.


Please try below.



onChange client script on you checkbox,



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


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


  return;


  }



  //Type appropriate comment here, and begin script below


  var ajax = new GlideAjax('MyDateTimeAjax');


  ajax.addParam('sysparm_name', 'nowDateTime');


  ajax.getXML(ajaxResponse);



  function ajaxResponse(serverResponse) {


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


  if(<your checkbox field name>== true)


  {


  g_form.setValue('your field name', answer);


  }


  }


}




script include:



var MyDateTimeAjax = Class.create();


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      nowDateTime: function () {


              return gs.nowDateTime();


      },  


      type: 'MyDateTimeAjax'


});



find_real_file.png