How to make field readonly using client script(scoped application)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2022 02:46 AM
Hi Experts,
We have a requirement to make reference field (bo_reference) readonly when check box (is bo inherited) is made false and form view should be module_data.
Have tried below onload and onchange scripts.
Onchange:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var view = getView();
//alert(view);
var isBOInherited = g_form.getValue('is_bo_inherited');
//alert(isBOInherited);
if (view == 'module_data') {
//if (isBOInherited == 'true') {
if (newValue == 'true') {
g_form.setReadOnly('bo_reference', true);
//g_form.setValue('bo_reference', '');
alert(bo_reference +'bo_reference');
// g_form.setMandatory('bo_reference', 'true');
}
else if (isBOInherited == 'false') {
g_form.setMandatory('bo_reference', 'false');
g_form.setReadOnly('bo_reference', 'true');
}
}
}
OnLoad:
function onLoad() {
var view = getView();
//alert('view is = ' + view);
var isBOInherited = g_form.getValue('is_bo_inherited');
// alert(isBOInherited);
//bo_referenceISEMPTY
if (view == 'module_data') {
if (isBOInherited == false) {
g_form.setMandatory('bo_reference', false);
g_form.setReadOnly('bo_reference', true);
} else {
g_form.setReadOnly('bo_reference', false);
g_form.setMandatory('bo_reference', true);
}
}
//Type appropriate comment here, and begin script below
}
.
Thanks Jay.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2022 02:50 AM
Hi @Jay N
On your onchange client script your alert is making issue : Use below script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var view = getView();
//alert(view);
var isBOInherited = g_form.getValue('is_bo_inherited');
//alert(isBOInherited);
if (view == 'module_data') {
//if (isBOInherited == 'true') {
if (newValue == 'true') {
g_form.setReadOnly('bo_reference', true);
//g_form.setValue('bo_reference', '');
// alert(bo_reference + 'bo_reference');
// g_form.setMandatory('bo_reference', 'true');
} else if (isBOInherited == 'false') {
g_form.setMandatory('bo_reference', 'false');
g_form.setReadOnly('bo_reference', 'true');
}
}
}
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2022 02:53 AM - edited 11-23-2022 02:55 AM
Hi @Jay N
Alert is causing an issue. comment out the highlighted alert or remove the quotes for bo_reference. If you want to print the bo_reference value, use g_form.getValue('bo_reference')
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2022 11:31 AM
Hi bhavaniy / Gunjan,
Thank you for the reply!
we have tried by removing the alert too, but it is not working as expected.
once we check the check box it is making bo_reference field to readOnly but when we again uncheck the check box then it is not making the bo_reference field to editable.
Regards,
Pravalika
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2022 11:36 AM
Use below code for onchange
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var view = getView();
//alert(view);
var isBOInherited = g_form.getValue('is_bo_inherited');
//alert(isBOInherited);
if (view == 'module_data') {
//if (isBOInherited == 'true') {
if (newValue == 'true') {
g_form.setReadOnly('bo_reference', true);
//g_form.setValue('bo_reference', '');
// alert(bo_reference + 'bo_reference');
// g_form.setMandatory('bo_reference', 'true');
} else if (isBOInherited == 'false') {
g_form.setMandatory('bo_reference', 'false');
g_form.setReadOnly('bo_reference', 'false');
}
}
}