Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Dynamic label on catalog form

saisahithiA
Tera Contributor

I have written onchange client script where I am setting the label of a variable on runtime. based on the other variable selection . But in RITM it is still showing old label name. Please advise on this .
NOTE:

  • RITM displays variable names and labels based on the variable definition in the catalog item, not the dynamic label applied during form rendering.
  • g_form.setLabelOf() updates the UI only for the current session; it does not persist the label to the variable’s value or metadata.

    My Script:
    function onChange(control, oldValue, newValue, isLoading) {
        if (isLoading || newValue == '') return;

        var sourceValue = g_form.getValue('source_field');
        var labelText = '';

        if (sourceValue == 'AAA') {
            labelText = 'XXX';
        } else if (sourceValue == 'BBB') {
            labelText = 'YYY';
        } else {
            labelText = 'ZZZ';
        }

        // Smooth label update
        setTimeout(function() {
            g_form.setLabelOf('source_data', labelText);
        }, 300);

     
    }
    O/p:
    on RITM it is still showing label name "source_data"


 

 

3 REPLIES 3

Paul Kunze
Tera Guru

Hi, you should set the field "Applies on Requested Items" on your Catalog Client Script to true.

PaulKunze_0-1762434697096.png

 

Ankur Bawiskar
Tera Patron
Tera Patron

@saisahithiA 

2 things to perform and then verify

1) ensure your catalog client script runs on RITM as well. Mark "Applies on Requested Item" as True

AnkurBawiskar_0-1762437932420.png

 

2) also update your script as this -> remove isLoading check so that your onchange runs when form loads as well

function onChange(control, oldValue, newValue, isLoading) {

    var sourceValue = g_form.getValue('source_field');
    var labelText = '';

    if (sourceValue == 'AAA') {
        labelText = 'XXX';
    } else if (sourceValue == 'BBB') {
        labelText = 'YYY';
    } else {
        labelText = 'ZZZ';
    }

    // :white_heavy_check_mark: Smooth label update
    setTimeout(function() {
        g_form.setLabelOf('source_data', labelText);
    }, 300);


}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@saisahithiA 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader