How to make field mandatory in a variable set based on the another variable set field value

sunil7
Giga Expert

HI Guys, Need a help to make field mandatory inside a variable set based on the selection of field inside another variable set on the same catalog item. For ex. I have a catalog item Paper and Supplies , we have two variable set Test1 and Test2. Test1 has two fields(Field1 and Field2) and Test2 has two field(Field3 and Field4).

My requiremt is when we enter value 1 in Field1 then Field3 should become mandatory. Please see screenshot below

find_real_file.png

find_real_file.pngfind_real_file.png

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Here it is.

Created onLoad() client script on test2() variable set.

function onLoad() {
    var test1Json = parent.g_form.getValue('test1');
    var test1 = JSON.parse(test1Json);
    for (var i = 0; i < test1.length; i++) { // loop through all rows to check if there is field1 == 1
        if (test1[i].field1 == 1) {
            g_form.setMandatory('field3', true);
            break;
        }
    }
}

Create onSubmit() client script on the form to catch form being submitted when field1=1 and field3 is empty.

This can happen if test2 variable set is filled before test1 variable set.

function onSubmit() {
    g_form.hideFieldMsg('test2');
    var field3Mandatory = false;
    var test1Json = g_form.getValue('test1');
    var test1 = JSON.parse(test1Json);
    for (var i = 0; i < test1.length; i++) {
        if (test1[i].field1 == 1) {
            field3Mandatory = true;
        }
    }
    if (field3Mandatory) {
        var test2Json = g_form.getValue('test2');
        var test2 = JSON.parse(test2Json);
        for (var j = 0; j < test2.length; j++) {
            if (test2[j].field3 == '') {
                g_form.showFieldMsg('test2', 'field3 is mandatory', 'error');
                return false;
            }
        }
    }
}

Execution result:

Test 1. Field1 = 1, Field2= 2 -> Field3 is mandatory

find_real_file.png

Test 2. Field1 = 2, Field2 = 2. Field3 is not mandatory

 find_real_file.png

Test 3:

Step 1. Field3 = empty, Field4 = 3

Step 2. Field1 = 1, Field2 = 2 

Step 3. Press "Submit" -> show error message that Field3 is mandatory.

find_real_file.png

Step 4. Field3 = 3

Step 5. Press "Submit" -> Request processes.

 

 

View solution in original post

12 REPLIES 12

Hey Ankur 

Please suggest if anyway is available

Not possible as my per understanding since both the MRVS are separate and outside variables cannot be made mandatory etc if you write client script/ui policy on MRVS variable

Regards
Ankur

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

Oh Okay Ankur, Thanks 

You can try to use onSubmit based validation which is mentioned below

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

Hitoshi Ozawa
Giga Sage
Giga Sage

Here it is.

Created onLoad() client script on test2() variable set.

function onLoad() {
    var test1Json = parent.g_form.getValue('test1');
    var test1 = JSON.parse(test1Json);
    for (var i = 0; i < test1.length; i++) { // loop through all rows to check if there is field1 == 1
        if (test1[i].field1 == 1) {
            g_form.setMandatory('field3', true);
            break;
        }
    }
}

Create onSubmit() client script on the form to catch form being submitted when field1=1 and field3 is empty.

This can happen if test2 variable set is filled before test1 variable set.

function onSubmit() {
    g_form.hideFieldMsg('test2');
    var field3Mandatory = false;
    var test1Json = g_form.getValue('test1');
    var test1 = JSON.parse(test1Json);
    for (var i = 0; i < test1.length; i++) {
        if (test1[i].field1 == 1) {
            field3Mandatory = true;
        }
    }
    if (field3Mandatory) {
        var test2Json = g_form.getValue('test2');
        var test2 = JSON.parse(test2Json);
        for (var j = 0; j < test2.length; j++) {
            if (test2[j].field3 == '') {
                g_form.showFieldMsg('test2', 'field3 is mandatory', 'error');
                return false;
            }
        }
    }
}

Execution result:

Test 1. Field1 = 1, Field2= 2 -> Field3 is mandatory

find_real_file.png

Test 2. Field1 = 2, Field2 = 2. Field3 is not mandatory

 find_real_file.png

Test 3:

Step 1. Field3 = empty, Field4 = 3

Step 2. Field1 = 1, Field2 = 2 

Step 3. Press "Submit" -> show error message that Field3 is mandatory.

find_real_file.png

Step 4. Field3 = 3

Step 5. Press "Submit" -> Request processes.