- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 03:50 PM
Hello everyone,
I am working on a UI policy to hide certain fields on a form depending on the sys_created_on date, if before a certain date I need specific fields hidden or if it's after that date different fields hidden. Have tried multiple things and cannot get it to work. Here is what I currently have.
function onCondition() {
var date1 = new GlideDateTime('2022-06-16 12:00:00');
if (g_form.getvalue('sys_created_on') >= date1); {
g_form.setVisible('u_funding_gl_organization' , false);
g_form.setVisible('u_funding_gl_department' , false);
g_form.setVisible('u_operating_gl_org' , false);
g_form.setVisible('u_operating_gl_dept' , false);
}
if (g_form.getvalue('sys_created_on') <= date1); {
g_form.setVisible('u_funding_gl_organization_v3' , false);
g_form.setVisible('u_operating_gl_organization_v1' , false);
g_form.setVisible('u_operating_gl_organization_v1.u_premier_department' , false);
g_form.setVisible('u_funding_gl_organization_v3.u_premier_department' , false);
}
}
Thanks,
Jon
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 05:58 PM - edited 09-06-2024 03:40 AM
Although it is generally recommended to use a UI Policy when setting fields visible, mandatory, or read only, from what I can see this case isn't the best use as the scripts are meant to be based on the "When to Apply" condition(s). If the condition evaluates to true, run the first script, if it evaluates to false, run the other script. The scripts then should do things that the simple UI Policy Actions cannot. Do you have any Conditions? If you got the date comparison in this example to work with conditions, then you could just hide the first set of fields in the Execute if true script, and the second set in the Execute if false script. It will work in one script without any conditions, it's just a non-standard implementation of this feature. Another straight and narrow path would be to use an onLoad and/or onChange Client Script.
In any event, you'll need to use methods that are supported in the client. GlideDateTime is a server class, so it can only be used in server scripts. Here's the definitive article on how to manipulate date and time in server scripts, getting the values in client scripts, which is always good to know how to do when that inevitably comes up
https://www.servicenow.com/community/developer-forum/client-script-date-time-functions/m-p/1457091
but a simple comparison of two dates can be done on the client. The solution will be similar for a scripted UI Policy or a Client Script. Firstly, since you are using sys_created_on as one of the dates, you need to be sure this field is selected on the form layout. It can be hidden, but it needs to be part of the form for g_form to be able to get the value. To get on with the actual solution, here's one way to do it:
var date1 = getDateFromFormat('2022-06-16 12:00:00', g_user_date_time_format);
var date2 = getDateFromFormat(g_form.getValue('sys_created_on'), g_user_date_time_format);
if (date2 >= date1) {
<<hide fields>>
} else {
<<hide different fields>>
}
I can't find the reference for all that, I've just used it before. 'Date' in getDateFromFormat actually means date/time, and since you're on the client you are getting the displayed version of the system value, so you'll encounter user preferences for date and time formats, timezone offsets, etc. This approach converts date1 and date2 to a number - milliseconds since 1970 or something like that, so this works down to the second in a comparison. I used an else instead of another if as the second date is either greater than or equal to the first date or it's not, but you can use your second if statement with the overlapping = if you want both blocks to execute.
You also may want to consider using setDisplay in place of setVisible as setDisplay consumes the empty space of the hidden fields with those that are shown, whereas setVisible leaves everything in its place, so you'll see the empty space. For troubleshooting, alerts work in UI Policy scripts the same as Client Scripts, so you can always add a bunch of those to see what, if anything the script is doing along the way, values assigned to variables, etc.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 04:25 PM
Have you tried using the condition builder instead to check your date logic? It's usually pretty good with dates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 04:31 PM
I can make it work with the condition builder, but I will need to create 2 UI policies if I go that route. Was hoping to do it with just one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2024 05:58 PM - edited 09-06-2024 03:40 AM
Although it is generally recommended to use a UI Policy when setting fields visible, mandatory, or read only, from what I can see this case isn't the best use as the scripts are meant to be based on the "When to Apply" condition(s). If the condition evaluates to true, run the first script, if it evaluates to false, run the other script. The scripts then should do things that the simple UI Policy Actions cannot. Do you have any Conditions? If you got the date comparison in this example to work with conditions, then you could just hide the first set of fields in the Execute if true script, and the second set in the Execute if false script. It will work in one script without any conditions, it's just a non-standard implementation of this feature. Another straight and narrow path would be to use an onLoad and/or onChange Client Script.
In any event, you'll need to use methods that are supported in the client. GlideDateTime is a server class, so it can only be used in server scripts. Here's the definitive article on how to manipulate date and time in server scripts, getting the values in client scripts, which is always good to know how to do when that inevitably comes up
https://www.servicenow.com/community/developer-forum/client-script-date-time-functions/m-p/1457091
but a simple comparison of two dates can be done on the client. The solution will be similar for a scripted UI Policy or a Client Script. Firstly, since you are using sys_created_on as one of the dates, you need to be sure this field is selected on the form layout. It can be hidden, but it needs to be part of the form for g_form to be able to get the value. To get on with the actual solution, here's one way to do it:
var date1 = getDateFromFormat('2022-06-16 12:00:00', g_user_date_time_format);
var date2 = getDateFromFormat(g_form.getValue('sys_created_on'), g_user_date_time_format);
if (date2 >= date1) {
<<hide fields>>
} else {
<<hide different fields>>
}
I can't find the reference for all that, I've just used it before. 'Date' in getDateFromFormat actually means date/time, and since you're on the client you are getting the displayed version of the system value, so you'll encounter user preferences for date and time formats, timezone offsets, etc. This approach converts date1 and date2 to a number - milliseconds since 1970 or something like that, so this works down to the second in a comparison. I used an else instead of another if as the second date is either greater than or equal to the first date or it's not, but you can use your second if statement with the overlapping = if you want both blocks to execute.
You also may want to consider using setDisplay in place of setVisible as setDisplay consumes the empty space of the hidden fields with those that are shown, whereas setVisible leaves everything in its place, so you'll see the empty space. For troubleshooting, alerts work in UI Policy scripts the same as Client Scripts, so you can always add a bunch of those to see what, if anything the script is doing along the way, values assigned to variables, etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2024 04:57 AM
Thank you again Brad, your script worked but I did just end up using the execute if true and execute if false blocks. I had overlooked them initially, really appreciate your support and sharing your knowledge. Very informative and educational, it slipped my mind that GlideDateTime was a server class, and I was working with the client side.
Thanks again,
Jonathan