Dynamically Changing Variable Labels Based on Form in ServiceNow

sravanku888
Tera Contributor

I'm looking for some guidance on a specific UI challenge I'm facing in ServiceNow

 

My Goal: I need to display the label of a variable differently on two distinct forms, while keeping the variable's backend name (and thus its data) consistent across both form.


The Scenario:

I have a variable named "Contract value" with the backend name contract_value. This variable is used on two separate forms:

1.India Budget Form
2.America Budget Form

 

India Budget Form and America Budget Form. Both forms use the same variable, 'Contract value' (with a backend name of contract_value). However, on the India Budget Form, I need the label to display as 'Contract value in INR', and on the America Budget Form, I need it to display as 'Contract value in USD'. Crucially, the backend contract_value should remain the same for both. How can we achieve this?

 

NOTE : That variable is in the variable set, both forms are using same variable set.

 

Any insights, best practices, or specific code examples would be greatly appreciated!

 

Thanks in advance for your help!

Best regards,

Sravan Kumar

1 REPLY 1

folusho
Tera Guru

@sravanku888 

If I understand correctly, your goal is to use same variable (contract_value) from a variable set, but different labels on different forms.

 

if this is correct, the constraint you would have is that since the variable is part of a Variable Set (shared), you cannot directly change the label from the Variable Set (as it would affect all items).

 

Recommended Solution would be to use a Catalog Client Script (onLoad)

This approach uses a catalog client script to dynamically update the label based on the current Catalog Item.

 

function onLoad() {
    var itemName = g_form.getValue('sysparm_item'); // fallback
    var catItem = g_form.getUniqueValue(); // current item sys_id

    // Replace with actual sys_ids or names of your catalog items
    var indiaItem = 'sc_cat_item_sys_id_for_india'; 
    var americaItem = 'sc_cat_item_sys_id_for_america';

    if (catItem === indiaItem) {
        g_form.setLabel('contract_value', 'Contract value in INR');
    } else if (catItem === americaItem) {
        g_form.setLabel('contract_value', 'Contract value in USD');
    }
}