Attachment Mandtory for a catalog item

nameisnani
Mega Sage

Hi Team ,

 

We have a catalog item called “AWS Support”. Inside it, there is a variable called “Type of Request”.
One of the options for this variable is “Create AWS AD Group”.

If the user selects this option and tries to submit the form without adding an attachment, the form should not be submitted. Instead, the system should show an error message asking the user to attach the required file.

 

nameisnani_0-1755498773072.png

function onSubmit() {

    var requestType = g_form.getValue('type_of_request'); 

    if (requestType == 'Create AWS AD Group') {

        var attachmentCount = g_form.getValue('attachments'); // this only works with attachment mandatory check in later versions

        // For safer check - use g_form.hasAttachments() if your instance supports

        if (!g_form.hasAttachments()) {

            alert("Attachment is mandatory when selecting 'Create AWS AD Group'. Please attach a file before submitting.");

            return false; // Prevent submission

        }

    }

    return true;

}

 

 

 

 

the script which i have written it is not working - what was the error . 

 

@Ankur Bawiskar  

1 ACCEPTED SOLUTION

Hi @nameisnani ,

 

"If the user selects this option and tries to submit the form without adding an attachment, the form should not be submitted. Instead, the system should show an error message asking the user to attach the required file."

 

It looks like - YOu want this field to be mandatory and when user is leaving blank, it should display error.

 

Regards,

Nikhil Bajaj

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.
Regards,
Nikhil Bajaj

View solution in original post

15 REPLIES 15

@nameisnani I agree with @Nikhil Bajaj9 ...

 

what's the difference between [1] user being disallowed to submit if no attachment is attached and [2] making the attachment mandatory...

 

 [2] doesn't let you submit it if no attachment is added, [1] requires too much efforts for such a single thing... 

 

Please consider my comment above:
https://www.servicenow.com/community/developer-forum/attachment-mandtory-for-a-catalog-item/m-p/3354... 

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


asaha
Tera Contributor

There is no such g_form method "hasAttchments()".

You can do the below. Do a GlideAjax call and check if you have any attachment in the 'sys_attachments" table for the form.

You can refer the below script. Just add your condition:

Client Script:

function onSubmit() {
    var requestType = g_form.getValue('requestType');
    var sys_id = g_form.getUniqueValue()
 
if (requestType === 'Create AWS AD Group') {
    var ga = new GlideAjax('CheckAttachment');
   
    ga.addParam('sysparm_name', 'hasAttachment');
    ga.addParam('sysparm_table', g_form.getTableName()) //will take your table dynamically
    ga.addParam('sysparm_sys_id', sys_id);
   
    ga.getXMLAnswer(function(response) {
        if (response === 'false') {
            alert("Please attach something before submitting this request.");
            return false;
        }
}
    });

    return true;
}

Script Include:

Client Callable:- Checked(true)
Script:
var CheckAttachment = Class.create();
CheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    hasAttachment: function() {
        var tableName = this.getParameter('sysparm_table');
        var sysId = this.getParameter('sysparm_sys_id');
       
        var gr = new GlideRecord('sys_attachment');
        gr.addQuery('table_name', tableName);
        gr.addQuery('table_sys_id', sysId);
        gr.query();

        return gr.hasNext().toString(); 
    }
});

This will work as per your requirement. Please mark the answer as helpful. Thanks


@asaha  Let me try this 

asaha
Tera Contributor

@nameisnani  Sure thing. Let me know if this works. It should work. You can ignore "gr.addQuery('table_name', tableName);" I think

 

Hi @nameisnani 

 

why do you want to call the server side if it can be done easily by mandatory attachment field...?

 

this is an overkill for something that could be done very easily 😞 

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */