How to Check attachment attached or not on submit client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-08-2024 11:14 PM
Hii All
i need to check attachment has attach or not by using on submit client script
like i make one catalog item in that i want to check that this catalog item has attachment or not
i try many thing but no luck
your help highly appreciated
thanks and regards
Dheerendra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 12:39 AM
Hi @dheeru_1994
You may use GlideAjax in that case, you can check this below code that I prepared for you
Client Script :
var ga = new GlideAjax('AttachmentCheckonIncident');
ga.addParam('sysparm_name', 'att');
ga.addParam('incId', g_form.getUniqueValue());
ga.getXML(checkAttHandler);
function checkAttHandler(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer){
alert(answer);
return true;
}else{
alert(answer);
return false;
}
}
Script Include : Which is client callable
var AttachmentCheckonIncident = Class.create();
AttachmentCheckonIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {
att: function() {
var incId = this.getParameter('incId');
var attachGR = new GlideRecord('sys_attachment');
attachGR.addQuery('table_sys_id', incId);
attachGR.query();
if (attachGR.next()) {
gs.log("form contains attachment");
return true;
}else{
gs.log("No attachment ");
return false;
}
},
type: 'AttachmentCheckonIncident'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 01:05 AM - edited 04-09-2024 01:08 AM
Hi @dheeru_1994
There is small change in Client Script please refer below code
var ga = new GlideAjax('AttachmentCheckonIncident');
ga.addParam('sysparm_name', 'att');
ga.addParam('incId', g_form.getUniqueValue());
ga.getXMLWait();
var answer = ga.getAnswer(); //getAnswer() retrieves the result for you
alert(answer)
if (answer == 'true') {
alert("Database already exist");
}else{
return false;
}
Script Include : Client callable
var AttachmentCheckonIncident = Class.create();
AttachmentCheckonIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {
att: function() {
var incId = this.getParameter('incId');
var attachGR = new GlideRecord('sys_attachment');
attachGR.addQuery('table_sys_id', incId);
attachGR.query();
if (attachGR.next()) {
gs.log("form contains attachment");
return true;
}else{
gs.log("No attachment ");
return false;
}
},
type: 'AttachmentCheckonIncident'
});
Please mark correct and helpful if this works for you.
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 02:01 AM
Will this work on service portal as well or not
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2024 02:36 AM
@dheeru_1994 , Yes it works on service portal as well, please check and update me, if anything is need feel free to reach me out here.
If my post helps you please mark as correct.
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 12:18 AM
This will never work on the Portal / Employee center.
g_form.getUniqueValue()
This will get you the unique sys_id of the catalog item.
However, when you add an attachment to a form on the portal, in the sys_attachment table it will appear as table_name sc_cart_item and the id of the cart_item at that moment.
So with your sys_id, you will never find that attachment, therefore it will always return false.
Can't believe this is still not an OOTB feature in ServiceNow.
Ok we now have the ability to put the Mandatory attachment box, but most of us need to to this conditionally, not ALWAYS.