UI Policy script to make a field autopopulate current date and time

manoj12398
Giga Contributor

Hi,

I want to make a field auto populate current date and time using UI policy.   Depending on other field. Please advice.

8 REPLIES 8

Abhinay Erra
Giga Sage

Use a client script and then use GlideAjax in the client script to get the current date time from the server. Let me know if you need any help in scripting.


GlideAjax - ServiceNow Wiki


Thanks Abhinay,



There is a choice field:   so if choice = New   then in other field it should populate current date and time. Can you please help in code.


Only if choice is new, current date should be set?



If so, (Untested Code)



Client Script Type:onChange


Field: your choice field


script:



if(newValue == 'New'){


        var ajax = new GlideAjax('MyDateTimeAjax');


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


        ajax.getXML(function () {


                      g_form.setValue('put your field name here', ajax.getAnswer());


        });


}



Use the same Script Include as in the article.


write a script include : It should be client callable



var MyDateTimeAjax = Class.create();


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  nowDateTime: function () {


    return gs.nowDateTime();



  In your client script: Onchange of filed name



check if condition: if type of choice field = new


{


var ajax = new GlideAjax('script include name');


ajax.addParam('sysparm_name', 'script include function name');


ajax.getXML(something);


function something(resp) {


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


g_form.setValue('filed',answer);


  }




}