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

Abish
Mega Guru
Mega Guru

Hi Isha,

For your first requirement you can have a onchange client script written on the checkbox so that when user clicks on the checkbox you can check if the attachment is added if not you can show an error message. 

See how to check if attachment are added or not on below links:

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

https://www.basicoservicenowlearning.in/2020/10/attachment-mandatory.html

You can make minor changes to the above and you can check your conditions. 

For your second requirement you can have this script on the server side code of catalog or on a OnSubmit script to update it.

 

Please mark correct and helpful if I have answered your question.

Regards

Abi R

Isha1
Kilo Contributor

Hi Abish,

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 

Hi Isha,

You have to abort the submission on the same block where you have specified the alert. That would prevent it from moving to the next step.

 

if(<your conditions>){

alert('No attachments');

return false;

}

return true;

Please mark my response as correct and helpful if I have answered your question.

Regards

Abi R

ABHAY_M
Tera Guru

Possible Ways for Making an Attachment Mandatory : Service Portal/Native UI

 

go through this awesome article and get the code form there  and change client script according to your checkbox field use case 

Thanks 
Abhay