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.

Mandatory Attachment on Submit based on Catalog Item Variable

gnunez
Kilo Guru

Hello all,

I have a requirement to make an attachment mandatory on submit based on a catalog item variable.

The question is: Are you requesting to relocate more than one phone? (relocate_phone)

If yes: attachment is required

If no: attachment is not required

In the past when making an attachment mandatory in a catalog task I have used business rules or catalog client scripts but in this case I cannot figure it out.

Can anyone assist?

Thanks,

Grace

14 REPLIES 14

German Monsalve
Giga Contributor

Hi The following codes worked for me: 

For Desktop:

function onSubmit() {
	var condition= g_form.getValue('condition');
	try{	
		if(condition=='upload_file'){
			//Condition to make attachement mandatory is true
			var cat_id = g_form.getParameter("sysparm_item_guid");//This will return the sc_cart_item sys_id that is reserved. 
			var gr = new GlideRecord("sys_attachment");
			gr.addQuery("table_name", "sc_cart_item"); //unnecessary, but improves query efficiency
			gr.addQuery("table_sys_id", cat_id);
			gr.query(); //synchronous for non-service portal CMS.
			if (!gr.hasNext()) {
            g_form.addErrorMessage("Please Attach a File");
            return false;
			}
		}
	}catch(e){
		g_form.addErrorMessage(e);
		return false;
	}
}

For Mobile/Service Portal: 

function onSubmit() {
   //Type appropriate comment here, and begin script below
	var condition= g_form.getValue('condition');
	
	if(condition=='upload_file')
		{

			if (this.document.getElementsByClassName('get-attachment').length == 0)	{
				 g_form.addErrorMessage('Please Attach a file');return false;
			}
		}
}

Thanks German Monsalve. This worked perfectly for me.

 

Regards,

Sailesh J

 

Thank you. This worked!

What if their are multiple options which require attachments 

Indrajit
Mega Guru

 

 

Hey gnunez,

refer below links might help you,

Make Attachment Mandatory - For A particular Catalog Item

Can you make an attachment mandatory?

Attachment mandatory

Attachment mandatory for catalog item

 

Kindly mark Correct and Helpful if applicable.

Regards,

Indrajit.