New help on changing the variable label text.

Lisa Goldman
Kilo Sage

Hello,

I’m using g_form.setLabelOf('Old Phone Number', 'New Phone Number') method to dynamically change the label of a variable. While the label changes successfully on the portal, it doesn't update in the desktop view.  Could someone please help? 

Thank you

9 REPLIES 9

@Chaitanya ILCR 

Attached in an update set if you would like to try it.  Thank you

Hi @Lisa Goldman ,

Looks like it's not a straightforward solution to set a label for catalog items

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0758098

https://servicenowguru.com/ui-scripts-system-ui/modifying-label-form-fields-client-scripts/

 

I have used these and update the client script as below with isolate script as false

 

function onChange(control, oldValue, newValue, isLoading) {
    var actualLabel = 'Old Phone:';

    var tableName = g_form.getTableName();
    if (tableName == 'sc_req_item' || tableName == 'sc_task') { //for RITM and SC Task
        var stb = Array.from(document.getElementsByClassName('sn-tooltip-basic'))
        stb.forEach(function(item) {
            var role = item.getAttribute('role');
            if (role == 'heading' && item.innerHTML == actualLabel) {
                item.innerHTML = 'New Phone';

            }

        });
        return;

    }

    try { //for native UI
        var labelElement = $('label_' + g_form.getControl('old_phone').id);
        labelElement.select('.sn-tooltip-basic').forEach(function(elmt) {
            if (newValue == 'yes')
                elmt.innerHTML = 'New phone';
            else
                elmt.innerHTML = actualLabel;

        });
    } catch (e) { //for portal
        if (newValue == 'yes')
            g_form.setLabelOf('old_phone', 'New phone');
        else
            g_form.setLabelOf('old_phone', actualLabel);
    }

}

 

I am also attaching the XML of updated version of client script.

import it and try

 

but at this point it's easier to create/maintain two separate variables for this as this involves DOM manipulation which is not recommended and might break with future releases.

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

I'm assuming you checked to make sure that the client script is set to run on the Item and cat task correct?

 

Also have you checked the browser console to see if there is an error?

 

Hi @DrewW 

The issue I'm encountering is with the Item and Catalog Task in the fulfiller view.

It works perfectly fine in the self-service portal without any errors.

 

Below are the steps I tested in my PDI. When you have a chance, you can replicate it in your PDI to check if you're experiencing the same issue.

 

STEP 1 - Create a text variable

 

LisaGoldman_0-1741990527128.png

 

STEP 2 - Create a Multiple choice variable with one value "yes"

 

LisaGoldman_1-1741990560981.png

 

STEP 3 - onChange Client Script code:

LisaGoldman_2-1741990673922.png

 

function onChange(control, oldValue, newValue, isLoading) {
    
	
	if (newValue == 'yes') {
        g_form.setLabelOf('old_phone', 'New Phone');
    } else {
        g_form.setLabelOf('old_phone', 'Old Phone');
    }
}

 

 

@DrewW 

Attached in an update set if you would like to try it in your PDI.  Thank you