- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 09:23 PM
I have a Field on Time Card I want to be Read only except when the user has timecard_admin role.
here is what I tried with no success. screenshots attached
1. UI Policy Script script execute if True
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 09:30 PM
please use onLoad client script and not UI policy
function onLoad() {
// Check if the current user does NOT have the 'timecard_admin' role
if (!g_user.hasRoleExactly('timecard_admin')) {
// Make the field read-only for everyone except timecard_admin
g_form.setReadOnly('your_field_name', true);
} else {
// Optional: Make sure the field is editable for timecard_admin
g_form.setReadOnly('your_field_name', false);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 09:30 PM
please use onLoad client script and not UI policy
function onLoad() {
// Check if the current user does NOT have the 'timecard_admin' role
if (!g_user.hasRoleExactly('timecard_admin')) {
// Make the field read-only for everyone except timecard_admin
g_form.setReadOnly('your_field_name', true);
} else {
// Optional: Make sure the field is editable for timecard_admin
g_form.setReadOnly('your_field_name', false);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 09:52 PM
Thanks heaps worked straight away, I spent two hours arguing with chatpgt yesterday🤣
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 11:02 PM
Can't rely much on ChatGPT !
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 09:32 PM
Hello @markdart ,
Create onload client script and use below code -
function onLoad() {
if (g_user.hasRole('timecard_admin') == true) {
g_form.setReadOnly('user', false);
g_form.setMandatory('user', true);
} else {
g_form.setMandatory('user', false);
g_form.setReadOnly('user', true);
}
}
Thank you.