6 Replies · Latest reply on Jun 19, 2017 1:42 PM by Shishir Srivastava

    make attachment mandatory based on variable

      I was able to find a script that makes my attachment mandatory. So it checks if we have an attachment in my cart.

      However, I have two variables 'block ' and 'unblock'. If we select block we don't require any attachment, but if we select unblock we require an attachment.

       

      This script works for both. it does require an attachment even if we select block.

           On submit script:

      function onSubmit() {  
         var cat_id = gel('sysparm_item_guid').value;  
         var gr = new GlideRecord("sys_attachment");  
         gr.addQuery("table_name", "sc_cart_item");  
         gr.addQuery("table_sys_id", cat_id);  
         gr.query();  
         if (!gr.next()) {  
            if(g_form.getValue('unblock') == true){
          alert("You must attach a screen shot of the website with the ticket before proceeding.");  
         }
            return false;  
         }  
      

       

       

       

       

        • Re: make attachment mandatory based on variable
          Shishir Srivastava

          Hi Claudia,

           

          Please note that block or unblock is not the bool type field so checking in if condition for true or false will not help. You need to check if the value matches then proceed. Kindly check if below code helps.

           

          1. function onSubmit() {   
          2.    var cat_id = gel('sysparm_item_guid').value;   
          3.    var gr = new GlideRecord("sys_attachment");   
          4.    gr.addQuery("table_name", "sc_cart_item");   
          5.    gr.addQuery("table_sys_id", cat_id);   
          6.    gr.query();   
          7.    if (!gr.next()) {   
          8.       if(g_form.getValue('block_or_unblock') == 'unblock'){ 
          9.      alert("You must attach a screen shot of the website with the ticket before proceeding."); 
          10.      return false 
          11.    }      
          12.    }   

          Thank you!

          Regards,
          Shishir Srivastava

          PS: Your feedback (Like, Helpful or Correct) helps community

          • Re: make attachment mandatory based on variable
            Amlan Pal

            Hi Claudia,

             

            The above solution provided by explorenow  will work. But I would like to tweak the script or better system performance.

            Let me clarify. The script is a onSubmit type Catalog Client Script and will work every time when the item is submitted. Now, in the script the 'sys_attachment' table is getting queried first and the condition is checked later. Even if the selected value is 'Block', the script will query the table every time on submission of the item. This will impact on system performance, even if small, but it will impact certainly. So all you need to do is to check the condition first then query the table. Please find the script provided below.

             

            function onSubmit() {

              if(g_form.getValue('variable_name_of_block_or_unlock') == 'unblock'){ //Checks whether the variable value is 'unblock'

              var cat_id = gel('sysparm_item_guid').value;

              var gr = new GlideRecord("sys_attachment");

              gr.addQuery("table_name", "sc_cart_item");

              gr.addQuery("table_sys_id", cat_id);

              gr.query();

              if (!gr.next()) {

              alert("You must attach a screen shot of the website with the ticket before proceeding.");

              return false;

              }

              }

            }

             

            I hope this helps.Please mark correct/helpful based on impact

            Thanks,
            Amlan Pal