- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 12:55 AM
Hi All,
I am having a requirement to make the attachment clip mandatory while submitting the request once a particular check box is selected.
Firstly, i wrote a onSubmit catalog client script with the below code but this is working on desktop view but it isn't working on the Employee Center Service Portal.
if (g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {
try {
var cat_id_return = gel('sysparm_item_guid').value;
//alert('Happy '+cat_id_return);
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id_return);
gr.query();
if (!gr.next()) {
alert("Please attach the Letter File to proceed further.");
return false;
}
I know 'gel' isn't supported by Service Portal so i did search in the community and found one solution to create a UI Script and call it in the catalog client script, but this also isn't working.
UI Script:
API Name: GlobalCatalogItemFunctions
function getSCAttachmentCount() {
var length;
try {
length = angular.element("Copies/Reprints Request").scope().attachments.length;
} catch(e) {
length = -1;
}
return length;
}
Catalog Client Script
if (g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {
try {
var cat_id_return = gel('sysparm_item_guid').value;
//alert('Happy '+cat_id_return);
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id_return);
gr.query();
if (!gr.next()) {
alert("Please attach the Letter File to proceed further.");
return false;
}
} catch (e) { //For Service Portal
var count = getSCAttachmentCount();
if (count <= 0) {
alert('Please attach the Letter File to proceed further.');
return false;
}
}
}
Can someone please let me know how to achieve this solution.
Thanks,
Rooma
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 01:30 AM
function onSubmit() {
if (g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {
try {
var cat_id_return = g_form.getParameter("sysparm_item_guid");
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id_return);
gr.query();
if (!gr.next()) {
alert("Attachment is mandatory");
return false;
}
} catch (e) {
if (this.document.getElementsByClassName('get-attachment').length == 0) {
g_form.addErrorMessage(getMessage('Attachment is mandatory'));
return false;
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 01:16 AM
Hi @roomawakar,
Have you seen the article that @AbhishekGardade has provideed covering a number of use cases and scenarios - Kudos. I'd steer you towards this as a starting point.
To help others (and for me to gain recognition for my efforts), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 01:30 AM
function onSubmit() {
if (g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {
try {
var cat_id_return = g_form.getParameter("sysparm_item_guid");
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id_return);
gr.query();
if (!gr.next()) {
alert("Attachment is mandatory");
return false;
}
} catch (e) {
if (this.document.getElementsByClassName('get-attachment').length == 0) {
g_form.addErrorMessage(getMessage('Attachment is mandatory'));
return false;
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 02:13 AM
this will work in both the UI Type
function onSubmit() {
//Type appropriate comment here, and begin script below
try {
if (window == null && g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {
// portal
if (this.document.getElementsByClassName('get-attachment').length != 1) {
alert('You must attach the completed form before submitting this request.');
return false;
}
}
} catch (ex) {
// native view
if (g_form.getValue('sbx_crr_requestType') == 'Manual Letter Generation for Address correction Requests') {
var length = getCurrentAttachmentNumber();
if (length != 1) {
alert('You must attach the completed form before submitting this request.');
return false;
}
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 03:44 AM
Thank you for marking my response as helpful.
As per new community feature you can mark multiple responses as correct.
I provided the correct solution as well to your question.
If my response helped please mark it correct as well so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader