Mandatory Attachments Order Guide and Catalog Items sharing solution

jeff_lord
Giga Contributor

To start I'm pretty new to ServiceNow. Only been working with it for about 3 months.
I had some requests to make attachments mandatory on catalog items.
When I added that request to an order guide it broke and wouldn't check for an attachment or submit.
I tried many different options and variations and finally go one to work and just wanted to share the solution that works.

I used the many articles on the forums for this solution. 🙂 So i have to say thank you to everyone.

The pieces:
On your catalog item (Requests or Record Producers)
After all your other variables create on called "u_order_guide" or whatever you wish (but make sure you modify all to match)
This variable must be a Yes/No and have the default value set to No.
Create a UI Policy that hides this variable onLoad.
Create a client script as follows:



/*
For the Client Script on the request item/Record Producer
Name: Mandatory Attachments
Applies to: A Catalog Item
Type: OnSubmit
*/

function onSubmit() {
if (g_form.getValue('u_order_guide') == 'Yes') {
var ord_id = gel('sysparm_cart_edit').value;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", ord_id);
gr.query();
if (!gr.next()) {
alert("You must attach a file to submit.");
return false;
}
}
else if (g_form.getValue('u_order_guide') == 'No') {
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;
}
}
}


Then on your Order Guide:
Make sure that Cascading Variables is checked or this will not work.
Create the same exact variable after all your other variables called "u_order_guide" and make it a Yes/No but make the default value be 'Yes'

Then create a Catalog Client Script as follows:



/*
For the Catalog Client Script that applies to the Order Guide
Name: Hide Order Guide Variable
Applies to: A Catalog Item
Type: OnLoad
Catalog Item: YOUR ORDER GUIDE
Applies on: check all boxes you need
*/

function onLoad() {
var item = $("current_item");
var guide = $("sysparm_guide");
if (item != null && guide != null && item.value == guide.value)
g_form.setDisplay('u_order_guide', false);
}


If you have any suggestions or comments I am happy to refine my work.

20 REPLIES 20

Middelkoop-Meij
Giga Contributor

Thank you so much for sharing! We just upgraded to Fuji and are very happy to find a solution to this question!


I only had a little issue with line 22 var gr = new GlideRecord("sys_attachment");  


SN was complaining that the variable already existed; so on line 10 we added:   var gr ;


and now everything is working perfect!



Again, thanks!!


divyalivingbell
Tera Contributor

I am working on order guide. I have to check whether the user has attached the attachment.I tried with the onsubmit script without creating the 'u_order_guide' variable. I am able to get the warning message to attach document.I will not be able to checkout without attaching the document. But after checking out, if I try to edit my item in shopping cart and click 'update cart', the changes made to the item are not getting captured and I am not rediredted to the Shopping cart page. I get an error message stating "Uncaught TypeError Cannot read property 'value' of null". Please give me a solution for this if you have encountered the same issue.


Hello Divya,



Yes that was the issue was facing when I first built this solution. It would get an error and not work correctly. That is why the 'u_order_guide' variable is important. It does not work correctly without it.


kemmy1
Tera Guru

What if you just need this for a simple service catalog item (requesting an account for example) and not using an order guide?   Customer wants to make the attachment mandatory for a particular service catalog item.


I think I got my answer here:   Re: Check Attachments on current form.



Crossing my fingers it works!



LIsa