Unable to make mandatory in catalog item Attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2025 02:31 AM
Unable to make mandatory in catalog item Attachment
I have tried to change attachment mandatory in catalog item this logic is working up to false attachment but after upload also i could not able to submit the catalog item kindly suggest .
- Labels:
-
Service Catalog
-
Service Desk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2025 09:01 PM
Hello @rajeshraji1,
- Create a Catalog Client Script:
- Navigate to "Service Catalog" > "Catalog Items".
- Select the catalog item and go to "Related Links" > "Catalog Client Scripts".
- Create a new script named something like "Check for Required Attachment".
- Set the script type to "onsubmit" and ensure "Isolate Script" is disabled (if using DOM manipulation).
- Write the Script Code:
function onSubmit() {
var condition = g_form.getValue('your_checkbox_variable'); // Replace with your condition variable
if (condition == true) {
var attachments = this.document.getElementsByClassName('get-attachment');
if (attachments.length == 0) {
g_form.addErrorMessage('Please attach a file.');
return false; // Prevent form submission
}
}
}
- Replace
'your_checkbox_variable'
With the actual name of the checkbox or other variable used to trigger the mandatory attachment requirement. - The code checks if the condition is met and then verifies if any attachments are present.
- If no attachments are found, an error message is displayed, and the submission is prevented.
-
1. Add the Script to the Catalog Item:
- In the catalog item, go to the "Related Links" > "Catalog Client Scripts" and add the script you created.
- In the catalog item, go to the "Related Links" > "Catalog Client Scripts" and add the script you created.
-
2. Consider UI Policies:
- If you need to control the visibility or mandatory status of the attachment field based on other variables, you can use UI policies.
- Create a UI policy that sets the visibility or mandatory status of the attachment field based on the value of a trigger field.
- If you need to control the visibility or mandatory status of the attachment field based on other variables, you can use UI policies.
- If you're using the Service Portal, you can also use the "Mandatory Attachment" checkbox in the catalog item's portal settings, as shown in the ServiceNow documentation.
- This setting automatically handles the mandatory requirement within the portal's UI.
-
Isolate Script:When using client scripts that manipulate the DOM (e.g., by checking for the presence of attachments using
getElementsByClassName
Ensure that "Isolate Script" is set to "false" to allow the script to interact with the form's elements. -
Service Portal:If you're using the Service Portal, leverage the "Mandatory Attachment" checkbox in the portal settings for simpler implementation.
-
Error Messages:Provide clear and informative error messages to users to indicate when an attachment is required.
-
Testing:Thoroughly test the implementation to ensure that the mandatory attachment functionality works as expected.If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in the future. It will be helpful to them.Thanks & Regards,Abbas Shaik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2025 10:34 PM
hi@Abbas_5 Your script is working fine with service portal but in test catalog view environment i am getting this error it make any impact in future
?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2025 10:37 PM
@Abbas_5 This script not working in catalog view making popup but working in SP view
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2025 10:14 PM
Hi @rajeshraji1
Please use the below onChange catalog client script:
var Dropdown_value = g_form.getValue('field_name');
if (Dropdown_value == '2') {
g_form.setVisible('attachment_field', true);
g_form.setMandatory('attachment_field', true);
} else {
g_form.setMandatory('attachment_field', false);
g_form.setVisible('attachment_field, false);
}
Note: Return true or false better works on onSubmit Client Scripts:
Please mark my answer as helpful/correct if it resolves your query. This will help other community members as well.
Regards,
Deepak Sharma