OnSubmit Attachments Validation on a Catalog item

Gowtham29
Tera Expert

Hi, I need to make the attachment mandatory through on submit client script, we have written script to make attachments in a specific formats like .jpg, doc, docx and so on, I have written below script but it is not working, Could you please me here ?

I need this validation on both native and portal view 

 

 

Script Include
-----------------------------------------------------------------------------------------------------------------------------------------------
 attachment: function() {
        var dataCheck = this.getParameter('sysparm_reqItem');
        var val = 1;
        var record = 'no';

        var gr = new GlideRecord("sys_attachment");
        gr.addQuery('table_name', "sc_cart_item");
        gr.addQuery('table_sys_id', dataCheck);
        gr.query();

        while (gr.next()) {
            record = 'yes';
            if (gr.file_name.toString().indexOf('.msg') > -1 || gr.file_name.toString().indexOf('.doc') > -1 || gr.file_name.toString().indexOf('.docx') > -1 || gr.file_name.toString().indexOf('.txt') > -1 || gr.file_name.toString().indexOf('.pdf') > -1 || gr.file_name.toString().indexOf('.eml') > -1  || gr.file_name.toString().indexOf('.jpg' || gr.file_name.toString().indexOf('.png') > -1) ){

val = val;
            } else 
                val = val * 0;
        }
        var str = val + record;
        return str;


    },
Client Script 
----------------------------------------------------------------------------------------------------------------------------------------
function onSubmit() {
    // if (g_scratchpad.isFormValid)
    //     return true;
   var catalogItemID = g_form.getValue('sysparm_item_guid');//native view
    //var catalogItemID = g_form.getUniqueValue(); // portal view
   
    alert(catalogItemID);
    var ajax = new GlideAjax('hasAttachmentMaconomy');
    ajax.addParam('sysparm_name', 'attachment');
    ajax.addParam('sysparm_reqItem', catalogItemID);
    ajax.getXMLAnswer(setAnswer);
    return false;
}

function setAnswer(answer) {
    alert(answer);
    var val = answer.toString();
    if (val.indexOf('0') > -1 || val.indexOf('no') > -1) {
        alert('inside if');
        alert('Please attach copy of cancelled copy');
        return false;
    } else {
        alert('insdie else');
        return true;
    }  

}

 

1 ACCEPTED SOLUTION

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Gowtham29 , 

 

 The main issue is observing when we have to validate smth on Submit action.

 

- We can't using methods the "getXmlWait()" anymore due to is not currently supported and does't work in Workspace/Portal. Previously it was working solution and there weren't any issues with it, but this method was deprecated.

 

- We can't using the getXML(function) method as we need to return "false" to abort submit action. Script runs function in the end of processing. All out of function will be processed first and it is not possible to make "return false". 

So you can do some manipulations in Portal view with this onsubmit client script. 

function onSubmit() {
    // Type appropriate comment here, and begin script below
    var arr = [];
    var extensions = ['.jpg', '.jpeg', '.png', '.gif', '.doc', '.docx', '.pdf', '.xlsx','.txt'];
    try {
        var names = this.document.getElementsByClassName('get-attachment ng-binding ng-scope');

        // Check if any attachments exist
        if (names.length === 0) {
            alert('Please attach at least one file.');
            return false;
        }

        for (var i = 0; i < names.length; i++) {
            var val = names[i].innerHTML;
            arr.push(val.toString());
        }

        for (var j = 0; j < arr.length; j++) {
            var isValid = false;
            for (var k = 0; k < extensions.length; k++) {
                if (arr[j].indexOf(extensions[k]) !== -1) {
                    isValid = true;
                    break;
                }
            }
            if (!isValid) {
                alert('Please give files with one of the following extensions: ' + extensions.join(', '));
                return false;
            }
        }
    } catch (error) {
        alert('Please submit the request from Portal');
        return false;
    }
}

 

It works like a charm in my PDI. 

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

View solution in original post

2 REPLIES 2

shyamkumar VK
Kilo Patron

@Gowtham29 ,

can you try below client script

if (this.document.getElementsByClassName('get-attachment').length == 0) {
		g_form.addErrorMessage(getMessage('attachment_mandatory'));

		return false;
	}	
   
}

 

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Gowtham29 , 

 

 The main issue is observing when we have to validate smth on Submit action.

 

- We can't using methods the "getXmlWait()" anymore due to is not currently supported and does't work in Workspace/Portal. Previously it was working solution and there weren't any issues with it, but this method was deprecated.

 

- We can't using the getXML(function) method as we need to return "false" to abort submit action. Script runs function in the end of processing. All out of function will be processed first and it is not possible to make "return false". 

So you can do some manipulations in Portal view with this onsubmit client script. 

function onSubmit() {
    // Type appropriate comment here, and begin script below
    var arr = [];
    var extensions = ['.jpg', '.jpeg', '.png', '.gif', '.doc', '.docx', '.pdf', '.xlsx','.txt'];
    try {
        var names = this.document.getElementsByClassName('get-attachment ng-binding ng-scope');

        // Check if any attachments exist
        if (names.length === 0) {
            alert('Please attach at least one file.');
            return false;
        }

        for (var i = 0; i < names.length; i++) {
            var val = names[i].innerHTML;
            arr.push(val.toString());
        }

        for (var j = 0; j < arr.length; j++) {
            var isValid = false;
            for (var k = 0; k < extensions.length; k++) {
                if (arr[j].indexOf(extensions[k]) !== -1) {
                    isValid = true;
                    break;
                }
            }
            if (!isValid) {
                alert('Please give files with one of the following extensions: ' + extensions.join(', '));
                return false;
            }
        }
    } catch (error) {
        alert('Please submit the request from Portal');
        return false;
    }
}

 

It works like a charm in my PDI. 

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)