Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client script to make attachment mandatory

Khalnayak
Kilo Sage

Hi,

I am trying to make attachment widget mandatory only if the request type is add access.

So I have created a UI policy, and chose to set mandatory and set visible, the visible bit works but it does not set it mandatory however.

find_real_file.png

 

How can i create an onsubmit or onchange client script to make this field mandatory or prevent submission if no attachments.

1 ACCEPTED SOLUTION

I used the below script provided by @Mark Roethof in another thread and it worked fine!

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

		return false;
	}	
   
}

View solution in original post

4 REPLIES 4

Abdul_Rahiman
Tera Guru

@Usmaan ,

 

Catalog Client Script Code:

function onSubmit() { 
var catalogItemID = g_form.getValue('sysparm_item_guid');
 
 
var demo = new GlideAjax('CheckAttachmentCatalogItem');
demo.addParam('sysparm_name', 'checkAttachment');
demo.addParam('sysparm_catalog_id', catalogItemID);
demo.getXMLWait();
 
var attachment=demo.getAnswer();
if(attachment == 'yes'){
        return true;
} else if(attachment == 'no'){
        alert("Please attach Image of your Employee Card");
        return false;
}
   
}
 

Script Include Code:

var CheckAttachmentCatalogItem = Class.create();
CheckAttachmentCatalogItem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
checkAttachment: function(){
var gr = new GlideRecord("sys_attachment");
gr.addQuery('table_name', "sc_cart_item");
gr.addQuery('table_sys_id', this.getParameter('sysparm_catalog_id'));
gr.query();
if (gr.next()) {
         return 'yes';
}
else{
        return 'no';
}
},
    type: 'CheckAttachmentCatalogItem'
});
 
Please note: Please change class name "CheckAttachmentCatalogItem" accordingly to your script include name. 
 
Regards,
Abdul

Hi @Abdul i tried your script include and client script but Im getting an error when submitting. the request submits without attachment.

find_real_file.png

I used the below script provided by @Mark Roethof in another thread and it worked fine!

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

		return false;
	}	
   
}

Hello,

In this, Where are you selecting your choice value?

 

Like,

var x = g_form.getValue('your_varaible');

 

if(x == 'add access'){

 

// then script here

}

 

I have similar situation & this solution isn't helping.

 

 

However I created a new variable, type attachment which is mandatory & visible only when certain choice is selected.