Current date and time in workspace

sakshi18
Tera Contributor

Greetings,

Element used: 

Workspace: BCM Workspace

Table: Event

Code written in : UI Action (ClosedComplete), Workspace Client Script field.

 

We have a requirement where upon clicking on a button the 'closed on' field should get populated with current date and time in BCM workspace.

But when we used 'var dateNow = new Date();' we received a different type of date format which cannot be used in the field i.e.., 'Fri Mar 21 2025 16:15:58 GMT+0530 (India Standard Time)'

Below is the code that we have written so far.

 

function onClick(g_form) {
    var dateNow = new Date();
    alert(dateNow);
    var title = getMessage('Accept policy');
    var message = getMessage("Are you sure you want to accept this policy?");
    g_modal.confirm(title, message, function(confirmed) {
        if (confirmed) {
           alert(dateNow);
            g_form.setValue('closed_on', dateNow);
            g_form.setValue('state', '30');
            g_form.save();
        }
    });
    return false;
}
 
We are unable to fetch date and set it in closed on field. When the setValue (Closed_on) line is commented, then the code runs fine.
Requesting for quick help as this is on priority.
ref: screenshot
sakshi18_12-1742554476737.png

 

Thank you,
Sakshi
1 ACCEPTED SOLUTION

@sakshi18 

why are you using JSON.parse() when you are not returning JSON from script include

that line is breaking the script

try this

function onClick(g_form) {
    var title = getMessage('Accept policy');
    var message = getMessage("Are you sure you want to accept this policy?");
    g_modal.confirm(title, message, function(confirmed) {
        if (confirmed) {
            var ga = new GlideAjax('sn_recovery.Crisis_Event_Client_Utils');
            ga.addParam('sysparm_name', 'getDatetime');
            ga.getXMLAnswer(function(answer) {
                alert('5 hi');
                alert('7 - result: ' + answer);
                g_form.setValue('closed_on', answer);
                g_form.setValue('state', '30');
                g_form.save();
            });
        }
    });
    return false;
}

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

16 REPLIES 16

@sakshi18 

the Script include should be client callable and then it should be able to call it

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

@Ankur Bawiskar 

UI Action:

function onClick(g_form) {
    var ga = new GlideAjax('sn_recovery.Crisis_Event_Client_Utils');
    ga.addParam('sysparm_name', 'getDatetime');
    ga.getXMLAnswer(function(answer) {
        alert('5 hi');
        result = JSON.parse(answer);
        alert('7 - result: ' + result);
    });
    var title = getMessage('Accept policy');
    var message = getMessage("Are you sure you want to accept this policy?");
    g_modal.confirm(title, message, function(confirmed) {
        if (confirmed) {
            var ga = new GlideAjax('sn_recovery.Crisis_Event_Client_Utils');
            ga.addParam('sysparm_name', 'getDatetime');
            ga.getXMLAnswer(function(answer) {
                alert('16 hi');
                result = JSON.parse(answer);
                alert('18-result: ' + result);
            });
            var dateNow = new Date();
            alert('dateNow: ' + dateNow);
            var today_date = new Date();
            var today_date_time_str = formatDate(today_date, g_user_date_time_format);
            alert('today_date_time_str: ' + today_date_time_str);
            // g_form.setValue('closed_on', today_date_time_str);
            // g_form.setValue('state', '30');
            // g_form.save();
        }
    });
    return false;
}
Script Include:
getDatetime: function() {
        var gdt = new GlideDateTime();
        var date = gdt.getValue();
        gs.addInfoMessage('Hi');
        return date;
    },
ref:
sakshi18_0-1742556484109.png

 

Except for 5, dateNow, 16 alerts, no other alerts have been generated

@sakshi18 

why are you using JSON.parse() when you are not returning JSON from script include

that line is breaking the script

try this

function onClick(g_form) {
    var title = getMessage('Accept policy');
    var message = getMessage("Are you sure you want to accept this policy?");
    g_modal.confirm(title, message, function(confirmed) {
        if (confirmed) {
            var ga = new GlideAjax('sn_recovery.Crisis_Event_Client_Utils');
            ga.addParam('sysparm_name', 'getDatetime');
            ga.getXMLAnswer(function(answer) {
                alert('5 hi');
                alert('7 - result: ' + answer);
                g_form.setValue('closed_on', answer);
                g_form.setValue('state', '30');
                g_form.save();
            });
        }
    });
    return false;
}

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

i got the date and time,

but can we change the format??

@sakshi18 

what format you are getting from script include i.e. in alert?

When you set it and the form gets saved, does it not set the date/time correctly?

share screenshots.

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