Using client Script set Date/Time = Current Date/Time

Not applicable

Please can someone let me know how to do this?

Thanks

6 REPLIES 6

poornima2
Mega Expert

malan,
Try this UI policy instead of client script,
Run script :true
On Load :true

Execute if true:

function onCondition() {
g_form.setValue('sys_created_on', new Date());

var currentTime = new Date();
var year = currentTime.getFullYear();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
if (month < 10){
month = "0" + month;
}
if (minutes < 10){
minutes = "0" + minutes;
}
if (seconds < 10){
seconds = "0" + seconds;
}
g_form.setValue('sys_created_on', month + "-" + day + "-" + year + " " + hours + ":" + minutes + ":" + seconds);

}


Not applicable

Very cool. Thanks.


this can be simplified even furtherScreenshot 2024-04-17 at 11.58.52 AM.pngScreenshot 2024-04-17 at 11.59.37 AM.png

Not applicable

Perhaps this should go on a new thread but here's the remaining issue I'm running into:

So lets say I have a change request. And let's say the change request is changed to a closed state. Then I want to update the closed date and default it to the current date/time.

The script you provided sets the date/time beautifully. However, it is doing it every time even if the closed_date is already populated. I only want it to occur if the closed date is empty.

So I set the field condition with ORs to check if the change request state is "closed complete" or "Closed incompete". And the script fires - all is good.

Now I want to add the caveat that only do this if closed_date is empty. I tried adding it as an AND but that doesnt work as there is already an OR condition.

Then I tried to check the "closed_at (closed date)" inside the script for null but it cannot find the variable name since it does not have visibility to the variable name.

Here is the script - any advice is appreciated:

function onCondition() {
alert(closed_at); /////////// this is where the javascript errors out as it can't see closed_at.

if(closed_at == null)

{
g_form.setValue('closed_at', new Date());
var currentTime = new Date();
var year = currentTime.getFullYear();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
if (month < 10){
month = "0" + month;
}
if (minutes < 10){
minutes = "0" + minutes;
}
if (seconds < 10){
seconds = "0" + seconds;
}
g_form.setValue('closed_at', month + "-" + day + "-" + year + " " + hours + ":" + minutes + ":" + seconds);

}
}