Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

without an attachment catalog item should not be submit .

nameisnani
Mega Sage

Hi Team , 

 

can any one please correct my script .

 

For a particular catalog item , attachment should become mandotory , with out attchment request should not be submit , it is should abort subimission with error an message , please provide cilent script and script include which should as per the requirment

 

I have written onsubmit cilent script and script include

 

function onSubmit() {
    // Get the current catalog item's sys_id (unique identifier)
    var itemId = g_form.getUniqueValue();

    // Initialize GlideAjax and pass required parameters
    var ga = new GlideAjax("AttachmentChecker");
    ga.addParam("sysparm_name", "checkAttachments"); // Specify the function name in the script include
    ga.addParam("sys_id", itemId);

    // Use synchronous call to block submission until response is received
    ga.getXMLWait();
    var hasAttachments = ga.getAnswer();

    // Validate the response from server-side
    if (hasAttachments !== "true") {
        g_form.addErrorMessage("Please attach a file before submitting.");
        return false; // Prevent submission
    }
    return true; // Allow submission
}



script include

 

var AttachmentChecker = Class.create();
AttachmentChecker.prototype = {
    initialize: function() {},

    // Function to check if attachments exist for the given sys_id
    checkAttachments: function(sys_id) {
        if (!sys_id) return "false"; // Return false if sys_id is not provided

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

        // Return true if at least one attachment exists, otherwise false
        return gr.hasNext() ? "true" : "false";
    },

    type: 'AttachmentChecker'
};

type: 'AttachmentChecker'
};


while testing , without attachment request is submitting .

what whas the issue in my script please correct

 

nameisnani_0-1737120729138.png

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@nameisnani 

check this and create catalog onSubmit script and it will work in both native + portal

function onSubmit() {
    //Type appropriate comment here, and begin script below
    try {
        if (window == null) {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length == 0) {
                g_form.addErrorMessage("Please attach a file before submitting.");
                return false;
            }
        }
    } catch (ex) {
        // native view
        var length = getSCAttachmentCount();
        if (length == 0) {
            g_form.addErrorMessage("Please attach a file before submitting.");
            return false;
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@nameisnani 

check this and create catalog onSubmit script and it will work in both native + portal

function onSubmit() {
    //Type appropriate comment here, and begin script below
    try {
        if (window == null) {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length == 0) {
                g_form.addErrorMessage("Please attach a file before submitting.");
                return false;
            }
        }
    } catch (ex) {
        // native view
        var length = getSCAttachmentCount();
        if (length == 0) {
            g_form.addErrorMessage("Please attach a file before submitting.");
            return false;
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@nameisnani 

your script uses getXMLWait() which is synchronous call and it won't work in portal

Use script I shared below and it will work in both native + portal

If my response helped please mark it correct and close the thread so that it benefits future readers.

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