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

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

I have modified the script but am receiving the same result.   After attaching a file, I am still prompted to attach a file and cannot submit:



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;


}


return true;


}


Kari,



I just attempted this in a fresh developer instance on Fuji and it worked as expected. I copy/pasted your code into a "On Submit" catalog client script on the item "Sales Laptop". Without attaching anything I am prompted when attempting to "Order Now", and attaching it lets me proceed.



Can you write down your steps as you test and post them here?


Testing Steps are:


1.   Service Catalog


2.   Select Application Enhancement (this take you to a Catalog Item)


3.   Complete form and choose Submit button and am prompted to attach a file.


4.   Attach file (have tried different files/types)


5.   Choose Submit and prompted to attach a file again, even though this was already completed.   Cannot continue.



This will eventually create an Enhancement in the SDLC app.   I just need the file attachment to be mandatory.


Okay, I think I'm understanding correctly.



Just to make sure, let's take a step back. I want you to open the item in question, and add an attachment. Then, go to the sys_attachment table, and find the file you attached. Let's make sure that the table name matches our glide record.



As a shortcut, if you enter "sys_attachment.list" in the navigation search box on the left it will take you there.



Post back what the table_name was for the attachment you added.


That was the piece I was missing.   I had to change the table to rm_enhancement.   It is now working.   THANK YOU!!!