Mandatory Attachment Before State Change

amkatulak
Giga Expert

Hi,
I'm wondering if anyone has successfully implemented functionality that would require an attachment before a field on the form can change.   This is not on submission of a catalog item.

Here is our use case:

Legal department puts cases in a 'Pending' state.   In order for them to be removed from pending, the user needs to attach a document.   It can't be just any document attached either, the user would purposefully need to click a button that said something like "Remove from Pending - Doc Attached" or something.

Thanks in advance for any help or advice!

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Adam,



Here is the script. Just make sure you add a condition as per your req to trigger it. It will be client script.


Script :


function onSubmit() {


  var att = new GlideRecord("sys_attachment");


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


  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 requested form before submitting.");


      return false;


  }


  return true;


}


View solution in original post

13 REPLIES 13

dhasselquist
Mega Guru

We have one on a "On Submit" catalog client script:



function onSubmit() {


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


  var gr = new GlideRecord("sys_attachment");


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


  gr.addQuery("table_sys_id", cat_id);


  gr.query();


  if (!gr.next()) {


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


  return false;


  }


}



In your case I imagine you would want a business rule to enforce it, however. The example above should get you started, let us know if you need additional help.


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Adam,



Here is the script. Just make sure you add a condition as per your req to trigger it. It will be client script.


Script :


function onSubmit() {


  var att = new GlideRecord("sys_attachment");


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


  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 requested form before submitting.");


      return false;


  }


  return true;


}


Pradeep,



I am trying to implement this script in a catalog item so the attachment will be mandatory.   It works; however, when I attach a file, I am still prompted.   What am I missing?



function onSubmit() {  


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


      var gr = new GlideRecord("sys_attachment");


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


      gr.addQuery("table_sys_id", cat_id);


      gr.query();


      if (!gr.next()) {


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


      return false;


}



Thanks for your help!


I believe the table you want for a a catalog item is "sc_cart_item".