Requiring a ticket attachment if a particular dropdown option is selected

44 Comentarios

  • CJ Johnson

    Is this possible to do without requiring a drop down field to have something selected? I want to require an attachment on a form that doesn't have any drop downs. 

    2
  • Katie Sullivan

    Here's a simpler way to enforce that users upload an attachment before submitting a request. These changes should be added to the script.js. 

    1. Disable the submit button on the form you wish to enforce attachment upload.

    const button = document.querySelector('[value="Submit"]');
    button.disabled = true;

    2. Change the attachments default label to let the user know an attachment is required.

    $('label[for="request-attachments"]').html('<b>Attach file template to proceed</b>.');  

    3. Add an event listener to the attachments area to detect changes, and only enable the submit button (and change warning text) if there is at least one file attached.

     document.getElementById('request-attachments').addEventListener('change', function(event) {
      var fileInput = event.target;
      var fileCount = fileInput.files.length;
       if (fileCount > 0) {
         attachmentLabel.text("Attachments"); 
         button.disabled = false;
       }
      });

    Here's how it looks on the submit a request page, before and after attachment upload:

    Before:

    After:

    Hope this helps someone else!

    2
  • rhys

    Anyone got a workaround for jQuery now not being available in Templating API v2?

    1
  • CJ Johnson

    Update: I have a working solution for my comment above! 

    First, I totally didn't realize that Fikri had already posted what I asked about above. Minus that final bracket, it's a perfect working code example of requiring an attachment on a form, period, no other field requirements. @... sorry to ping you about a year old comment on a Zendesk article but your comment on this saved me hours of work and headaches, you are my hero this week!! 

    Second, I am not confident the more official code example in the guide still works. I tried so many things to get it to work but also consistently got that 'formAttachmentDropdownClass is not defined', and while I'm an absolute beginner at javascript, I gotta say that I don't think it's an implementation issue, I can't see anywhere that it's being defined, either. 

    Fortunately for me, and possibly for whoever is reading this comment, you can still use Fikri's code to require an attachment on a form because it never references that class, because a dropdown isn't needed for that version. 

    1
  • Anonimas Anonimas

    Hi,

    Where to find # for attachmentCheckboxField? I did everything as explained, but the error message doesn't pop up at all. I assume I'm missing this ID. Any suggestions?

    Talking about this part:

    var attachmentCheckboxId = '#' + attachmentCheckboxField;

    Thanks!

    1
  • Jan Schweigerer

    I'm sorry, but I can't believe this is not a standard feature available via forms.

    This is such a basic use case which thousands of customers will require, and we have to build some complicated, messy workaround on our own in order to realize it.

    It baffles me how expensive this tool is for the amount of features it offers.

    1
  • Karen D Snyder

    Sorry, I need to make a change in what I wrote above. When you create the custom field, you should not select the option Required to solve a ticket.

    0
  • Brett Bowser
    Zendesk Community Manager

    Thanks for sharing this Karen :)

    This is awesome!

    I've also updated your post to remove the Required to solve ticket option.

    Cheers!

    0
  • Jennifer Rowe
    Zendesk Documentation Team

    I love this tip! Thanks so much for sharing it, Karen.

    0
  • Kevin Witt

    Great article, but I am having problems with the js.  I get the following error in console "startObserveMutations is not defined".

    Am I missing something?

    0
  • Trapta Singh
    Community Moderator

    Hi @...,

    Can you share a bit more detail here? Like the screen shot of the code of the URL of you HC?

    Thanks

    0
  • Kevin Witt

    Sure, a configured as per the article (Except I didn't hide the checkbox at this point so I can see what is happening).

    I have a drop-down that determines if the attachment is required, with the options "Attachment not required" and "Attachment required".

    The error I see in the console is:

    The HC is in my sandbox: https://unifydemo1508140682.zendesk.com

    The form is called "Attachment Test".

    My JS is as follows:

    // 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_360003651537';
    var attachmentCheckboxId = '#' + attachmentCheckboxField;
    var attachmentErrorNotification = 'Report must be attached';
    var formDropdownClass = '.request_custom_fields_360003651357';
    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() === 'Attachment required';
    }

    // Select checkbox
    function selectCheckbox(eltselector) {
    $(eltselector).prop('checked', true);
    }

    // Clear checkbox
    function clearCheckbox(eltselector) {
    $(eltselector).prop('checked', false);
    }

    // If attachment checkbox field exists, select it,
    // and watch for changes to attachments and dropdown
    if ($(attachmentCheckboxId).length) {
    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
  • Karen D Snyder

    Oops, I wrote the post above with the JS to do this, and it looks like I left out the code for the startObserveMutations function! Sorry about that, here it is:

    // 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;
      }
    };

     

    Hopefully this will fix the problem.

    [Edit: I edited the original post to add the function]

    0
  • Trapta Singh
    Community Moderator

    @..., as @... mentioned, you need to write the startObserveMutations function. Also, looking at your code, you are looking for the changes in the attachment field and in your code you are observing the checkbox for changes.

    Thanks

    0
  • Kevin Witt

    A big thank-you to @..., you saved me allot of time with this.

     

    0
  • Karen D Snyder

    Glad it's working, sorry for leaving out the function in the first place!

    0
  • Fikri Hedianto

    Hi @...,

     

    I tried to use the script above and some adjustment for my case its working out. But I have some issue where the code to check the attachment error notification below doesn't work.

    // Adjust attachment error notification
    var attachmentErrorElt = $('.' + attachmentCheckboxField + '.notification-error');
    if (attachmentErrorElt.length) {
    attachmentErrorElt.text(attachmentErrorNotification);
    }

    Do you know what could possibly cause this? Thanks before for providing us the idea of solution to this case!

     

    0
  • Karen D Snyder

    Hi @...,

    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
  • Fikri Hedianto

    @...

     

    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

    @...,

    In your script.js, there should be a line of code like the below, where XXXXX is the custom field ID for the checkbox:

    var attachmentCheckboxField = 'request_custom_fields_XXXXX';

    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!

    attachmentErrorElt.text('Hello!');

    This is my console output from doing the above steps:

    When I typed the last command, the notification changed:

    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

    @...,

    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:

    $('#request_custom_fields_XXX').show();

    Feel free to paste in the code, and then I can take a look to see if I can spot anything.

     

    0
  • Fikri Hedianto

    Hi @...,

    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

    @...

    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
  • Carlo Ligthart

    Hi @...

    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

    @... this problem that you and @... are having is very strange. Can you locate this line in your code:

    var attachmentErrorElt = $('.' + attachmentCheckboxField + ' .notification-error');

    And add this code under it:

    console.log('attachmentErrorElt.length: ' + attachmentErrorElt.length);
    console.log('attachmentErrorNotification: ' + attachmentErrorNotification);

    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 @...

    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

    @... The notification isn't being changed, because attachmentErrorElt is not referencing the element that contains the attachment checkbox! We just have to figure out why! Perhaps the custom field ID is not correct?

    If I hover over the checkbox, and then right-click and select Inspect, this is what I see in the console:

    The highlighted element is the input element of type checkbox. A few lines above it is the <div> element that we are trying to reference, which:in my case has class request_custom_fields_360017775731:

    [If you have hidden your checkbox, then you'll need to show it by removing the CSS that hides it.]

    In my code, I have the line below that sets the variable attachmentErrorElt; note that the text inside the quotes matches the class of the <div>:

    var attachmentCheckboxField = 'request_custom_fields_360017775731';

    So the question is, does your code match what you see when you inspect the element?

     

    0
  • Carlo Ligthart

    Hi @...

     

    When I inspect  the checkbox I see the following:


    I have the following set in the code:

    var attachmentCheckboxField = 'request_custom_fields_360028787572';


     

    Here is the full code if that might help:

    // 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_360028787572';
    var attachmentCheckboxId = '#' + attachmentCheckboxField;
    var attachmentErrorNotification = 'Report must be attached';
    var formDropdownClass = '.request_custom_fields_360028850151';
    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 it,
    // and watch for changes to attachments and dropdown
    if ($(attachmentCheckboxId).length) {
    selectCheckbox(attachmentCheckboxId);
    startObserveMutations('#request-attachments-pool', formObserveMutationOptions, mutationObservedForm);
    startObserveMutations(formDropdownClass, formObserveMutationOptions, mutationObservedForm);
    }

    // Adjust attachment error notification
    var attachmentErrorElt = $('.' + attachmentCheckboxField + ' .notification-error');

    //Logging on request of Karen D Snyder
    console.log('attachmentErrorElt.length: ' + attachmentErrorElt.length);
    console.log('attachmentErrorNotification: ' + attachmentErrorNotification);

    if (attachmentErrorElt.length) {
    attachmentErrorElt.text(attachmentErrorNotification);
    }

    0
  • Karen D Snyder

    @... I copied your code into a default Copenhagen theme and got a console error that startObserveMutations is not defined. I'm not sure if you just forgot to include it in your post.

    Also, I realized that I should have had you inspect the element with the error notification. When I inspect it, it looks like this:

     

    0

Iniciar sesión para dejar un comentario.

Tecnología de Zendesk