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

    • Hello isha there is feild called isolate script in client script form  make that to false then it will run 

the isolate script should false and paste the code i have provided and this is working in pdi as per your requirement mentioned above  

find_real_file.png

RC8
Kilo Guru

Hi Isha

try the below onsubmit() script it works fine for me 

function onSubmit() {
    var a = g_form.getValue('checkbox'); // Map your field name
    if (a == 'true') {
        if (this.document.getElementsByClassName('get-attachment').length < 1) {
            alert('attachment_is_mandatory');
            return false;
        }
    }
    if (a == 'false') {
        if ((this.document.getElementsByClassName('get-attachment').length > 0) && (a == 'false')) {
            g_form.setValue('checkbox', true); // Map your field name
        }
    }
}

=(tested)

Let me know if any

 

Regards,

Rakesh

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

Hi,

 

If your query is resolved, can you please mark my answer as correct and close this thread.

Regards,

Shloke

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

Regards,
Shloke