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

kmohammed
Kilo Guru

Yes, it is possible to dynamically update a field label on a form based on the values in the sys_class_name field on the alm_asset table using an Onload Client script.

The script you provided is a good starting point, it retrieves the value of the sys_class_name field using the getValue method and compares it to the string "Kittens". If the values match, it updates the label of the serial_number field to "Cats" using the setLabelOf method.

You can add more conditions to update the label based on other values of the sys_class_name field. For example:

 

function onLoad() {
    // Retrieve the value of the sys_class_name field
    var type = g_form.getValue('sys_class_name');
    // Compare the value to different options
    if (type == "Kittens") {
        // Update the label of the serial_number field to "Cats"
        g_form.setLabelOf('serial_number', 'Cats');
    } else if (type == "Dogs") {
        // Update the label of the serial_number field to "Dogs"
        g_form.setLabelOf('serial_number', 'Dogs');
    } else if (type == "Fish") {
        // Update the label of the serial_number field to "Fish"
        g_form.setLabelOf('serial_number', 'Fish');
    } else {
        // Update the label of the serial_number field to "Serial Number"
        g_form.setLabelOf('serial_number', 'Serial Number');
    }
}

 

 

This script retrieves the value of the sys_class_name field using the getValue method, compares it to different options using an if-else statement, and updates the label of the serial_number field based on the value of the sys_class_name field.

You can add more conditions to update the label based on other values of the sys_class_name field.

Please note that this code is just an example, and you should adjust it to your specific use case and testing it in a development instance before deploying it to a production instance. Also, be careful when modifying or disabling any of the OOB functionality as it may affect the overall functionality of the instance.



Sagar Pagar
Tera Patron

Hi @Cobee,

 

As per my understanding, you have to show a field/column name based on sys_class_name . i.e. table name.

 

I would suggest adding labels based on the table for your column in dictionary level instead of on load client scripts. It will work without scripts.

 

Steps -

1)  Right click on serial_number field and select Configure dictionary.

2) Go to the bottom of the form and See the Label related lists. Open an existing record. take a look into it.

3) Create a similar record for your required tables.

 

Example (1) -

Table name - Kittens [kittens]

Label - Cat

Plural - Cats

Element - serial_number

Language - en

Hint - add help text here

 

Example (2) -

Table name - Dogs [dogs]

Label - Dog

Plural - Dogs

Element - serial_number

Language - en

Hint - add help text here

 

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Thanks for the reply. I am looking at updating the label name OnLoad based on sys_class_name condition.

Hi @Cobee,

This will also work as same. Try this and let me know.

 

Thanks,
Sagar Pagar

The world works with ServiceNow