Glide Ajax Doubt

yuvarajkate
Giga Guru

I want to display the current date on the Incident form as an alert.

Client Script: 

function onLoad() {
    var ga = new GlideAjax('GetDateTime');
    ga.addParam('sysparam_name', 'date');
    ga.getXMLWait();
    var response = ga.getAnswer(); 
    alert("Current Date: " + response);  
}

Script Include:

var GetDateTime = Class.create();
GetDateTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    date: function() {
        var dt = new GlideDateTime();
        var currentdate = dt.getDate().toString();
        gs.info("Current Date Retrieved by GetDateTime: " + currentdate); 
        return currentdate; 
    },
    type: 'GetDateTime'
});

 

In Alert i am getting Null.

yuvarajkate_0-1735628311360.png

 

Please help out with this.

4 ACCEPTED SOLUTIONS

Rohit99
Mega Sage

Hi @yuvarajkate ,

In client script by mistake you wrote 

ga.addParam('sysparam_name', 'date');

instead of this use following line.

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



Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi



View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@yuvarajkate 

no script include and ajax required.

you can get it directly in onload client script

function onLoad(){
var selected_date = new Date(getDateFromFormat(new Date(), g_user_date_format));
var selected_dateStr = formatDate(selected_date, g_user_date_format);
alert(selected_dateStr);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

dgarad
Giga Sage

Hi @yuvarajkate 

change the client side code as below

    ga.addParam('sysparm_name', 'date');
 
 
If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

View solution in original post

vishwajeet5550
Mega Guru

To display the current date on the Incident form as an alert using GlideAjax, follow these steps. First, create a Script Include to fetch the current date. In the Script Include, write a server-side function that returns the current date. Make sure the Script Include is accessible from the client-side by setting the "Accessible from" field to Client callable

var CurrentDateUtils = Class.create();
CurrentDateUtils.prototype = {
    initialize: function() {},
    getCurrentDate: function() {
        var currentDate = new GlideDateTime();
        return currentDate.getDisplayValue();
    },

    type: 'CurrentDateUtils'
};

To display the current date on the Incident form as an alert using GlideAjax, follow these steps. First, create a Script Include to fetch the current date. In the Script Include, write a server-side function that returns the current date. Make sure the Script Include is accessible from the client-side by setting the "Accessible from" field to Client callable

function onLoad() {
    var ga = new GlideAjax('CurrentDateUtils');
    ga.addParam('sysparm_name', 'getCurrentDate');
    ga.getXMLAnswer(function(response) {
        var currentDate = response.responseXML.documentElement.getAttribute("answer");
        alert('Current date is: ' + currentDate);
    });
}

To display the current date on the Incident form as an alert using GlideAjax, follow these steps. First, create a Script Include to fetch the current date. In the Script Include, write a server-side function that returns the current date. Make sure the Script Include is accessible from the client-side by setting the "Accessible from" field to Client callable

View solution in original post

8 REPLIES 8

Harish Bainsla
Kilo Patron
Kilo Patron

Hi @yuvarajkate  check below link have same requirement

https://www.servicenow.com/community/developer-forum/displaying-current-date-and-time/m-p/1484480

if my answer helps you mark helpful and accept solution

Could you help me with this using Glide Ajax?

rambo1
Tera Guru

Hi @yuvarajkate  

Use below code :

function onLoad() {
var currentDate = new Date();

// Display an alert box with the current date and time
alert("The current date and time is: " + currentDate.toLocaleString());
}
 

Rohit99
Mega Sage

Hi @yuvarajkate ,

In client script by mistake you wrote 

ga.addParam('sysparam_name', 'date');

instead of this use following line.

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



Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi