set DateTime form field to now() on UI Action

AndyLock
Mega Guru

 

Hi,

On the change_request form we would like to edit the OOB UI Action for 'Review' to set the work_end date.

The below script (customisation in bold) works fine where the users' PC timezone matches their profile timezone:

 

function moveToReview(){

var ga = new GlideAjax("ChangeRequestStateHandlerAjax");

ga.addParam("sysparm_name", "getStateValue");

ga.addParam("sysparm_state_name", "review");

ga.getXMLAnswer(function(stateValue) {

g_form.setValue("state", stateValue);

g_form.setValue("work_end", formatDate(new Date(), g_user_date_time_format));

gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_review");

});

}

 

if (typeof window == 'undefined')

   setRedirect();

 

function setRedirect() {

current.setValue('work_end', new GlideDateTime()); 

    current.update();

    action.setRedirectURL(current);

}

 

 

The issue we face is that g_form.setValue("work_end", formatDate(new Date(), g_user_date_time_format)); appears to use the PC clock rather than the profile timezone. 

The current.setValue('work_end', new GlideDateTime()); in the subesquent setRedirect sets the time correctly, but not before the OOB Client Script 'Actual End Date onChange validation' sees a problem.

We have some users who like to set their ServiceNow timezone differently to their PC (to match the datacentre tz on the other side of the world), so we have a discrepacy between the client-side and server-side dates. 

I'd also like the work_end field to only be populated if it is empty.

Can someone please enlighten me how to fix these two issues?

thx.

 

 

 

OOB 'Review' UI Action link: https://<instance>.service-now.com/nav_to.do?uri=%2Fsys_ui_action.do%3Fsys_id%3Dd82737b3cb100200d71cb9c0c24c9cb4

 

 

1 ACCEPTED SOLUTION

Giles Lewis
Giga Guru

Here is a workaround that might solve your problem. It does not really matter value you use for work_end, because it will be overwritten a couple of lines later. You just need need a value that will satisfy the UI policies. So instead of setting it to the current time, set it to a week in the future.

// Set work_end to a week in the future to satisfy UI Policy
// Note that this value will be overwritten in the server script
var tempFutureDate = new Date((new Date().getTime() + 7*24*60*60*1000));
g_form.setValue("work_end", formatDate(tempFutureDate, g_user_date_time_format));

View solution in original post

4 REPLIES 4

Giles Lewis
Giga Guru

Do not set work_end in the client script portion of the UI Action. Just leave the field blank, and set it in the server portion (which you are already doing). If the UI Action is getting blocked by a UI Policy, then fix the UI Policy so that work_end is not required.

The only way to safely set the the value of date field in a client script is to use GlideAjax; so you should avoid doing it if you can find another solution.

 

Thanks Giles.
We have already considered removing these OOB checks but the suite of UI policies for work_start and work_end validation are OOB, and are a specific user requirement. 

Do you know how to incorporate a second GlideAjax in the above UI action? 

thx,

Andy

Giles Lewis
Giga Guru

Here is a workaround that might solve your problem. It does not really matter value you use for work_end, because it will be overwritten a couple of lines later. You just need need a value that will satisfy the UI policies. So instead of setting it to the current time, set it to a week in the future.

// Set work_end to a week in the future to satisfy UI Policy
// Note that this value will be overwritten in the server script
var tempFutureDate = new Date((new Date().getTime() + 7*24*60*60*1000));
g_form.setValue("work_end", formatDate(tempFutureDate, g_user_date_time_format));

That's a great idea. I'll go with just a day which should account for most (all?) TZs.