Check Attachments on current form.

ankit025
Kilo Contributor

Is there a way to check for attachments in the current form and making attachment mandatory if a field is checked ?

1 ACCEPTED SOLUTION

Hi Ankit Lohani



Try the below script,If you are looking for mandatory attachments on catalog item.


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


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


return false;


}


}


View solution in original post

13 REPLIES 13

adiddigi
Tera Guru

If you are talking about ServiceCatalog form then,



  1. /*
  2.   For the Client Script on the request item/Record Producer
  3.   Name: Mandatory Attachments
  4.   Applies to: A Catalog Item
  5.   Type: OnSubmit
  6. */
  1. function onSubmit() {
  2. if (g_form.getValue('u_order_guide') == 'Yes') {
  3. var ord_id = gel('sysparm_cart_edit').value;
  4. var gr = new GlideRecord("sys_attachment");
  5. gr.addQuery("table_name", "sc_cart_item");
  6. gr.addQuery("table_sys_id", ord_id);
  7. gr.query();
  8. if (!gr.next()) {
  9. alert("You must attach a file to submit.");
  10. return false;
  11. }
  12. }
  13. else if (g_form.getValue('u_order_guide') == 'No') {
  14. var cat_id = gel('sysparm_item_guid').value;
  15. var gr = new GlideRecord("sys_attachment");
  16. gr.addQuery("table_name", "sc_cart_item");
  17. gr.addQuery("table_sys_id", cat_id);
  18. gr.query();
  1. if (!gr.next()) {
  2. alert("You must attach a file to submit.");
  3. return false;
  4. }
  5. }
  6. }

If you are talking about incident, change or some table then,


  1. var sys_id = gel('sys_uniqueValue').value;  
  2. var attachment = new GlideRecord('sys_attachment');  
  3. attachment.addQuery('table_name','incident');
  4. attachment.addQuery('table_sys_id',sys_id);  
  5. attachment.query();
  6. if (attachment.next()) {  
  7.       alert ('There is an attachment.');  
  8. }
  9. else {  
  10.       alert ('There is no attachment.');  
  11. }



Please dont use any Business Rule, you will be wasting a Server - Client transaction.



Now that using the above snippet you can know if an Attachment is attached or not.   If that field you are talking is checked, let the onsubmit run, if not skip it.



Both the snippets aren't written by me.   We have something similar but not exactly similar. The second one is written by Geoff Cox and the first I don't know who.


Hey Abhiram, I used this script in the catalog item,



function onSubmit() {


  if (g_scratchpad.hasAttachments)


  {}


else


{alert('You need to attach a SQL script to this form. ' );}


}



and that is not doing anything.


What is going on ?


I am not sure why you are using this on Catalog Item, I mean did you see this somewhere?



Also can you try using the script I pasted above?


Where do you think this should be used instead of the catalog item ?