- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I have a Field of type Name-Value pairs where users(manually)/Through BR - can enter multiple Kay-Value inputs, I want to make only the Value of a Specific key(for example, "hosts") read-only, without making the entire field read-only. How can this be achieved through a on-load client script or other method ?
@Ankur
@ravi gouni
@harditsingh
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
No direct way available.
You will have to use DOM manipulation in onLoad client script for this.
Note: DOM manipulation is not recommended
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
3 weeks ago
Hi @Nishna,
If you want to make specific key read-only within Name-Value pairs so it is not possible through standard client-side APIs like g_form or UI Policies. The Name-Value pairs field is treated as a single unit, and these standard methods lack the granularity to interact with individual key-value pairs.
However, this can be accomplished using an onLoad client script that utilizes DOM (Document Object Model) manipulation. It's important to note that DOM manipulation is not a ServiceNow best practice as it can be unreliable and may break with future UI upgrades.
Here is a brief explanation and a sample script:
This approach involves inspecting the HTML structure of the Name-Value pairs field to target the specific input element for the desired key's value and then setting its disabled attribute.
function onLoad() {
// Specify the key for which the value should be read-only
var targetKey = "hosts";
// Function to find and disable the input for the target key
function disableValueByKey(key) {
try {
// Select all rows in the Name-Value pairs field
var nameValueRows = g_form.getControl('YOUR_FIELD_NAME').select('.name-value-row');
// Iterate through each row to find the target key
nameValueRows.forEach(function(row) {
var keyInput = row.querySelector('input[id$="_name"]');
if (keyInput && keyInput.value === key) {
var valueInput = row.querySelector('input[id$="_value"]');
if (valueInput) {
valueInput.disabled = true;
}
}
});
} catch (e) {
jslog('Error disabling Name-Value pair value: ' + e);
}
}
// A timeout is often necessary to ensure the field has rendered
setTimeout(function() {
disableValueByKey(targetKey);
}, 500);
}
Note: For more complex solutions where you need granular control over individual fields, consider using a Multi-Row Variable Set (for catalog items) or a related list with individual fields (for standard forms). These are more stable and supported methods for managing multiple sets of data.
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
No direct way available.
You will have to use DOM manipulation in onLoad client script for this.
Note: DOM manipulation is not recommended
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
3 weeks ago
Hope you are doing good.
Did my reply answer your question?
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