The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to Make Attachments Mandatory on a Record Producer Only for Specific conditions

Sushma Pavani K
Tera Guru

In many business scenarios, users must attach supporting documents when submitting a Record Producer form.

While ServiceNow provides a Mandatory Attachment checkbox, it doesn’t always cover conditional use cases.

 

This article explains how to enforce mandatory attachments only when the case type is "broken", using a Client Script with the onSubmit type.

 

Use Case

You have a Record Producer with a field called Case Type.

If the user selects “broken”, they must attach at least one file before submitting.

For all other case types, attachments are optional.

 

Problem Statement

The standard Mandatory Attachment checkbox applies to all submissions. However, business requirements may dictate that only certain case types (e.g., “broken”) require attachments.

You need a way to validate dynamically before submission.

 

Solution – Client Script

By creating an onSubmit client script, you can check both the Case Type value and whether attachments are present.

 

Script Example

function onSubmit() {
    // Check only if case type is "broken"
    if (g_form.getValue('u_case_type') == 'broken') {
        if (document.getElementsByClassName('get-attachment').length == 0) {
            g_form.addErrorMessage("Please attach a file before submitting.");
            return false; // Prevent form submission
        }
    }
    return true; // Allow submission otherwise
}

Configuration Steps

  1. Open the Record Producer in your ServiceNow instance.

  2. Under Related Lists, create a new Client Script.

  3. Configure it with:

    • UI Type: All

    • Type: onSubmit

    • Applies to: A Catalog Item

  4. Copy and paste the script above.

  5. Save and test.

 

Behavior

  • If Case Type = broken and no attachment is present
    The user will see:

    “Please attach a file before submitting.”
    and the form submission will be blocked.

  • If Case Type = broken and attachment is present
    The form submits successfully.

  • If Case Type is anything else
    The form submits without requiring attachments.

 

Result: You now have a conditional way to enforce mandatory attachments only when Case Type = broken, giving you more flexibility than the standard checkbox.

1 REPLY 1

GlideFather
Tera Patron

Hi @Sushma Pavani K,

 

there's alternative option - and I'd say easier - hide the attachment section in the bottom part of the field:

GlideFather_0-1758398450327.png

 

 

then create a new variable that is type: attachment

GlideFather_2-1758398499964.png

 

 

And then just create 1) catalog client script or 2) catalog UI policy where you will configure the exact behaviour when this is supposed to be mandatory or not. 

 

It can be always visible in case that the attachment would be optional, just enforcing the mandatory attribute under specific conditions can be achieved by this. And also, you can add field messages.

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */