Make MRVS fields mandatory based on a variable outside the MRVS (Country + No of Children)

GhitaB
Tera Expert

Hi ServiceNow Community,

I’m trying to achieve the following behavior in a catalog item (Service Portal / Employee Center) and would appreciate some guidance or confirmation of best practice.

 

I have:

  • A variable no_of_children (outside the MRVS)

  • A Multi-Row Variable Set called Children Information

    • name

    • child_last_name

    • child_s_date_of_birth

    • child_s_status

Expected behavior:

  • Only when Country = Italy AND no_of_children > 0

    • The MRVS fields above should be mandatory

    • Users should not be able to submit unless all fields in each row are filled

Is there A supported way to make MRVS row fields mandatory based on a variable outside the MRVS?

Thanks in advance for any insight or confirmation of best practice 🙏
Happy to share code snippets if needed.

2 REPLIES 2

Ankur Bawiskar
Tera Patron

@GhitaB 

yes you can access outside variable in onLoad catalog client script which applies on MRVS

Syntax is this

var val = g_service_catalog.parent.getValue("no_of_children");

I created blog for this in past

Access value of catalog item variable within MRVS client script 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Terry_Yeti
Tera Contributor

Hey GhitaB,

I ran your requirements through snowcoder ai and it generated a solution for making MRVS fields mandatory based on parent catalog item variables. This is a common scenario when you need child row validation to depend on conditions set at the parent level.

Here's a Catalog Client Script you can use on your MRVS (set Type to "onLoad"):

```javascript
function onLoad() {
// Get parent catalog item variables using g_service_catalog.parent
var country = g_service_catalog.parent.getValue("country");
var numChildren = parseInt(g_service_catalog.parent.getValue("no_of_children")) || 0;

// Only make fields mandatory when conditions are met
if (country == "Italy" && numChildren > 0) {
g_form.setMandatory("name", true);
g_form.setMandatory("child_last_name", true);
g_form.setMandatory("child_s_date_of_birth", true);
g_form.setMandatory("child_s_status", true);
} else {
g_form.setMandatory("name", false);
g_form.setMandatory("child_last_name", false);
g_form.setMandatory("child_s_date_of_birth", false);
g_form.setMandatory("child_s_status", false);
}
}
```

**Key points:**

- **g_service_catalog.parent.getValue()** is the magic here. It lets you read variables from the parent catalog item while inside the MRVS context.
- You'll want to update the field names (name, child_last_name, etc.) to match your actual MRVS variable names.
- The parseInt() with a fallback to 0 handles cases where the number field might be empty.

**One thing to watch out for:** If users can change the Country or No of Children values after adding MRVS rows, you may also want an onChange script on those parent variables to refresh or revalidate the MRVS. Otherwise the mandatory state only gets set when the MRVS row loads.

Let me know if you need help adapting this to your specific field names or adding that onChange logic!

_______________________________________
I used snowcoder ai to generate this. If you need to tweak the requirements, you can run it through their Yeti AI for free.