If attachment not added not allow to submitted catalog item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 08:11 AM
I have attachment_upload yes/no variable If I select yes, I must include an attachment; if an attachment is not included, I cannot submit it. if an attachment is added and attachment_upload is yes then i want to submit my cat item other than any i want through alert. Please check my script and suggest any changes that are required.
On submit catalog client script:
function onSubmit() {
var att = g_form.getValue('attachment_upload');
var catId = g_form.getUniqueValue();
var demo = new GlideAjax('CheckAttachmentCatalogItem');
demo.addParam('sysparm_name', 'checkAttachment');
demo.addParam('sysparm_catalog_id', catId);
demo.getXMLAnswer(catResponse);
function catResponse(response) {
var attachment = response;
if (attachment === 'yes' && att === 'Yes') {
return true;
} else {
alert("You must select 'Yes' and must have an attachment to submit Templates for Data Load Types");
return false;
}
}
}
Script include client callable
var CheckAttachmentCatalogItem = Class.create();
CheckAttachmentCatalogItem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkAttachment: function(){
var catId = this.getParameter('sysparm_catalog_id');
var gr = new GlideRecord("sys_attachment");
gr.addQuery('table_nameINsc_req_item,sc_cart_item');
gr.addQuery('table_sys_id', catId);
gr.query();
if (gr.next()) {
return 'yes';
} else {
return 'no';
}
},
type: 'CheckAttachmentCatalogItem'
});
It is working on service portal side but not working native UI side, Ui type is all still not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 08:15 AM
Hi @RammohanP
Here are solutions
function onSubmit() {
//Type appropriate comment here, and begin script below
var countRequired = 1;
if(g_form.getValue('request_required') == 'Manage document'){
if(window == null){
// portal
if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
return false;
}
}
else{
// native view
var length = $j("li.attachment_list_items").find("span").length;
if(length != countRequired){
g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
return false;
}
}
}
}
Credits : @Ankur Bawiskar
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 08:39 AM
Thank you for your reply
Above script i tried but it is not working i am adding an attachment not allowing me to submit the cat item, got the alert message, i turned off the isolated script also
Could you please review my script if possible, it is working on portal side but not working on UI Side

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 11:15 AM
You can use Attachment variable to obtain attachment from the user.
Use UI policy to control visibility of attachment variable and make It mandatory based on your requirement. (Attachment upload = Yes)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 06:10 PM
Thanks for reply
I don't have a separate attachment variable on the cat item, we are using OOTB attachment icon, is it possible to make it mandatory by using UI policy