最近搜索
没有最近搜索
Requiring a ticket attachment if a particular dropdown option is selected
已于 2019年3月27日 发布
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 条评论
Carlo Ligthart
Hi 367254856428
Thank you for your quick reply.
I did what you suggested and I get a different output:
However, this results in 1:
0
Karen D Snyder
366686281727 this problem that you and 397366577434 are having is very strange. Can you locate this line in your code:
And add this code under it:
Then open the development console and run your test. What is output in the console? When I tested in the Copenhagen theme where I added Fikri's code, this was my output:
0
Carlo Ligthart
Hi Karen D Snyder
First of all: Thank you for this article!
I encounter the same problem as Fikri Akbar Hedianto ("Attachment: cannot be blank" message). I followed your steps exactly in the console and it indeed changed to: "Hello!"
Any advice?
0
Karen D Snyder
397366577434
I used a default Copenhagen theme to make sure that I was starting from scratch, and put your code into script.js, inside the document ready function. The only change that I made was to change your field ID to the field ID of my form's checkbox in the line that sets attachmentCheckboxField. Note that the code that you pasted above has an extra } at the end, which caused a console error.
When I removed the extra }, that fixed the console error. When I opened the form with the checkbox and clicked Submit without attaching anything, I got the correct error notification Report must be attached.
So does your code have the extra } at the end? That could be the problem.
0
Fikri Hedianto
Hi Karen D Snyder,
I have tried to check in the console, everything is working fine. But it seems like the "if" for error check is not being read at all on my end.
Below is my code, I may miss something here.
// 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;
}
};
// 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_360026907032';
var attachmentCheckboxId = '#' + attachmentCheckboxField;
var attachmentErrorNotification = 'Report must be attached';
var formObserveMutationOptions = { childList: true, subtree: true };
function setFormAttachmentCheckbox() {
if ($('#request-attachments-pool .upload-item').length) {
selectCheckbox(attachmentCheckboxId);
}
else {
clearCheckbox(attachmentCheckboxId);
}
}
// Clear checkbox
function clearCheckbox(eltselector) {
$(eltselector).prop('checked', false);
}
// Select checkbox
function selectCheckbox(eltselector) {
$(eltselector).prop('checked', true);
}
// If attachment checkbox field exists, select it,
// and watch for changes to attachments and dropdown
if ($(attachmentCheckboxId).length) {
clearCheckbox(attachmentCheckboxId);
startObserveMutations('#request-attachments-pool', formObserveMutationOptions, mutationObservedForm);
}
// Adjust attachment error notification
var attachmentErrorElt = $('.' + attachmentCheckboxField + ' .notification-error');
if (attachmentErrorElt.length) {
attachmentErrorElt.text(attachmentErrorNotification);
}
}
0
Karen D Snyder
Jennifer Neer,
The way that the code should work is that your form should contain a custom checkbox field that is hidden. It should be selected at the start. Then if the specific option is selected by the user (or in your case, a certain form is selected), the checkbox is selected by code if there is at least one attachment, or cleared if there are no attachments. One way to get an idea of what is going on would be to show the checkbox, and observe whether it is ever selected. You can do this by entering the following in the developer console, where you replace XXX with the custom field ID:
Feel free to paste in the code, and then I can take a look to see if I can spot anything.
0
Jennifer Neer
Hello,
I'm trying this code on our help desk but I'm having an issue where it still displays the error even when I there is an attachment present and I'm not quite sure where the issue is. I have also made to add the code for ObserveMutations. Any guidance as to where to look? I'm also requiring the attachment based on the form that is selected, not a custom field, in case that makes a difference.
I'm happy to paste in any of the code to look over as well. Thanks!!
0
Karen D Snyder
397366577434,
In your script.js, there should be a line of code like the below, where XXXXX is the custom field ID for the checkbox:
Please try this:
1. Do whatever scenario you need, to get the notification "Attachment: cannot be blank" to appear
2. In the developer tools console, please type the following, replacing the first line with the line from your code::
var attachmentCheckboxField = 'request_custom_fields_XXXXX';
var attachmentErrorElt = $('.' + attachmentCheckboxField + ' .notification-error');
attachmentErrorElt.length;
Is the displayed length 1? If not, then there is a problem somewhere in the code.
If the displayed length is 1, then type the following in the console, and let me know if the displayed notification changes to Hello!
This is my console output from doing the above steps:
When I typed the last command, the notification changed:
0
Fikri Hedianto
Karen D Snyder
I am able to receive error message but just the default "Attachment: cannot be blank" message when I already put the script below which should change the error notification message:
var attachmentErrorNotification = 'Report must be attached';
I also have added the startObserveMutations code to observe the change in the form and everything is working, only the error notifications that seems to be skipped.
0
Karen D Snyder
Hi 397366577434,
Just to double-check, I did realize that my original post had omitted the function startObserveMutations, which I provided in this comment: https://support.zendesk.com/hc/en-us/community/posts/360028913434/comments/360009177754. Did you see that comment, and add the code for startObserveMutations?
If you did add the code for startObserveMutations, then can you explain further in what way the code is not working: Is an error notification appearing like the one below when you click Submit and you have no attachment?
Or are you not getting any error notification at all when you click Submit and you have no attachment?
Please let me know, and then I can trouble-shoot further.
0
请先登录再写评论。