If attachment not added not allow to submitted catalog item

RammohanP
Tera Contributor

I have attachment_upload yes/no variable If I select yes, I must include an attachment; if an attachment is not included, I cannot submit it. if an attachment is added and attachment_upload is yes then i want to submit my cat item other than any i want through alert. Please check my script and suggest any changes that are required.

On submit catalog client script:

function onSubmit() {
var att = g_form.getValue('attachment_upload');
var catId = g_form.getUniqueValue();

var demo = new GlideAjax('CheckAttachmentCatalogItem');
demo.addParam('sysparm_name', 'checkAttachment');
demo.addParam('sysparm_catalog_id', catId);
demo.getXMLAnswer(catResponse);

function catResponse(response) {
var attachment = response;

if (attachment === 'yes' && att === 'Yes') {
return true;
} else {
alert("You must select 'Yes' and must have an attachment to submit Templates for Data Load Types");
return false;
}
}
}

Script include client callable

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

checkAttachment: function(){
var catId = this.getParameter('sysparm_catalog_id');
var gr = new GlideRecord("sys_attachment");
gr.addQuery('table_nameINsc_req_item,sc_cart_item');
gr.addQuery('table_sys_id', catId);
gr.query();

if (gr.next()) {
return 'yes';
} else {
return 'no';
}
},
type: 'CheckAttachmentCatalogItem'
});
It is working on service portal side but not working native UI side, Ui type is all still not working

 

4 REPLIES 4

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @RammohanP 

 

Here are solutions

 

https://www.servicenow.com/community/developer-forum/attachment-mandatory-for-a-catalog-item/m-p/179...

 

function onSubmit() {
	//Type appropriate comment here, and begin script below

	var countRequired = 1;
	if(g_form.getValue('request_required') == 'Manage document'){
		if(window == null){
			// portal
			if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
				g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
				return false;
			}
		}
		else{
			// native view
			var length = $j("li.attachment_list_items").find("span").length;
			if(length != countRequired){
				g_form.addErrorMessage(getMessage('attachment_is_mandatory'));
				return false;
			}
		}
	}
}

 

 

https://www.servicenow.com/community/developer-forum/how-to-make-attachment-mandatory-for-catalog-it...

 

Credits : @Ankur Bawiskar

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Thank you for your reply
Above script i tried but it is not working i am adding an attachment not allowing me to submit the cat item, got the alert message, i turned off the isolated script also
Could you please review my script if possible, it is working on portal side but not working on UI Side

You can use Attachment variable to obtain attachment from the user.

 

Use UI policy to control visibility of attachment variable and make It mandatory based on your requirement. (Attachment upload = Yes)

Thanks for reply
I don't have a separate attachment variable on the cat item, we are using OOTB attachment icon, is it possible to make it mandatory by using UI policy