OnLoad Client Script g_form.setLabelOf

Cobee
Tera Expert

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');
}
}

14 REPLIES 14

Alex Coope - SN
ServiceNow Employee
ServiceNow Employee

@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

Cobee_0-1673879998484.png

It's listed on the servicenow developer site. 

@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

Cobee
Tera Expert

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. 

@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