- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-26-2022 09:22 PM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-27-2022 12:40 AM
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
Test 2. Field1 = 2, Field2 = 2. Field3 is not mandatory
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.
Step 4. Field3 = 3
Step 5. Press "Submit" -> Request processes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-27-2022 12:02 AM
Hey Ankur
Please suggest if anyway is available
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-27-2022 12:12 AM
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
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-27-2022 12:19 AM
Oh Okay Ankur, Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-27-2022 01:11 AM
You can try to use onSubmit based validation which is mentioned below
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā01-27-2022 12:40 AM
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
Test 2. Field1 = 2, Field2 = 2. Field3 is not mandatory
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.
Step 4. Field3 = 3
Step 5. Press "Submit" -> Request processes.