Attachment to be attached if the user Checks the checkbox

Balaraju K B
Tera Expert

Hi Team,

I have a check box in my catalog item

  • If the user checks that check box then he must attach an attachment
  • If the user not checking that check box in this case he can submit the catalog item without attaching.

Please let me know how to get this done?

@Ankur Bawiskar 

 

Thanks in Advance.,

Balaraju K B

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Balaraju K B 

this onSubmit client script should help

1) Ensure the Isolate Script field is set to false for this client script

2) By default when you create new client script it is true

Note:

Ensure you give valid checkbox variable name and your message for alert/form message

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

	if(g_form.getValue('checkbox_variable').toString() == 'true'){
		if(window == null){
			// portal
			if(this.document.getElementsByClassName('get-attachment').length == 0) {
				g_form.addErrorMessage('You must add attachment before submitting this request.');
				return false;
			}
		}
		else{
			// native view
			var length = $j("li.attachment_list_items").find("span").length;
			if(length == 0){
				g_form.addErrorMessage('You must add attachment before submitting this request.');
				return false;
			}
		}
	}
}

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

15 REPLIES 15

Alok Das
Tera Guru

Hi Balaraju,

I wish if you were on Paris then you could have done this very easily without any coding. However please find the below link for your information which you can use after your upgrade:

https://community.servicenow.com/community?id=community_article&sys_id=d21e5f76dbf610147d3e02d5ca961...

Since, you are in orlando release, you may have a look on below Article to achieve your target:

https://community.servicenow.com/community?id=community_article&sys_id=5df1797b1b0c5050fff162c4bd4bc...

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

Regards,

Alok

Willem
Giga Sage
Giga Sage

You can do it as follows.

Create a Script include:

var AttachmentUtil = Class.create();
AttachmentUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    AttachmentMandatory: function () {
        var sysID = this.getParameter('sysparm_id');
        var att = new GlideRecord('sys_attachment');
        att.addQuery('table_name', 'sc_cart_item');
        att.addQuery('table_sys_id', sysID);
        att.query();
        if (!att.next()) {
            return false;
        }
        else {
            return true;
        }
    }
})

 

Then create onSubmit client script:

Replace your_checkbox_field with the your checkbox field.

function onSubmit() {
    if (g_form.getValue('your_checkbox_field')) {
        var id = g_form.getValue('sysparm_item_guid');
        var ga = new GlideAjax('AttachmentUtil');
        ga.addParam('sysparm_name', 'AttachmentMandatory');
        ga.addParam('sysparm_id', id);
        ga.getXMLWait();
        var ans = ga.getAnswer();
        if (ans == "false") {
            alert("Please add attachment");
            return false;
        }
    }

}

 

Based on:

https://community.servicenow.com/community?id=community_question&sys_id=d117ed1cdb278c901cd8a345ca96...

Yash Agrawal1
Tera Guru

Hello Balaraju,

You can use OOTB functionality.

Please open the Catalog item go to "Portal Settings" section and check "mandatory attachment"

Please refer belowfind_real_file.png

Please Mark it helpful/correct if my answer helps in any way to resolve your query.
Reach out to me if any more help required.

Regards

Yash.K.Agrawal

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Balaraju K B 

this onSubmit client script should help

1) Ensure the Isolate Script field is set to false for this client script

2) By default when you create new client script it is true

Note:

Ensure you give valid checkbox variable name and your message for alert/form message

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

	if(g_form.getValue('checkbox_variable').toString() == 'true'){
		if(window == null){
			// portal
			if(this.document.getElementsByClassName('get-attachment').length == 0) {
				g_form.addErrorMessage('You must add attachment before submitting this request.');
				return false;
			}
		}
		else{
			// native view
			var length = $j("li.attachment_list_items").find("span").length;
			if(length == 0){
				g_form.addErrorMessage('You must add attachment before submitting this request.');
				return false;
			}
		}
	}
}

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader