Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How to check attachment before on submit client script in catalog item & attachment must be xlsx

surajsironi
Kilo Sage

Hi All,

Kindly help me 
How to check attachment before on submit client script in catalog item &
there must be 1 attachment and that attachment type must be xlsx only.


10 REPLIES 10

Hi @lauri457 ,

Syn working in Native UI, not working for Portal

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

    isAttached: function() {
        var obj = {
            attached: 'false',
            count: 0
        };

        var id = this.getParameter('sysparm_sysid');
        var tablename = this.getParameter('sysparm_tablename');
        var allowedTypes = ['xlsx', 'xls'];

        var gr = new GlideRecord('sys_attachment');
        gr.addQuery('table_name', tablename);
        gr.addQuery('table_sys_id', id);
        gr.query();
        while (gr.next()) {
            var name = gr.file_name.toString().toLowerCase();
            var ext = name.split('.').pop();
            if (allowedTypes.indexOf(ext) > -1) {
                obj.count++;
                obj.attached = 'true';
            }
        }
        return JSON.stringify(obj);
    },
    type: 'CheckAttachment'
});

 

function onSubmit() {

    var itemSysId = g_form.getValue('sysparm_item_guid') || gel('sysparm_item_guid').value;

    var ga = new GlideAjax('CheckAttachment');
    ga.addParam('sysparm_name', 'isAttached');
    ga.addParam('sysparm_sysid', itemSysId);
    ga.addParam('sysparm_tablename', 'sc_cart_item');

    ga.getXMLWait();
    var result = JSON.parse(ga.getAnswer());

    if (result.attached === 'false') {
        alert("Attach exactly one file (xlsx, xls, pdf).");
        return false;
    }
    if (result.count > 1) {
        alert("Only one attachment is allowed.");
        return false;
    }
    return true;
}

You can't get the sysid like this on the portal

var itemSysId = g_form.getValue('sysparm_item_guid') || gel('sysparm_item_guid').value;

But it can be found from the catitem widgets scope

 this.angular.element("#sc_cat_item").scope().data._generatedItemGUID;

 

so then , it should be like below ?
if(window==null)

if(window==null)
var itemSysId =  this.angular.element("#sc_cat_item").scope().data._generatedItemGUID;
else
var itemSysId = g_form.getValue('sysparm_item_guid') || gel('sysparm_item_guid').value;

 

Ankur Bawiskar
Tera Patron

@surajsironi 

Easiest way

-> create attachment variable and make it mandatory at variable config level (handles only 1 attachment check)

-> add this in variable attributes so that it allows only xlsx file

AnkurBawiskar_0-1770386476377.png

 

šŸ’” 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

Ankur Bawiskar
Tera Patron

@surajsironi 

If you don't want to use attachment variable then use this script in onSubmit catalog client script

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var arr = [];
    var hiddenVariable = g_form.getValue('hidden_variable');
    try {
        if (window == null) {
            // portal
            var z = this.document.getElementsByClassName("get-attachment ng-binding ng-scope");
            var k;
            for (k = 0; k < z.length; k++) {
                var value = z[k].innerHTML;
                value = value.substring(0, value.indexOf('('));
                arr.push(value.trim());
            }
            // now check if the file name has extension as xlsx or not by iterating array
        }
    } catch (ex) {
        // native get all the file names
        $j("a.content_editable").each(function(index) {
            var val = $j(this).text();
            arr.push(val);
        });
        // now check if the file name has extension as xlsx or not by iterating array
    }

}

šŸ’” 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