How to check is an attachment has been added to a request

Nikkikobako
Giga Contributor

Hi I have used a number of suggestions I found online to check if an attachment is attached but most just error. The closest i get is below I check first if a selection is yes then if it is I run the code below: 

This gives me alert 2 msg then alert 3 msg with a value of 0 if attached or not, 

Any help would be very much appreciated, thank you 🙂 

function onSubmit() {
var myFieldValue = g_form.getValue('polygon_details_attached');

//Check if meant to have an attachment
if (myFieldValue == "Yes") {
  var cat_id = 'sysparm_item_guid'.value;
  var gr = new GlideRecord("sys_attachment");
  alert ("its yes 2");
  gr.addQuery("table_name", "sc_cart_item");
  gr.addQuery("table_sys_id", cat_id);
  gr.query();
  alert("its yes3 = " + gr.getRowCount());

  if (!gr.next()) {
     alert("You must attach a file to submit.");
    return false;
  }
}
}

1 ACCEPTED SOLUTION

AbhishekGardade
Giga Sage

This may help:

Possible Ways for Making an Attachment Mandatory : Service Portal/Native UI

Please mark as Correct Answer and Helpful.
Thank You!
Abhishek Gardade

Thank you,
Abhishek Gardade

View solution in original post

17 REPLIES 17

Mandeep Karan
Tera Guru

is this in Service Portal or native UI?

Service Portal I have used a catalog client script

try now. 

 


function onSubmit() {
var myFieldValue = g_form.getValue('polygon_details_attached');

//Check if meant to have an attachment
if (myFieldValue == "Yes") {
  var cat_id = g_form.getUniqueValue();
  var gr = new GlideRecord("sys_attachment");

  gr.addQuery("table_name", "sc_cart_item");
  gr.addQuery("table_sys_id", cat_id);
  gr.query();
  alert("its yes3 = " + gr.getRowCount());

  if (!gr.next()) {
     alert("You must attach a file to submit.");
    return false;
  }
}
}

no 😞 count is still showing as 0 when i attach a file