- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-18-2023 12:27 PM
Is it possible to make a field read-only if a checkbox on a dot-walking field is checked?
For example, I have a custom table in which records are associated to a user record (via a reference field). If that user record is inactive, I want to make the fields on the custom form to be read only.
Is this possible? Would I have to do it from the user record or the custom form?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-19-2023 09:48 AM
Looks like the previous solutions did not consider reverting back the fields after they have been set to readonly.
Unfortunately you can't use the g_form.getEditableFields()
twice after you have locked the fields. so you will have to define your own array of fields that you would like to affect by this script.
here is my solution with .getReference()
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Add fields here
var fieldsToControl = [
'description',
'short_description',
'state'
];
g_form.getReference('assigned_to', function(e) {
fieldsToControl.forEach(function(field){
g_form.setReadOnly(field, e.active === 'false');
});
});
}
As a demo i used the "assigned_to" field as the reference field and the "active" field on the user as the checkbox.
Feel free to change those two in the code.
Now if you don't want to use getReference because it's not recommended by SN.
You can now use GlideRecord inside of client script.
Only downside is, if the user viewing the ticket does not have read permissions on your target gr table. they won't be able to do the query.
Here is the GlideRecord solution for client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) return;
var gr = new GlideRecord('sys_user');
if(!newValue ||! gr.get(newValue)) return;
//Add fields to control here
var fieldsToControl = [
'description',
'short_description',
'state'
];
setFieldState(fieldsToControl, gr.getValue('active') === 'false');
function setFieldState(fieldsArr, readOnly){
fieldsArr.forEach(function(field){
g_form.setReadOnly(field, readOnly);
});
}
}
I also added a bonus here to unlock the fields again if the reference field is cleared/is empty.
Here is a demo video:
ā
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-19-2023 09:48 AM
Looks like the previous solutions did not consider reverting back the fields after they have been set to readonly.
Unfortunately you can't use the g_form.getEditableFields()
twice after you have locked the fields. so you will have to define your own array of fields that you would like to affect by this script.
here is my solution with .getReference()
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Add fields here
var fieldsToControl = [
'description',
'short_description',
'state'
];
g_form.getReference('assigned_to', function(e) {
fieldsToControl.forEach(function(field){
g_form.setReadOnly(field, e.active === 'false');
});
});
}
As a demo i used the "assigned_to" field as the reference field and the "active" field on the user as the checkbox.
Feel free to change those two in the code.
Now if you don't want to use getReference because it's not recommended by SN.
You can now use GlideRecord inside of client script.
Only downside is, if the user viewing the ticket does not have read permissions on your target gr table. they won't be able to do the query.
Here is the GlideRecord solution for client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) return;
var gr = new GlideRecord('sys_user');
if(!newValue ||! gr.get(newValue)) return;
//Add fields to control here
var fieldsToControl = [
'description',
'short_description',
'state'
];
setFieldState(fieldsToControl, gr.getValue('active') === 'false');
function setFieldState(fieldsArr, readOnly){
fieldsArr.forEach(function(field){
g_form.setReadOnly(field, readOnly);
});
}
}
I also added a bonus here to unlock the fields again if the reference field is cleared/is empty.
Here is a demo video:
ā
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā07-19-2023 09:56 AM
This is much better solution Edvin, Thanks!
Kind Regards,
Ravi Chandra.