Check the attachment when resolving the incident
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2024 07:10 AM
Hello Community Experts,
Could you please help me the coding snippet to check the attachment is added or not when resolving incident, If there is no attachment don't allow anything. We are looking for the on submit client script through glide Ajax.
Thanks,
Nagesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2024 09:28 AM - edited 08-11-2024 09:32 AM
Hi @Nagesh5 Try below code
1. Script include
var AttachmentChecker = Class.create();
AttachmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasAttachment: function() {
var incidentId = this.getParameter('sys_id');
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', incidentId);
attachment.addQuery('table_name', 'incident');
attachment.query();
return attachment.hasNext();
}
});
2. Client Script
function onSubmit() {
var ga = new GlideAjax('AttachmentChecker');
ga.addParam('sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(function(response) {
var hasAttachment = response.responseXML.getDocumentElement().getAttribute("answer") === "true";
if (!hasAttachment) {
// Display an error message
g_form.addErrorMessage( 'You must add at least one attachment before resolving this incident.');
return false;
}
});
return true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2024 07:30 PM
Thanks for your time to provide above script and unfortunately it's not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2024 09:43 PM
Hi @Nagesh5 Try below code
Script include
var AttachmentUtils = Class.create();
AttachmentUtils.prototype = {
initialize: function() {
},
hasAttachments: function(recordId) {
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', recordId);
gr.query();
return gr.getRowCount() > 0;
},
type: 'AttachmentUtils'
};
onSubmit client Script
function onSubmit() {
var attachmentUtils = new AttachmentUtils();
var recordId = g_form.getUniqueValue();
if (g_form.getValue('state') == 'Resolved') {
if (!attachmentUtils.hasAttachments(recordId)) {
g_form.addErrorMessage('An attachment must be added before resolving the incident.');
return false;
}
}
return true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2024 10:21 AM
Hello @Nagesh5 ,
Script Include:
var AttachmentChecker = Class.create();
AttachmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasAttachments: function() {
var incidentId = this.getParameter('sys_id');
var attachmentGr = new GlideRecord('sys_attachment');
attachmentGr.addQuery('table_sys_id', incidentId);
attachmentGr.addQuery('table_name', 'incident');
attachmentGr.query();
return attachmentGr.hasNext() ? 'true' : 'false';
}
});
onSubmit Client Scripts:
function onSubmit() { // Get the sys_id of the current incident var incidentId = g_form.getUniqueValue(); // Check for attachments directly var attachmentCount = 0; var ga = new GlideAjax('AttachmentChecker'); ga.addParam('sys_id', incidentId); ga.getXMLAnswer(function(response) { if (response === 'false') { // Display an error message and prevent form submission g_form.addErrorMessage('You must attach a file before resolving this incident.'); g_form.setInvalid('state'); // Prevent form submission by setting an invalid state } }); // Prevent form submission until the AJAX call completes return false; }
If you found my response helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you!