Fetching current date and time from onclick UI Macro

mstockel
Kilo Contributor

Hi SN community, 

I have a UI macro that is invoked when a particular button is pressed. When this happens, I send some data to a webserver, and one of these data poubts is the current time and date according to the servicenow instance, I have tried to do it the following way:

data["==unixtime_ms=="] = "${new GlideDateTime().getNumericValue()}";

however, it seems the expression is only evaluated when I update the UI macro, and not OnClick as I want. How do I crate a new GlideDateTime object on each onclick event in a UI macro? Help much appreciated!

1 ACCEPTED SOLUTION

Hi 

it seems you have done something wrong while creating script include.

Create a new one with client callable tick and do this. 

and check if this script include is for all application scopes i.e Global.

var getdate = Class.create();
getdate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

type: 'getdate'
});

This type of Script include is created when client callable is true. 

 

Mark Correct if it helps.

Warm Regards,

Omkar Mone

find_real_file.png

www.dxsherpa.com

View solution in original post

12 REPLIES 12

Hi 

Use

var ga = new GlideAjax('Script_include name');

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

ga.getXML(callBackFunc);

 

function callBackFunc(response)

{

here do what you need 

}

Mark Correct if it helps.

Warm Regards,

Omkar Mone

find_real_file.png

www.dxsherpa.com

 

@omkar if you are working on ui macro then you must have to use that glide ajax in <script> tag in XML body.

 

eg:

 

<script>

function onClick()

{

var ga = new GlideAjax('HelloWorld');

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

ga.addParam('sysparm_user_name',"Bob");

ga.getXML(HelloWorldParse);

}

function HelloWorldParse(response) {

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

    alert(answer);

}

</script>

Hi Harshvardhan,

 

Yes that's what i meant thanks 🙂

Hi, 

So I am using the following script include 

var MyGetDate = Class.create();
MyGetDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	initialize :function(){},
	
	getDate: function(){
			return new GlideDateTime().getNumericValue();
	},
    type: 'MyGetDate'
});

 

and inside <script> </script> tags I do:

var ga = new GlideAjax('MyGetDate');
ga.addParam('sysparm_name','getDate');
ga.getXML(dt_callback);

function dt_callback(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer)
}

 

But it returns null in the answer var each time. Can you see the problem?

Hi 

it seems you have done something wrong while creating script include.

Create a new one with client callable tick and do this. 

and check if this script include is for all application scopes i.e Global.

var getdate = Class.create();
getdate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

type: 'getdate'
});

This type of Script include is created when client callable is true. 

 

Mark Correct if it helps.

Warm Regards,

Omkar Mone

find_real_file.png

www.dxsherpa.com