Catalog Client Script Issue

Community Alums
Not applicable

Hi,

I have 5 values for a variable called Application. One of the values of the Application variable is 'TransPerfect'. The Application variable is a list collector. So, if the user selects 'TransPerfect' as well as other values, two other variables 'client_id', 'uin' should be shown at task level. I have written this code, but it is not working. Issue is in the code at line 12 maybe. It is showing JavaScript error. Kindly help.

 

Application.PNG

 

variables.PNG

 

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

    var appl = g_form.getDisplayValue('application').toString().split(',');

    alert("appl " + appl);
    var count = 0;
    if (newValue == "add") {

        count = appl.match(/TransPerfect/g).length;
		alert(count);
        if (count > 1) {
            g_form.setVisible("uin", true);
            g_form.setMandatory("uin", true);
            g_form.setVisible("client_id", true);
            g_form.setMandatory("client_id", true);
        }
    }
}

 

Issue.PNG

 

(g_env) [SCRIPT:EXEC] Error while running Client Script "onChangeTransPerfectAdd": TypeError: appl.match is not a function

 

Regards

Suman P.

4 REPLIES 4

Sai Shravan
Mega Sage

Hi @Community Alums , can you give a try with below code 

 

 

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

    var appl = g_form.getValue('application').toString().split(',');
    
    // Count how many times 'TransPerfect' appears in the selected values
    var count = 0;
    for (var i = 0; i < appl.length; i++) {
        if (appl[i].trim() == 'TransPerfect') {
            count++;
        }
    }

    if (count > 1) {
        g_form.setVisible('uin', true);
        g_form.setMandatory('uin', true);
        g_form.setVisible('client_id', true);
        g_form.setMandatory('client_id', true);
    } else {
        g_form.setVisible('uin', false);
        g_form.setMandatory('uin', false);
        g_form.setVisible('client_id', false);
        g_form.setMandatory('client_id', false);
    }
}

 

 

- Instead of using getDisplayValue, I've used getValue to get the selected values of the 'application' list collector field.

- Instead of using match, I've used a loop to iterate through the selected values and count the occurrences.

 

Please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Community Alums
Not applicable

Hi @Sai Shravan ,

It went through the form. But, in the task level, it is not showing the variables.

 

not showing.PNG

 

Regards

Suman P.

Hi @Community Alums,

Make sure that the fields are already added on that form, the client script setting only the visibility, to add those fields to the catalog task record:

1. Click on the gear icon in the top right corner and select "Configure > Form Layout".

2. In the Form Layout configuration, you will see a list of available variables on the left side. Select the variables you want to add to the form and click the right arrow button to move them to the Selected Variables column on the right side.

Vishal Birajdar
Giga Sage

Hello Dave,

 

So you want to display two fields on sctask level if variable "Application" contains "TransPerfect".

 If it was selected by end user the I will suggest not use onChange() script on sctask instead use onLoad script.

 

var requestType = g_form.getValue('request_type');
var appl = g_form.getDisplayValue('application').toString().split(',');


    if (requestType == "add") {

       if (appl.indexOf("TransPerfect") > -1){

           g_form.setVisible("uin", true);
            g_form.setMandatory("uin", true);
            g_form.setVisible("client_id", true);
            g_form.setMandatory("client_id", true);
        } else {
//make the variables visible to false
}
    }

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates