on submit make attachment mandatory in catalog client script in service now

Priyanka153
Kilo Explorer

On submit make attachment mandatory on catalog client scrript

6 REPLIES 6

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi @Priyanka 

use the following catalog client script, it should work:

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;
			}
		}
}

If I have answered your question, please mark my response as correct and helpful so that this thread can be closed and others can be benefited by this.

Thank you very much

Cheers
Alberto

@Priyanka 

Did this solve your question? Or do we need to follow-up on this?

If I have answered your question, please mark my response as correct and helpful so that others with the same question in the future can find it quickly and that it gets removed from the unanswered list.

Thank you very much

Cheers
Alberto

Alok Das
Tera Guru

Hi Priyanka,

If it's attachment is mandatory based on condition then you can use the below script:

function onSubmit() {
    if (g_form.getValue("field_name") == 'abc') //condition when attachment needs to be mandatory for example 
    {
        //Works in non-portal ui
        try {
            var attachments = document.getElementById('header_attachment_list_label');
            if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
                alert('Attachment madatory');
                return false;
            }
        }

        //For Service Portal
        catch (e) {
            var count = getSCAttachmentCount();
            if (count <= 0) {
                alert('Attachment madatory');
                return false;
            }
        }
    }
}

And if it's always mandatory then you can simply check the checkbox highlighted below in catalog item and you are done. It's completely no code solution.

find_real_file.png

 

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Alok

Hello Alok,

This worked like a charm.

Thank you Sir!

Eamonn