- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2019 06:43 AM
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;
}
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2020 02:25 AM
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
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2019 06:51 AM
You can use this method for service portal.
1. create a ui script of mobile/service portal type add below code:
function getSCAttachmentCount() {
var length;
try {
length = angular.element("#sc_cat_item").scope().attachments.length;
} catch(e) {
length = -1;
}
return length;
}
function getSCAttachment(){
var attach;
try {
attach = angular.element("#sc_cat_item").scope().attachments;
attach = JSON.stringify(attach);
} catch(e) {
attach = 'Empty';
}
return attach;
}
2. Create a widget dependency and it the SC catalog item widget used for your portal.
3. create a on submit client script on your item with below code:
function onSubmit() {
var myFieldValue = g_form.getValue('polygon_details_attached');
if (myFieldValue == "Yes") {
try { //Works in non-portal ui
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
//alert('This item requires an attachement.');
alert("You must attach a file to submit.");
return false;
}
} catch(e) { //For Service Portal
var count = getSCAttachmentCount();
if(count <= 0) {
//alert('This item requires an attachement.');
alert("You must attach a file to submit.");
return false;
}
}
}
}
Note: you can modify on submit script accordingly.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2019 06:53 AM
DOM will not work on portal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2019 06:55 AM
This is not only DOM. In service portal it will call the UI script.
I am using this method in madrid service portal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2020 01:51 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2019 06:55 AM
As of the Madrid release you should be able to check if an attachment (not a specific attachment) is added within the Service Portal out of the box:
Now if you need to check attachments based upon variable values you can script that in, I've used the script from ServiceNow Elite in different instances without an issue and used an if statement to check my variable value, it also works for the platform as well:
https://www.servicenowelite.com/blog/2017/5/12/service-portal-require-attachments
Here's one I've used in the past as a catalog client script, just make sure you've added the UI Script and JS Include:
var type = g_form.getValue('uvar_typeOfRequest');
try { //Works in non-portal ui
if (type =='infochange') {
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
g_form.addErrorMessage("Please attach the Informational Change Template to this request");
return false;
}
}
} catch(e) { //For Service Portal
var count = getSCAttachmentCount();
if(count <= 0 && type == 'infochange') {
g_form.addErrorMessage("Please attach the Informational Change Template to this request");
return false;
}
}
If my response was helpful, please mark as Helpful and consider setting the reply as the Correct Answer if it best answered your question, thank you!