OnLoad Client Script g_form.setLabelOf
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 12:41 PM
Hello,
I am looking to dynamically update a field label on a form based on the values in a the sys_class_name on alm_asset table using Onload Client script. as per my example below, if the sys_class_name is Kittens, I would like to update the serial_number field label to Cats. I was successful is using this same idea using the state field, But then I could reference the actual value of the choice selected. Not sure if this is possible on the sys_class_name option.
Thanks,
function onLoad() {
//Type appropriate comment here, and begin script below
var type = g_form.getValue('sys_class_name');
if (type == "Kittens"){
g_form.setLabelOf('serial_number', 'Cats');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2023 05:40 AM
@Cobee,
This is not considered good technical best-practice to change the label of a field via a script as they are overriding the value set in the dictionary and thus are what is termed as "hard-coded" in script = bad.
- This also makes translating the form next to impossible as well as introduce technical debt for maintenance in the event of troubleshooting or label changing requires a code change.
If you need to change the label per extended table (which is what's implied in your code snippet), you actually want to leverage a feature called "dictionary overrides" for labels where you are defining (via configuration) how a label for that extended field should be seen on the extended table rather than the inherited label from the top level table,
Many thanks,
Kind regards
Director of Globalization Deployment, Internationalization
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2023 06:40 AM
It's listed on the servicenow developer site.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2023 07:24 AM
@Cobee,
That page is explaining that the method exists and how to use, rather than what is "best-practice", which in this case would be "dictionary overrides" (linked above),
Many thanks,
Kind regards
Director of Globalization Deployment, Internationalization
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2023 06:43 AM
I don't think there is a dictionary override available for field labels. Maybe I didn't explain myself correctly. I just need to update the field label based on different conditions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2023 07:30 AM
@Cobee,
If it's just purely for field labels, you can just add records in [sys_documentation] for the extended tables for each of those fields. You'll see some examples for fields that extend from task.
Purely from an architectural / process perspective, having it change from label A to label B based on a condition wouldn't be ideal from a process perspective and would introduce technical debt (explained above) if that's implemented.
So, putting the solutioning aside for a moment, what process consideration are you looking to solve? (as there might be a completely different approach to the business problem),
Many thanks,
Kind regards
Director of Globalization Deployment, Internationalization