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

Sumit Maniktal1
Tera Expert

You can use g_service_catalog.object & make a call to the values of variables on main form e.g. 

 

function onLoad()

{

if (g_service_catalog.parent.getValue("address_type") == "ipv4")

{

                 g_form.setValue("ipv4_address", "XX.XX.XX.XX");

                 g_form.setVisible("ipv6_address", "false");

}

}

 

Try https://developer.servicenow.com/dev.do#!/reference/api/rome/client/g_service_catalogClientAPI

Using this method we can fetch catalog item variables inside another variable but not variable set variable

Mario Quinones1
Mega Guru

Hi Sunil,

You need to create a Catalog UI policy that applies to the whole catalog item.

Please mark as correct if this works for you.

Cheers,

Mario

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sunil,

I doubt this can be achieved. accessing another variable set's variable from 1st MRVS is difficult

Regards
Ankur

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