Change Variable Question based on Condition in Catalog Item

Snow_Hustler42
Tera Contributor

Is it possible to change the question value based on condition?

 

For example, we've a catalog item wherein multiple variables available like:

-service provider(multiple choice[a,b,c,d]).

-request type(multiple choice--[x,y]).

-account number(single line text)

 

when service provider is 'b' and request type is 'y', then the variable 'account number' should be changed to 'account ID'(only the question should change).

#itsm #catalog item

 

There is an option to create 2 variables each for account number and account ID, then use Catalog UI policy based on condition; however, is there any other way to do it without creating multiple variables.  I've tried onchange CS with g_form.setLabelof() but isn't helping.

 

Gurus, please help me with the Catalog CS code..

 

Thanks in Advance

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hello @Snow_Hustler42 ,

Hope you are well!

In the catalog item, when 'Service Provider' is 'b' and 'Request Type' is 'y', dynamically change the label of the 'Account Number' variable to 'Account ID'.

Solution:

1. We have to configure 2 onChange client scripts for the variables 'Service Provider' and 'Request Type' like below. 
#onChange Client Script for Service Provider:

PrasadS_0-1690386203356.png

 

Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //g_form.addInfoMessage(newValue + "\n" + g_form.getValue('request_type'));
    //Type appropriate comment here, and begin script below
    if (newValue == 'b' && g_form.getValue('request_type') == 'y') {
        g_form.setLabelOf('account_number', 'account ID');
    } else {
        g_form.setLabelOf('account_number', 'account number');
    }
}

 

# 2 onChange Client Script for 'Request Type':

PrasadS_1-1690386298576.png

Script:

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

    //Type appropriate comment here, and begin script below
    //g_form.addInfoMessage(newValue + "\n" + g_form.getValue('service_provider'));
    //Type appropriate comment here, and begin script below
    if (newValue == 'y' && g_form.getValue('service_provider') == 'b') {
        g_form.setLabelOf('account_number', 'account ID');
    } else {
        g_form.setLabelOf('account_number', 'account number');
    }
}

 

If this helped you in any way, please hit the like button/mark it helpful. Also, don't forget to accept it as a solution. So it will help others to get the correct solution.

 

thanks,

Prasad

View solution in original post

7 REPLIES 7

Harshal Aditya
Mega Sage
Mega Sage

Hi @Snow_Hustler42 ,

 

Hope you are doing well

 

Please refer to the thread below

https://www.servicenow.com/community/developer-forum/change-label-of-catalog-item-variable-in-servic...

 

Regards,

Harshal

Hi Harshal,

 

I've tried it and It's changing the label of the account number in the related list i.e., question to account ID in the catalog item. It's not changing the question.name on the form for end user. 

Ahmmed Ali
Mega Sage

Hello @Snow_Hustler42 

 

You can use below script to change label in client script.

g_form.setLabelOf('account_id',"Account Name") ;

 

where first argument is variable backend name and second argument is new label.

 

However, I would consider creating new field and showing/hiding that field based on condition using UI policy. This would remove confusion in user while reporting and also easy to mainttain.

 

Thank you,

Ali

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Community Alums
Not applicable

Hello @Snow_Hustler42 ,

Hope you are well!

In the catalog item, when 'Service Provider' is 'b' and 'Request Type' is 'y', dynamically change the label of the 'Account Number' variable to 'Account ID'.

Solution:

1. We have to configure 2 onChange client scripts for the variables 'Service Provider' and 'Request Type' like below. 
#onChange Client Script for Service Provider:

PrasadS_0-1690386203356.png

 

Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //g_form.addInfoMessage(newValue + "\n" + g_form.getValue('request_type'));
    //Type appropriate comment here, and begin script below
    if (newValue == 'b' && g_form.getValue('request_type') == 'y') {
        g_form.setLabelOf('account_number', 'account ID');
    } else {
        g_form.setLabelOf('account_number', 'account number');
    }
}

 

# 2 onChange Client Script for 'Request Type':

PrasadS_1-1690386298576.png

Script:

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

    //Type appropriate comment here, and begin script below
    //g_form.addInfoMessage(newValue + "\n" + g_form.getValue('service_provider'));
    //Type appropriate comment here, and begin script below
    if (newValue == 'y' && g_form.getValue('service_provider') == 'b') {
        g_form.setLabelOf('account_number', 'account ID');
    } else {
        g_form.setLabelOf('account_number', 'account number');
    }
}

 

If this helped you in any way, please hit the like button/mark it helpful. Also, don't forget to accept it as a solution. So it will help others to get the correct solution.

 

thanks,

Prasad