How to Hide 03 Variable fields when 'None' is selected on other Variable field

rishabh31
Mega Sage

Hi Community Members,

I am struggling with one part of my requirement, When Job Responsibilities- 'None', then 03 Variables Fields- Task entry, Duties Entry & Responsibilities Entry should be Invisible (Hide) but currently these 03 variable fields are visible (per below Screenshot), 

find_real_file.png

I Used On Change Catalog Client Script on Variable field- Job Responsibilities, its working fine with my other requirements e.g.,- When select choice 'Task', then only Variable 'Task Entry' should become Mandatory & Visible (per below screenshot)

find_real_file.png

Similarly, when selecting other choices, related variable field becomes Mandatory & Visible (like below Screenshots)

find_real_file.png

--------------------------------

find_real_file.png

-------------------------------

find_real_file.png

-------------------------------------

I Used below mentioned Onchange catalog client script;

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

    if (isLoading || newValue == '') {

        return;

    }

 

    var jobResponsibility = g_form.getValue("job_responsibilities");

    if (jobResponsibility == '4') { // Value for Choice 'All' is 4

        g_form.setDisplay("task_entry", true);

        g_form.setDisplay("duties_entry", true);

        g_form.setDisplay("responsibilities_entry", true);

        g_form.setMandatory("task_entry", true);

        g_form.setMandatory("duties_entry", true);

        g_form.setMandatory("responsibilities_entry", true);

    } else if (jobResponsibility == '1') { // Value for Choice 'Task' is 1

        // While hiding the fields you first need to set Mandatory as false and then set Display as false;

        g_form.setMandatory("duties_entry", false);

        g_form.setMandatory("responsibilities_entry", false);

        g_form.setDisplay("duties_entry", false);

        g_form.setDisplay("responsibilities_entry", false);

 

        g_form.setDisplay("task_entry", true);

        g_form.setMandatory("task_entry", true);

    } else if (jobResponsibility == '3') { // Value for Choice 'Duties' is 3

        // While hiding the fields you first need to set Mandatory as false and then set Display as false;

        g_form.setMandatory("task_entry", false);

        g_form.setMandatory("responsibilities_entry", false);

        g_form.setDisplay("task_entry", false);

        g_form.setDisplay("responsibilities_entry", false);

 

        g_form.setDisplay("duties_entry", true);

        g_form.setMandatory("duties_entry", true);

    } else if (jobResponsibility == '2') { // Value for Choice 'Responsibilities' is 2

        // While hiding the fields you first need to set Mandatory as false and then set Display as false;

        g_form.setMandatory("task_entry", false);

        g_form.setMandatory("duties_entry", false);

        g_form.setDisplay("task_entry", false);

        g_form.setDisplay("duties_entry", false);

 

        g_form.setDisplay("responsibilities_entry", true);

        g_form.setMandatory("responsibilities_entry", true);

    } 

}

-------------------------------------------------

But stuck with only part of requirement i.e., When Job Responsibilities- 'None', then 03 Variables Fields- Task entry, Duties Entry & Responsibilities Entry should be Invisible (Hide) but currently these 03 variable fields are Visible.

Requesting to please provide the revised script to achieve this only part of requirement.

Thanks in advance.

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron

Try this -


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

    if (isLoading || newValue == '') {
        g_form.setMandatory("task_entry", false);
        g_form.setDisplay("task_entry", false);
        
        g_form.setMandatory("duties_entry", false);
        g_form.setDisplay("duties_entry", false);
        
        g_form.setMandatory("responsibilities_entry", false);
        g_form.setDisplay("responsibilities_entry", false);
        
        return;
    
    }

    var jobResponsibility = g_form.getValue("job_responsibilities");
    
    if (jobResponsibility == '4') { // Value for Choice 'All' is 4
    
    g_form.setDisplay("task_entry", true);
    g_form.setMandatory("task_entry", true);
    
    g_form.setDisplay("duties_entry", true);
    g_form.setMandatory("duties_entry", true);
    
    g_form.setDisplay("responsibilities_entry", true);
    g_form.setMandatory("responsibilities_entry", true);
    
    } else if (jobResponsibility == '1') { // Value for Choice 'Task' is 1
    
        // While hiding the fields you first need to set Mandatory as false and then set Display as false;
    
        g_form.setMandatory("task_entry", true);
        g_form.setDisplay("task_entry", true);
        
        g_form.setMandatory("duties_entry", false);
        g_form.setDisplay("duties_entry", false);
        
        g_form.setMandatory("responsibilities_entry", false);
        g_form.setDisplay("responsibilities_entry", false);
    
    } else if (jobResponsibility == '3') { // Value for Choice 'Duties' is 3
    
        // While hiding the fields you first need to set Mandatory as false and then set Display as false;
    
         g_form.setMandatory("task_entry", false);
        g_form.setDisplay("task_entry", false);
        
        g_form.setMandatory("duties_entry", true);
        g_form.setDisplay("duties_entry", true);
        
        g_form.setMandatory("responsibilities_entry", false);
        g_form.setDisplay("responsibilities_entry", false);
    
    } else if (jobResponsibility == '2') { // Value for Choice 'Responsibilities' is 2
    
        // While hiding the fields you first need to set Mandatory as false and then set Display as false;
    
        g_form.setMandatory("task_entry", false);
        g_form.setDisplay("task_entry", false);
        
        g_form.setMandatory("duties_entry", false);
        g_form.setDisplay("duties_entry", false);
        
        g_form.setMandatory("responsibilities_entry", true);
        g_form.setDisplay("responsibilities_entry", true);
    
    } 

}

 

Thanks,

Sagar Pagar

The world works with ServiceNow

View solution in original post

1 REPLY 1

Sagar Pagar
Tera Patron

Try this -


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

    if (isLoading || newValue == '') {
        g_form.setMandatory("task_entry", false);
        g_form.setDisplay("task_entry", false);
        
        g_form.setMandatory("duties_entry", false);
        g_form.setDisplay("duties_entry", false);
        
        g_form.setMandatory("responsibilities_entry", false);
        g_form.setDisplay("responsibilities_entry", false);
        
        return;
    
    }

    var jobResponsibility = g_form.getValue("job_responsibilities");
    
    if (jobResponsibility == '4') { // Value for Choice 'All' is 4
    
    g_form.setDisplay("task_entry", true);
    g_form.setMandatory("task_entry", true);
    
    g_form.setDisplay("duties_entry", true);
    g_form.setMandatory("duties_entry", true);
    
    g_form.setDisplay("responsibilities_entry", true);
    g_form.setMandatory("responsibilities_entry", true);
    
    } else if (jobResponsibility == '1') { // Value for Choice 'Task' is 1
    
        // While hiding the fields you first need to set Mandatory as false and then set Display as false;
    
        g_form.setMandatory("task_entry", true);
        g_form.setDisplay("task_entry", true);
        
        g_form.setMandatory("duties_entry", false);
        g_form.setDisplay("duties_entry", false);
        
        g_form.setMandatory("responsibilities_entry", false);
        g_form.setDisplay("responsibilities_entry", false);
    
    } else if (jobResponsibility == '3') { // Value for Choice 'Duties' is 3
    
        // While hiding the fields you first need to set Mandatory as false and then set Display as false;
    
         g_form.setMandatory("task_entry", false);
        g_form.setDisplay("task_entry", false);
        
        g_form.setMandatory("duties_entry", true);
        g_form.setDisplay("duties_entry", true);
        
        g_form.setMandatory("responsibilities_entry", false);
        g_form.setDisplay("responsibilities_entry", false);
    
    } else if (jobResponsibility == '2') { // Value for Choice 'Responsibilities' is 2
    
        // While hiding the fields you first need to set Mandatory as false and then set Display as false;
    
        g_form.setMandatory("task_entry", false);
        g_form.setDisplay("task_entry", false);
        
        g_form.setMandatory("duties_entry", false);
        g_form.setDisplay("duties_entry", false);
        
        g_form.setMandatory("responsibilities_entry", true);
        g_form.setDisplay("responsibilities_entry", true);
    
    } 

}

 

Thanks,

Sagar Pagar

The world works with ServiceNow