Requiring a ticket attachment if a particular dropdown option is selected
Posted Mar 27, 2019
Update from Community Team: jQuery has removed in v2 themes and this tip doesn't work without jQuery.
How to require an attachment in a ticket if a particular dropdown option is selected
We needed to require an attachment if a certain dropdown option was selected by the user when they are filling out a certain form to submit a ticket. Without being able to require the attachment, extra time would need to be spent by the agent to contact the user and ask for the additional document.
Solution Overview
This was done by creating a custom checkbox field that is set to be required, and adding the checkbox to the form, hiding the checkbox via CSS, then programatically selecting or clearing the checkbox according to the dropdown option selected and the number of attachments present.
Limitations
- This solution detects that an attachment has been uploaded, but doesn't verify that the attachment is the particular document that needs to be attached.
- This solution uses MutationObserver (https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver), which is not supported in some older browser versions
- A user could get around the requirement to upload an attachment by using the DOM inspector to show the checkbox, and then selecting it manually.
Steps
Add the custom checkbox field
Create the custom checkbox field
- Navigate to the Ticket Fields page
- Click Add field
- Select Checkbox type
- Under Permissions, select Editable for end users
- Enter whatever title you would like, in Title shown to agents and Title shown to end users. I used the title 'Attachment'
- Select the Required to submit a request option.
- Click Save to create the field
- Note the field ID shown in the list of ticket fields for the new ticket field
Add the custom checkbox field to the form
- Navigate to the Ticket Fields page
- Locate the form for which you need to require the attachment, and click it
- Drag the new field from the list of fields on the right to the list of fields in the form. Make sure that it is the last field in the form (because you want the error notification that appears if an attachment is required but not uploaded, to appear above the attachment upload box).
- Click Save form to save the change.
Hide the checkbox field
Add the following to style.css to hide the field label and the field (replace the custom field ID in this code with your custom field ID). You have to hide the field label and field individually instead of hiding the <div> that contains them, because if there is an error notification that an attachment wasn't uploaded, it is inside the <div>.
#request_custom_fields_360017775731_label,
#request_custom_fields_360017775731 {
display: none;
}
Set the checkbox according to the attachments and dropdown
1) Determine the field ID of the dropdown. You can do this from the Ticket Fields page or with the DOM inspector by opening the form in your Zendesk, and then inspecting the dropdown. The screenshot below shows the dropdown on the web page, and the element inspector portion that shows the field ID
2) Add the following code in script.js inside the document ready function, e.g.after $(document).ready(function () { : and before the ending line }); [You have to adjust the field IDs in attachmentCheckboxField and formDropdownClass to match your field IDs, and adjust the option checked in function isFormAttachmentRequired to match the particular option in your case]:
// Function to start observing node for mutations
var startObserveMutations = function (nodeSelector, options, callbackFunction) {
var node = document.querySelector(nodeSelector);
if (node) {
var observer = new MutationObserver(callbackFunction);
observer.observe(node, options);
return observer;
}
};
// Callback function to execute when mutations in form attachments or dropdown are observed:
// clear or select Attachment checkbox according to dropdown
var mutationObservedForm = function (mutationsList) {
mutationsList.forEach(function (mutation) {
if (mutation.type == 'childList') {
setFormAttachmentCheckbox();
}
});
};
// Define some variables for requiring form attachments
var attachmentCheckboxField = 'request_custom_fields_360017775731';
var attachmentCheckboxId = '#' + attachmentCheckboxField;
var attachmentErrorNotification = 'Report must be attached';
var formDropdownClass = '.request_custom_fields_360017548611';
var formObserveMutationOptions = { childList: true, subtree: true };
// Clear or select checkbox according to dropdown and attachments:
// Set Attachment checkbox if no attachments required, or if attachments are required and at least one is uploaded, otherwise clear it
function setFormAttachmentCheckbox() {
if (isFormAttachmentRequired()) {
if ($('#request-attachments-pool .upload-item').length) {
selectCheckbox(attachmentCheckboxId);
}
else {
clearCheckbox(attachmentCheckboxId);
}
}
else {
selectCheckbox(attachmentCheckboxId);
}
}
// Return true if dropdown option 'ABCD' is selected
function isFormAttachmentRequired() {
return $(formDropdownClass + ' a.nesty-input').attr('aria-expanded') &&
$(formDropdownClass + ' a.nesty-input').text() === 'ABCD';
}
// Select checkbox
function selectCheckbox(eltselector) {
$(eltselector).prop('checked', true);
}
// Clear checkbox
function clearCheckbox(eltselector) {
$(eltselector).prop('checked', false);
}
// If attachment checkbox field exists:
// Select the checkbox if attachment is not required
// Watch for changes to attachments and dropdown
if ($(attachmentCheckboxId).length) {
if (!isFormAttachmentRequired()) {
selectCheckbox(attachmentCheckboxId);
}
startObserveMutations('#request-attachments-pool', formObserveMutationOptions, mutationObservedForm);
startObserveMutations(formDropdownClass, formObserveMutationOptions, mutationObservedForm);
}
// Adjust attachment error notification
var attachmentErrorElt = $('.' + attachmentCheckboxField + ' .notification-error');
if (attachmentErrorElt.length) {
attachmentErrorElt.text(attachmentErrorNotification);
}
Note: The code that detects changes to the attachments and dropdown uses MutationObserver (https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver), as mentioned above. Basically you create an observer instance and pass it a callback function to execute when changes are observed. Then you start the observer, passing it the target node and options specifying what changes to observe.
When the above steps are done, and style.css and script.js are changed as shown above, then you will see that if you open the form and select the dropdown option that requires an attachment, and then click Submit, an error notification is displayed:
Hope this helps somebody!
0
61 comments
Ulises
Hi 1265174068049,
Regarding your comment above, how do you add the event listener? I was able to disable the submit button, but coding is not my strong point. Can you provide some guidance?
0
Jack Brown
Hi all,
I've tried following the above and can't get it to work! My coding side of things are not great…
However I wondered, if anyone has managed to make the Attachment field mandatory based on the Form the customer has selected rather than a field?
0
VK_0000
Sorry to ping this post years later. I ran the code above and I get the error message when no file has been uploaded. But could one of you explain how to remove the rest ‘Attachment (optional)’. No matter what I do, I cant seem to remove it.
0
Noelle Cheng
1263213581889 Does anyone know how to make it work for multiselect fields? I'm having trouble getting it to trigger for those. This is my code for just script.js and the bolded areas are the other elements that I changed.
// REQUIRE ATTACHMENTS FOR FORM WITH MULTISELECT
$(document).ready(function() {
// Function to start observing node for mutations
var startObserveMutations = function (nodeSelector, options, callbackFunction) {
var node = document.querySelector(nodeSelector);
if (node) {
var observer = new MutationObserver(callbackFunction);
observer.observe(node, options);
return observer;
}
};
// Callback function to execute when mutations in form attachments or dropdown are observed:
// clear or select Attachment checkbox according to dropdown
var mutationObservedForm = function (mutationsList) {
mutationsList.forEach(function (mutation) {
if (mutation.type == 'childList') {
setFormAttachmentCheckbox();
}
});
};
// Define some variables for requiring form attachments
var attachmentCheckboxField = 'request_custom_fields_123456789';
var attachmentCheckboxId = '#' + attachmentCheckboxField;
var attachmentErrorNotification = 'Documents selected to be submitted must be attached.';
var formMultiselect = '.request_custom_fields_987654321';
var formObserveMutationOptions = { childList: true, subtree: true };
// Clear or select checkbox according to dropdown and attachments:
// Set Attachment checkbox if no attachments required, or if attachments are required and at least one is uploaded, otherwise clear it
function setFormAttachmentCheckbox() {
if (isFormAttachmentRequired()) {
if ($('#request-attachments-pool .upload-item').length) {
selectCheckbox(attachmentCheckboxId);
}
else {
clearCheckbox(attachmentCheckboxId);
}
}
else {
selectCheckbox(attachmentCheckboxId);
}
}
// Return true if dropdown options are selected
function isFormAttachmentRequired() {
return $(formMultiselect + ' div.hc-multiselect-menu').attr('aria-checked') &&
$(formMultiselect + ' div.hc-multiselect-menu').text() === 'Selection1','Selection2','Selection3','Selection4';
}
// Select checkbox
function selectCheckbox(eltselector) {
$(eltselector).prop('checked', true);
}
// Clear checkbox
function clearCheckbox(eltselector) {
$(eltselector).prop('checked', false);
}
// If attachment checkbox field exists:
// Select the checkbox if attachment is not required
// Watch for changes to attachments and dropdown
if ($(attachmentCheckboxId).length) {
if (!isFormAttachmentRequired()) {
selectCheckbox(attachmentCheckboxId);
}
startObserveMutations('#request-attachments-pool', formObserveMutationOptions, mutationObservedForm);
startObserveMutations(formDropdownClass, formObserveMutationOptions, mutationObservedForm);
}
// Adjust attachment error notification
var attachmentErrorElt = $('.' + attachmentCheckboxField + ' .notification-error');
if (attachmentErrorElt.length) {
attachmentErrorElt.text(attachmentErrorNotification);
}
})
0
Helder Custodio(Eteg)
Hello everyone, I carry out the process here but the attachment fields are not being hidden and the attachments are not becoming mandatory, could you help me?
style.css
#request_custom_fields_31964770778516_label,
#request_custom_fields_31964770778516 {
display: none;
}
script.js
// Callback function to execute when mutations in form attachments are observed:
// clear or select Attachment checkbox according to attachments
var mutationObservedForm = function(mutationsList) {
mutationsList.forEach(function(mutation) {
if (mutation.type === 'childList') {
setFormAttachmentCheckbox();
}
});
};
// Define some variables for requiring form attachments
var attachmentCheckboxField = 'request_custom_fields_32026654442260';
var attachmentCheckboxId = '#' + attachmentCheckboxField;
var attachmentErrorNotification = 'Attachments cannot be blank';
var formDropdownClass = '.request_custom_fields_31964770778516, .request_custom_fields_30367309244953';
var formObserveMutationOptions = { childList: true, subtree: true };
// Clear or select checkbox according to attachments:
// Set Attachment checkbox if at least one attachment is uploaded, otherwise clear it
function setFormAttachmentCheckbox() {
if ($('#request-attachments-pool .upload-item').length >= 1) {
selectCheckbox(attachmentCheckboxId);
} else {
clearCheckbox(attachmentCheckboxId);
}
}
// Select checkbox
function selectCheckbox(eltselector) {
$(eltselector).prop('checked', true);
}
// Clear checkbox
function clearCheckbox(eltselector) {
$(eltselector).prop('checked', false);
}
// If attachment checkbox field exists:
// Watch for changes to attachments
if ($(attachmentCheckboxId).length) {
startObserveMutations('#request-attachments-pool', formObserveMutationOptions, mutationObservedForm);
}
// Adjust attachment error notification
var attachmentErrorElt = $(attachmentCheckboxId + ' .notification-error');
if (attachmentErrorElt.length) {
attachmentErrorElt.text(attachmentErrorNotification);
}
// Initial checkbox setting
setFormAttachmentCheckbox();
0
Noelle Cheng
6902233512986 so remove the below
then add this to the bottom?
0
C A
4513632515226 You can always use a second script to change the wording and remove it from the main script
0
Noelle Cheng
6902233512986 I tried your code versus mine but it ended up failing. I have 2 forms that need the same attachment message but another form that says something different. Here is my code for one of the forms that requires the attachment. I keep getting the issue where if I submit a ticket without the attachment if the dropdown selection requires it, the attachment field reverts back to “Attachment (optional)” instead of maintaining my custom verbiage.
0
C A
4513632515226, I copy pasted the code from my instance. You would just need to make sure that you swap out the IDs.
In the bit where you check for a certain drop down value
var formDropdownClass =
I have multiple drop down options added, as I needed to have attachments mandatory for multiple forms.That's how it looks like if no attachment is added

0
Noelle Cheng
6902233512986 Looks like I had misspelled one of the "formDropdownClass" fields. Although I am having an issue when testing that if there is no attachment but it's required, the attachment text reverts back to the “Attachments (optional)” text. If you have any insight that would be great! Thank you!
Prior to submitting request
Submitted request but submission failed due to no attachment – I need it to retain the custom Attachment verbiage.
0
Sign in to leave a comment.