The CreatorCon Call for Content is officially open! Get started here.

How do I check file attachment in a form by clientscript

rock2freedom
Mega Contributor

How do I check file attachment in a form by clientscript

1 ACCEPTED SOLUTION

Mihir Mohanta
Kilo Sage

Hi Soumen,


No need to create a separate business rule for this.


Just query to sys_attachment table.


Please try below client script.



var sysid = g_form.getUniqueValue();


  var attach = new GlideRecord('sys_attachment');


  attach.addQuery('table_sys_id',sysid);


  attach.query();


  if(attach.next()){


  alert("form contains attachment");


  }




Please mark it as Helpful/Correct according to its impact.




Thanks,


Mihir


View solution in original post

11 REPLIES 11

Harish Murikina
Tera Guru

Hi Soumen,



If your trying to validate on catalog item below code works.






  Client script


_________________



function onSubmit()


{


                       


                             


                                                             


                                                              var ga = new GlideAjax('GiveIncludeScriptname');


                                                              ga.addParam('sysparm_name','isAttachmentAttached');


                                                              ga.addParam('sysparm_catalog_item_id',gel('sysparm_item_guid').value);


                                                              ga.getXMLWait();


                                                              var answer = ga.getAnswer();


                                                              try


                                                              {


                                                                                              if(answer == 'Not Attached')


                                                                                                {


                                                                                                                             


                                                                                                                                                              alert("You must attach a file to submit.");


                                                                                                                                                              return false;


                                                                                                    }


                                                                                             


                                                              }


                                                              catch(err)


                                                              {


                                                                                              console.log('Something going wrong while checking attachment mandatory :'+err.message);


                                                              }


                             


}







Include script


_________________



var GiveIncludeScriptname = Class.create();


GiveIncludeScriptname.prototype = Object.extendsObject(AbstractAjaxProcessor,{


                             


                              isAttachmentAttached : function() {


                                                             


                                                              var answer = 'Attached';


                                                              var cat_id = this.getParameter('sysparm_catalog_item_id');


                                                              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()) {


                                                                                              answer = 'Not Attached';


                                                                                             


                                                              }


                                                              return answer;


                              },


                             


                              type: ' GiveIncludeScriptname '


});










If your trying to validate on task based tables below code works.






  Client script


_________________



function onSubmit()


{


                       


                             


                                                             


                                                              var ga = new GlideAjax('GiveIncludeScriptname');


                                                              ga.addParam('sysparm_name','isAttachmentAttached');


                                                              ga.addParam('sysparm_taskbased_record_sysid', g_form.getUniqueValue(););


                                                              ga.getXMLWait();


                                                              var answer = ga.getAnswer();


                                                              try


                                                              {


                                                                                              if(answer == 'Not Attached')


                                                                                                {


                                                                                                                             


                                                                                                                                                              alert("You must attach a file to submit.");


                                                                                                                                                              return false;


                                                                                                    }


                                                                                             


                                                              }


                                                              catch(err)


                                                              {


                                                                                              console.log('Something going wrong while checking attachment mandatory :'+err.message);


                                                              }


                             


}







Include script


_________________



var GiveIncludeScriptname = Class.create();


GiveIncludeScriptname.prototype = Object.extendsObject(AbstractAjaxProcessor,{


                             


                              isAttachmentAttached : function() {


                                                             


                                                              var answer = 'Attached';


                                                              var task_id = this.getParameter('sysparm_taskbased_record_sysid');


                                                              var gr = new GlideRecord("sys_attachment");


                                                              gr.addQuery("table_name", "incident"); // validate attachment mandatory on incident record


                                                              gr.addQuery("table_sys_id", task_id);


                                                              gr.query();


                                                              if (!gr.next()) {


                                                                                              answer = 'Not Attached';


                                                                                             


                                                              }


                                                              return answer;


                              },


                             


                              type: ' GiveIncludeScriptname '


});






Regards,


Harish Murikinati.


try this, i have return it on documentation task.




function onSubmit() {


    //Type appropriate comment here, and begin script below


if(g_form.getValue('state') == 2)


  {



  var att = new GlideRecord("sys_attachment");  


  att.addQuery("table_name", "rm_doc");


  att.addQuery("table_sys_id",g_form.getUniqueValue('number'));


  //att.addQuery("sys_created_by", g_user.userName);  


  att.addQuery("sys_created_on", '>=', 'javascript:gs.minutesAgo(10)');  


  att.query();  


  if (!att.next()) {  


      alert("Please attach the relevant document before submiting.");  


      return false;  


  }


  return true;  




}


  }