How to make fields read only using Client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2017 10:51 PM
I am a newbie to Snow i wrtten client script on incident but fields are not getting read only:
function onLoad() {
//Type appropriate comment here, and begin script below
alert("testing");
g_form.setReadOnly('number', true);
g_form.setReadOnly('date_format', true);
}
- 49,870 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2017 12:25 AM
Hi.
As others have mentioned there is probably something else in the way.
g_form.setReadOnly(field, boolean) is the correct syntax anyway.
To see if the issue might be related to the order scripts are resolved in, try to set the order value to 1000 or whatever might be the highest order value in your instance.
I just wanted to chime in and remind you that setting a field to read only isn't necessarily enough as a proficient end user can code around the read only status as it is set in the end users web browser.
To ensure that nothing is changed you have to create an ACL as ACLs are resolved server side.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 01:43 AM
Here are some possible reasons why your fields are not getting read-only and some solutions:
1. Incorrect Field Name:
- Ensure that the field names you are using in your script are correct. In ServiceNow, field names are case-sensitive. For example, 'number' and 'Number' are different fields.
2. Field Accessibility:
- Check if the fields are accessible and not hidden on the form. If the field is hidden, it cannot be set to read-only.
3. Script Execution:
- Make sure your script is executing. The alert("testing") should pop up when the form loads. If it doesn't, there might be an issue with the script execution.
4. Client Script Type:
- Ensure that the client script is of type 'onLoad'. If it's not, the script will not execute when the form loads.
5. User Permissions:
- Check if the user has the necessary permissions to make the field read-only. Some fields may require admin rights to change their properties.
6. Use of UI Policy:
- If a UI Policy is making the field editable, it will override the client script. Check for any conflicting UI Policies.
7. Correct Syntax:
- The correct syntax to make a field read-only is g_form.setReadOnly('field_name', true); Make sure you are using the correct syntax.
8. Field Presence in Form:
- Ensure that the fields 'number' and 'date_format' are present in the form layout.
9. Script Errors:
- Check the browser console for any script errors. If there are any, resolve them.
10. Clear Cache:
- Sometimes, clearing the browser cache can resolve such issues. Try clearing your cache and then reload the form.
Remember, client scripts run on the client-side (browser) and not on the server-side. So, they are dependent on the user's browser and its settings.
nowKB.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 01:58 AM
Its SN or ServiceNow not SNOW.
You can use UI policy which is without code and make field mandatory / read only or visible based on conditions.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 02:03 AM
function onLoad() {
var isNew = g_form.isNewRecord();
if (!isNew)
g_form.setReadOnly('target_table_name', true);
//Retrain scheduled job will be dynamically created on insert/update of config record
g_form.setReadOnly('retrain_scheduled_job', true);
var ga = new GlideAjax('AutoResolutionMLAjax');
ga.addParam('sysparm_name', 'doMLandNLUChecks');
ga.addParam('sysparm_sysId', g_form.getUniqueValue());
ga.getXML(process); // async ajax call
}
function process(response) {
var result = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
if (result) {
//if training is in progress Training Language glide list should be set to readonly
if (!result.languageConfigPresent || result.trainingCompleted)
g_form.setReadOnly('training_language', false);
else if (!result.trainingCompleted)
g_form.setReadOnly('training_language', true);
else
g_form.setReadOnly('training_language', false);
if (!result.intentsPresent)
g_form.addErrorMessage(getMessage('Your Auto-Resolution intents aren\'t accessible for topic mapping. Check that the prediction server is set up.'));
}
}
nowKB.com
For a good and optimistic result, and solving ServiceNow-related issues please visit this website.https://nowkb.com/home