how to check if attachments are added before submitting the RITM?

saranyavs
Tera Expert

How to check if attachments are added to the catalog item before submitting the Request?

 

Regards,

Saranya

 

11 REPLIES 11

@saranyavs 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

vaishali231
Tera Guru

Hey @saranyavs 

 

Option 1: Client-Side Validation Using g_form.getAttachments()

This is the simplest and recommended approach for most cases.

 

Type: onSubmit
UI Type: All

Script

function onSubmit() {

var attachments = g_form.getAttachments();

if (!attachments || attachments.length == 0) {
alert("Please attach the required document before submitting the request.");
return false; // Prevent submission
}

return true; // Allow submission
}

Option 2: GlideAjax with Script Include 

If attachment validation is inconsistent in Service Portal or custom widgets, use this method.

We will check the sys_attachment table before submission.

Step 1: Create Script Include

Name: CheckAttachmentAjax
Client Callable: True

var CheckAttachmentAjax = Class.create();
CheckAttachmentAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkAttachment: function() {

var sysId = this.getParameter('sysparm_sys_id');

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', sysId);
gr.query();

if (gr.hasNext()) {
return 'true';
}

return 'false';
}

});

Step 2: Create Catalog Client Script

Type: onSubmit
UI Type: All

function onSubmit() {

    var ga = new GlideAjax('CheckAttachmentAjax');
    ga.addParam('sysparm_name', 'checkAttachment');
    ga.addParam('sysparm_sys_id', g_form.getUniqueValue());

    var answer = ga.getXMLWait();

    if (answer != 'true') {
        alert('Please attach a file before submitting the request.');
        return false; // Prevent submission
    }

    return true; // Allow submission
}

 

*************************************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.

Regards
Vaishali Singh