How to Check attachment attached or not on submit client script

dheeru_1994
Tera Contributor

Hii All 
 i need to check attachment has attach or not by using on submit client script  
like i  make one catalog item in that i want to check that this catalog item has attachment or not  
i try many thing but no luck
your help highly appreciated 
thanks and regards 
Dheerendra 

10 REPLIES 10

Community Alums
Not applicable

Hi @dheeru_1994 

You may use GlideAjax in that case, you can check this below code that I prepared for you 

Client Script : 

var ga = new GlideAjax('AttachmentCheckonIncident');
    ga.addParam('sysparm_name', 'att');
    ga.addParam('incId', g_form.getUniqueValue());
    ga.getXML(checkAttHandler);

    function checkAttHandler(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
		if(answer){
			alert(answer);
			return true;
		}else{
			alert(answer);
			return false;
		}
    }

 

Script Include  : Which is client callable 

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

    att: function() {
		var incId = this.getParameter('incId');
        var attachGR = new GlideRecord('sys_attachment');
        attachGR.addQuery('table_sys_id', incId);
        attachGR.query();
        if (attachGR.next()) {
            gs.log("form contains attachment");
			return true;
        }else{
			gs.log("No attachment ");
			return false;
        }

    },

    type: 'AttachmentCheckonIncident'
});

Community Alums
Not applicable

Hi @dheeru_1994 

There is small change in Client Script please refer below code 

 

 

var ga = new GlideAjax('AttachmentCheckonIncident');
    ga.addParam('sysparm_name', 'att');
    ga.addParam('incId', g_form.getUniqueValue());
    ga.getXMLWait();

    var answer = ga.getAnswer(); //getAnswer() retrieves the result for you
	alert(answer)
    if (answer == 'true') {
        alert("Database already exist");
    }else{
		return false;
	}

 

 

Script Include  : Client callable 

 

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

    att: function() {
		var incId = this.getParameter('incId');
        var attachGR = new GlideRecord('sys_attachment');
        attachGR.addQuery('table_sys_id', incId);
        attachGR.query();
        if (attachGR.next()) {
            gs.log("form contains attachment");
			return true;
        }else{
			gs.log("No attachment ");
			return false;
        }

    },

    type: 'AttachmentCheckonIncident'
});

Please mark correct and helpful if this works for you.

 

Thanks and Regards 

Sarthak

Will this work on service portal as well or not 

Community Alums
Not applicable

@dheeru_1994 , Yes it works on service portal as well, please check and update me, if anything is need feel free to reach me out here.

If my post helps you please mark as correct.

 

Thanks and Regards 

Sarthak

This will never work on the Portal / Employee center.

g_form.getUniqueValue()


This will get you the unique sys_id of the catalog item.
However, when you add an attachment to a form on the portal, in the sys_attachment table it will appear as table_name sc_cart_item and the id of the cart_item at that moment.

So with your sys_id, you will never find that attachment, therefore it will always return false.

Can't believe this is still not an OOTB feature in ServiceNow.
Ok we now have the ability to put the Mandatory attachment box, but most of us need to to this conditionally, not ALWAYS.