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

 

7 REPLIES 7

SohamTipnis
Tera Guru

Hi @saranyavs,

 

You can just make the attachment mandatory using a checkbox in the catalog item.

You can use the below image for reference:

 

SohamTipnis_0-1770983288733.png

 

If you find my answer useful, please mark it as Helpful and Correct ‌😊


Regards,
Soham Tipnis
ServiceNow Developer ||  Technical Consultant
LinkedIn: www.linkedin.com/in/sohamtipnis10

Ankur Bawiskar
Tera Patron

@saranyavs 

If you users are only using Portal then this checkbox will achieve your requirement.

AnkurBawiskar_0-1770983662852.png

 

If they are using both Native + Portal then this will work in both places

Create onSubmit Catalog Client Script

UI Type - ALL

function onSubmit() {
    //Type appropriate comment here, and begin script below

    try {
        if (window == null) {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length != 1) {
                alert('You must attach file before submitting this request.');
                return false;
            }
        }
    } catch (ex) {
        // native view
        var length = getSCAttachmentCount();
        if (length != 1) {
            alert('You must attach file before submitting this request.');
            return false;
        }
    }
}

💡 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  ||  9x 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