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.

Making an attachment mandatory and auto checking the checkbox.

Isha1
Kilo Contributor

Hi All, 

Wanted your help for my requirement where I have to use Script Include and an OnSubmit client script using GlideAjax. 

Requirement is : 1. I have one checkbox in my catalog item so when i check the checkbox without adding the attachment while submitting the form it should give me an alert to add the attachment.

2.Adding to this, the second thing in the same requirement is if i add an attachment but doesn't mark the checkbox as checked so while submitting the form, the checkbox should be default tick.

Please give your suggestions.

Thanks in Advance.

 

 

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi,

You can simply achieve this  by following the below steps:

1) Create a UI Script, by navigating to UI Script module. Advantage with this approach is you don't need to use DOM directly and it will be a s a reusable utility and can be used in any client script with bare minimum effort:

UI Script as below:

function getSCAttachmentCount() {
    var length;
    try {
        length = angular.element("#sc_cat_item").scope().attachments.length;

    } catch (e) {
        length = -1;
    }
    return length;
}

Client Script:

Write an On Submit Catalog Client script on your catalog item with below code as written:

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var count = getSCAttachmentCount(); // Function called from UI Script to valdiate the count of attachments uploaded.
if(g_form.getValue('Pass your Backend Variable Name here') == true){
    if (count <= 0) {
        alert('You must attach template before submitting this request.');
        return false;
    }
}
else if(count > 0){
if(g_form.getValue('Pass your Backend Variable Name here') == false){
g_form.addErrorMessage('Please seelct the field as True');
return false;
}
}
}

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

14 REPLIES 14

Isha1
Kilo Contributor

Hi Abhay,

I tried for the first condition for making an attachment mandatory while submitting if the checkbox is checked by an OnSubmit client script and script include using GlideAjax and i am getting an alert but on the portal after getting an alert the item is moving to cart so can you guide me in that how can i stop it to move into cart after getting an alert.

Thanks,

Isha

  1. Basically you have to make return false in on sumbit catalog client script 

I will recreate this scenario in my pdi i paste the code soon

ABHAY_M
Tera Guru

Add this code and change the feild name of you check box and test this in your instance.


function onSubmit() {
//Works in non-portal ui
try {
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
alert('You must attach the completed form before submitting this request.');
return false;
}
	else if(g_form.getValue('check_box')=='false') {
		
		g_form.setValue('check_box','true');
		
	}
}
//For Service Portal
catch(e) {
var count = getSCAttachmentCount();
if(count <= 0) {
alert('You must attach the completed form before submitting this request.');
return false;
}
	else if(g_form.getValue('check_box')=='false') {
		
		g_form.setValue('check_box','true');
		
	}
}
}

  • Did this worked in your catalog item ? 

Isha1
Kilo Contributor

Hi Abhay, 

No it didn't, as its said document.getElementByID() is basically performing DOM Manipulation, which is not supported on the platform so looking for an alternate solution for this.