chirag_bagdai
ServiceNow Employee
ServiceNow Employee

Below script may help you to check how many attachments are uploaded in catalog item.

You can create new Catalog Client Script and make sure to set following fields:

  • Isolate script: should be unchecked
  • UI Type - All
  • Script :
function onSubmit() {
    var attachmentCount = 0;
    var expectedAttachmentCount = 2;
    var errorMsg = 'Expected attachments are ' + expectedAttachmentCount;
    try {
        if (typeof parent.angular != 'undefined') { // portal
            var pageScope = parent.angular.element('[template="sp_attachment_single_line"]').scope();
            if (!pageScope) { //no attachments
                attachmentCount = 0;
            } else { // when attachments count is more > 0
                attachmentCount = pageScope.attachments.length;
            }
        } else { // native view
            attachmentCount = getCurrentAttachmentNumber();
        }
        if (attachmentCount < expectedAttachmentCount) {
            g_form.addErrorMessage(errorMsg);
            return false;
        }
    } catch (e) {
        console.error(e);
        g_form.addErrorMessage("Something went wrong. Please contact system administrator");
        return false;
    }
}
Comments
nivea
Tera Contributor

Hi @chirag.bagdai ,

I'm using your script to validate attachment count based on a variable input. It is working when attachment is not attached.

After attaching the attachment, attachmentCount is showing as 1 but I'm getting Javascript error in browser console while submitting the request.

PFB the script which I'm using to Validate attachments in Portal.

function onSubmit() {

    if (g_form.getValue('more_than_one_device_the_list_has_been_uploaded') == 'true') {

    var attachmentCount = 0;
    var expectedAttachmentCount = 1;
    var errorMsg = 'You must add an attachment before submitting this request as you have selected more than one device option';
    try {
        if (typeof parent.angular != 'undefined') { // portal
            
            var pageScope = parent.angular.element('[template="sp_attachment_single_line"]').scope();
            if (!pageScope) { //no attachments
                attachmentCount = 0;
                
            } else { // when attachments count is more > 0
                             attachmentCount = pageScope.attachments.length;
            }
        } 
        if (attachmentCount < expectedAttachmentCount) {
            alert('inside attachment portal script - if loop validation');
            alert(errorMsg);
            return false;
        }
    } 
        catch (e) {
      console.error(e);
        alert("Something went wrong. Please contact system administrator"); 
        return false;
    }
}
}

 

Can you please help me to understand why I'm getting Javascript error in your browser console error ?

 

Thanks,

Nivea.

Jim Coyne
Kilo Patron

Thanks for the tip, but I'm curious: where is "getCurrentAttachmentNumber" defined?  I searched for it but could not find it.  Definitely not a documented call, which is risky.

chirag_bagdai
ServiceNow Employee
ServiceNow Employee

@Jim Coyne  - You are right getCurrentAttachmentNumber is not a documented API hence there is a risk of using non published API. 

Jim Coyne
Kilo Patron

That's too bad.  Maybe you have some power to get it documented and supported?  🙂

chirag_bagdai
ServiceNow Employee
ServiceNow Employee

Unfortunately, I haven't reached that level yet 🙂

 

You can submit product enhancement request - https://www.servicenow.com/community/itom-articles/have-you-visited-our-ideas-portal-use-this-to-pro... 

 

I completely understand that relying on DOM is risky but I am really not sure if there are any published API available.

rogie_mar
Tera Contributor

Tried this but 'pageScope' is undefined

Peter Williams
Kilo Sage

This was very helpful but question when you have a variable for attachment it doesnt seem that it detects if it has an attachment

i have this variable in an MRV.

 

how can you modified this for this case

 

PeterWilliams_0-1727290573963.png

 

Version history
Last update:
‎03-07-2022 08:59 AM
Updated by: