Mandatory field is not able to control via on change choice field

rajeshraji1
Tera Expert

On change Catalog item

variables string filed like : Field1 ,field11,filed2,field22,filed3,field33  there are six string filed .

and choice field : name : select value 

choice value : none,0,1,2,3.

 

Scenario :

1.When i select none and 0 no one filed populate all the field need to hide .

2. Then If i selected choice value 1 the Field 1 and field11 this field need to visible and need to set mandatory rest of the fields need to Hide .

3.Then if i select choice value 2 then Field1 , field11 and filed2 , field22 these four field need to visible and set mandatory rest of the fields need to Hide .

4.Then if i select choice value 3 then Field1 , field11 and filed2 , field22 , field3 , field33  these six field need to visible and set mandatory for six field .

 

and main thing is these conditions are need to apply task view as well.

 

Note : if on change is not good practice the suggest any other method to archive this .

 

1 ACCEPTED SOLUTION

J Siva
Tera Sage

Hi @rajeshraji1 
Create Catalog UI policies as below.

 

1. When Choice value is 1 or 2 or 3: In the Catalog  UI policy action, add Field 1 and Field 11 (Make mandatory and Visible as true)

2. When Choice value is 2 or 3:In the Catalog UI policy action, add Field 2 and Field 22  (Make mandatory and Visible as true)

3. When choice value is 3:  In the Catalog  UI policy action, add Field 3 amd Field 33  (Make mandatory and Visible as true)

Sample UI policy:

JSiva_1-1745465586390.png

JSiva_2-1745465656318.png

 

 

 

In the Catalog  UI policies check the below highlighted check boxes.

JSiva_0-1745464948782.png

Hope this helps.
Regards,
Siva

View solution in original post

5 REPLIES 5

J Siva
Tera Sage

Hi @rajeshraji1 
Create Catalog UI policies as below.

 

1. When Choice value is 1 or 2 or 3: In the Catalog  UI policy action, add Field 1 and Field 11 (Make mandatory and Visible as true)

2. When Choice value is 2 or 3:In the Catalog UI policy action, add Field 2 and Field 22  (Make mandatory and Visible as true)

3. When choice value is 3:  In the Catalog  UI policy action, add Field 3 amd Field 33  (Make mandatory and Visible as true)

Sample UI policy:

JSiva_1-1745465586390.png

JSiva_2-1745465656318.png

 

 

 

In the Catalog  UI policies check the below highlighted check boxes.

JSiva_0-1745464948782.png

Hope this helps.
Regards,
Siva

HI @J Siva This method is not working i have tried when i apply all this conditions isn't behave correctly kindly suggest i have mentioned on change script so kindly review it and propose solution .this on change script not working in task view also   i have selected all the view form  option. and global icon also checked

if i select 0 to 1 and 1 to 2 its working fine when i select 2 to none functions not working .

 

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

        return;
    }
    var val = g_form.getValue('select');
    if (val == '0' || val == "") {
        g_form.setDisplay('answer1', false);
        g_form.setDisplay('answer11', false);
        g_form.setDisplay('answer2', false);
        g_form.setDisplay('answer22', false);

    }
   
    if (val == '1') {
        g_form.setMandatory('answer1', true);
        g_form.setMandatory('answer11', true);
        g_form.setDisplay('answer1', true);
        g_form.setDisplay('answer11', true);
        g_form.setMandatory('answer2', false);
        g_form.setMandatory('answer22', false);
        g_form.setDisplay('answer2', false);
        g_form.setDisplay('answer22', false);

    } else {
        g_form.clearValue('answer1');
        g_form.clearValue('answer11');
        g_form.setMandatory('answer1', false);
        g_form.setMandatory('answer11', false);
        g_form.setDisplay('answer1', false);
        g_form.setDisplay('answer11', false);
        g_form.setMandatory('answer2', false);
        g_form.setMandatory('answer22', false);
        g_form.setDisplay('answer2', false);
        g_form.setDisplay('answer22', false);

    }
    if (val == '2') {
        g_form.setDisplay('answer1', true);
        g_form.setDisplay('answer11', true);
        g_form.setMandatory('answer1', true);
        g_form.setMandatory('answer11', true);
        g_form.setDisplay('answer2', true);
        g_form.setDisplay('answer22', true);
        g_form.setMandatory('answer2', true);
        g_form.setMandatory('answer22', true);
    }else{
        g_form.clearValue('answer1');
        g_form.clearValue('answer11');
        g_form.clearValue('answer2');
        g_form.clearValue('answer22');   
       
    }  refer the screen short for understanding

Ankur Bawiskar
Tera Patron
Tera Patron

@rajeshraji1 

you can use onChange catalog client script which applies to Catalog Task as well

something like this, please enhance

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

    // Define the fields
    var fields = ['field1', 'field11', 'field2', 'field22', 'field3', 'field33'];

    // Hide all fields initially
    fields.forEach(function(field) {
        g_form.setDisplay(field, false);
        g_form.setMandatory(field, false);
    });

    // Show and set mandatory based on the selected value
    switch (newValue) {
        case '1':
            g_form.setDisplay('field1', true);
            g_form.setDisplay('field11', true);
            g_form.setMandatory('field1', true);
            g_form.setMandatory('field11', true);
            break;
        case '2':
            g_form.setDisplay('field1', true);
            g_form.setDisplay('field11', true);
            g_form.setDisplay('field2', true);
            g_form.setDisplay('field22', true);
            g_form.setMandatory('field1', true);
            g_form.setMandatory('field11', true);
            g_form.setMandatory('field2', true);
            g_form.setMandatory('field22', true);
            break;
        case '3':
            g_form.setDisplay('field1', true);
            g_form.setDisplay('field11', true);
            g_form.setDisplay('field2', true);
            g_form.setDisplay('field22', true);
            g_form.setDisplay('field3', true);
            g_form.setDisplay('field33', true);
            g_form.setMandatory('field1', true);
            g_form.setMandatory('field11', true);
            g_form.setMandatory('field2', true);
            g_form.setMandatory('field22', true);
            g_form.setMandatory('field3', true);
            g_form.setMandatory('field33', true);
            break;
        default:
            // 'none' or '0' case
            break;
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

rajeshraji1
Tera Expert

Hi @Ankur Bawiskar 

 as you mentioned code is not working as expected when we select the value vise versa its behave different.

 

you have any UI policy suggestion ?