
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 06-11-2024 12:15 PM
Introduction
In many business scenarios, it's crucial to ensure that records submitted through a form have all the necessary attachments. This can prevent incomplete or invalid submissions that might lead to delays and additional follow-up work.
There are two common variations of this requirement:
-
Specific Categories of Files: Some forms may require specific categories of files to be attached before submission. For instance, a Travel Expense form might need a receipt for airfare and another for lodging. In such cases, dedicated properties should be used to store each attachment instead of using the attachment component. You would add two fields of type File Attachment and make them mandatory.
-
At Least One File Attached: In other scenarios, the form only needs to ensure that at least one file is attached, and the type of file does not matter.
This article presents a way to support the second scenario, ensuring that a record has at least one attachment before submission. Here’s how to achieve this in ServiceNow UI Builder.
Step-by-Step Guide
Overview
To address this need, we'll use the events triggered by the attachment component whenever users add or remove an attachment. These events will help us keep track of the number of files attached, and we will use this information to control whether the submit button is enabled or disabled. Essentially, we'll enable the submit button whenever at least one file is attached.
Step 1: Set Up the page
Set up a page with the following components:
- A form component
- A attachment component
- And a submit button
.
Step 2: Configure Client State Parameters
To manage the state of the submit button, you'll need client state parameters that will be updated based on the attachment events.
-
Add the
hasAttachments
Client State Parameter:- Go to the "Client State Parameters" section in UI Builder.
- Add a new client state parameter named
hasAttachments
and set its initial value tofalse
.
-
Add the
attachmentCount
Client State Parameter:- Go to the "Client State Parameters" section in UI Builder.
- Add another client state parameter named
attachmentCount
and set its initial value to0
.
Step 3: Map Events to Scripts
We'll now create scripts that update the client state parameters based on the attachment events, and then map these events to the scripts.
- Create a Client Script for "Attachment upload succeeded":
-
In the script editor, write a script to update the client state parameter
attachmentCount
and sethasAttachments
totrue
if there is at least one attachment. Here is the script: -
Name: Handle Attachment upload
- Script:
function handler({api, event, helpers, imports}) { const newCount = api.state.attachmentCount + 1; api.setState('attachmentCount', newCount); api.setState('hasAttachments', newCount > 0); }
-
- Create a Client Script for "Attachment delete succeeded":
- Write a script to decrement the
attachmentCount
and updatehasAttachments
accordingly. - Name: Handle Attachment Delete
- Script:
function handler({api, event, helpers, imports}) { const newCount = api.state.attachmentCount - 1; api.setState('attachmentCount', newCount); api.setState('hasAttachments', newCount > 0); }
- Write a script to decrement the
- Map the "Attachment upload succeeded" Event:
- Select the attachment component.
- Go to the "Events" section.
- Click on " + Add event mapping"
- Find the
Attachment upload succeeded
event. - Map this event to the
Handle Attachment Upload
client script. - Click Add.
-
Map the "Attachment delete succeeded" Event:
- Similarly, find the
Attachment delete succeeded
event in the attachment component. - Map this event to the
Handle Attachment Delete
client script. - Click Add.
- Similarly, find the
Step 3: Map Events to Scripts
Step 4: Bind the State Parameter to the Submit Button
Finally, we'll bind the client state parameter to the disabled property of the submit button to ensure it is only enabled when there are attachments.
- Select the Submit Button Component
- Go to the properties panel.
- Find the
disabled
property. - Click the Bind icon
- Select script and set the script to return
true
if there are no attachments, effectively disabling the submit button, andfalse
otherwise. Here is the simple script:function evaluateProperty({api, helpers}) { return ! api.state.hasAttachments; }
- Click Apply.
Step 5: Test Your Configuration
See that it's working as expected.
- Open the page: Notice that the submit button is disabled by default
- Upload an Attachment: See how the submit button becomes enabled when an attachment is uploaded.
By following these steps, you can effectively manage the attachment requirement before allowing the submission of a record in the ServiceNow UI Builder workspace. This ensures that users cannot submit records without adding the necessary attachments.
I hope this article was helpful and inspires you to create an even better user experience. With these techniques, you can enhance your forms to ensure they meet all necessary requirements before submission.
- 1,286 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you Marc for sharing this solution! I believe we can use this in the Catalog submission form as well!!
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks for this article it was helpful, how can we configure event if attachments are added from side bar component i dont find the event attachment upload succeeded

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Liyakhat can you share a screenshot of your screen? As you can see below in my workspace i can select the attachment tab item and select the Attachment upload succeeded event.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@Marc Mouries Thanks for the post. When form load with attachment already added, How to get the existing attachment count?

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@SundaramR please create a new question and elaborate about the use case