How to make Attachment Mandatory for Record Prouder request created in a Scoped Application?

Salah2
Tera Contributor

Hi all, 

Created a Record Producer within an Application Scope from App Engine Studio.  We are struggling to make attachment mandatory.  

Requirement: If "id_required_for_every_employer" is "Yes", then set Attachment Mandatory/prevent request submission | Field type:  Option - Yes/No

Release version: Utah 

Script Include: 

Name:CheckAttachPRecodProducer | Client callable: true | Application: Fow Application | Accessible: This App scrope

 

var CheckAttachPRecodProducer = Class.create();
CheckAttachPRecodProducer.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
CheckAttachments: function() {
        var pForm = this.getParameter('sysparm_p_form');
        var pForm = new GlideRecord("sys_attachment");
        pForm.addQuery('table_name', "x_appl_di_tile_form");
        pForm.addQuery('table_sys_id', dataCheck);
        pForm.query();

        if (gr.next()) {
            return 'yes';
        } else {
            return 'no';
        }
    },
    type: 'CheckAttachPRecodProducer'
});

 

Catalog Client Scripts: 

Applies: Catalog Item | UI Type: All | Type: onChanage | CatalogItem: Fow Request | VariableName: id_required_for_every_employer

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue == "Yes")
        var pAttach = new GlideAjax('CheckAttachPRecodProducer');

    pAttach.addParam('sysparm_name', 'CheckAttachments');
    pAttach.addParam('sysparm_p_attach', g_form.getValue("p_attach"));
    pAttach.getXML(clientCallback);
}
function clientCallback(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == 'yes') {
        return true;
    } else if (answer == 'no') {
        alert("You must attach document before submitting the form.");
        return false;
    }
}

 

 

need your help a bit desperately.  Please, and thank you! 

1 ACCEPTED SOLUTION

Harish KM
Kilo Patron
Kilo Patron

Follow the procedure below, it will be reusable for all catalogs to make a attachment mandatory

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0743672

 

Once you set up UI script and added to Portal. you could follow below client script

 

function onSubmit() {
if ( g_form.getValue('id_required_for_every_employer') == "No") {
       
        var alertWord = "Please use the paperclip icon below to attach documents before submitting the request.";
       
        try {
            var attachments = g_form.getControl('header_attachment_list_label');
            if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
                alert(alertWord);
                return false;
            }
           
        } catch (e) { //For Service Portal
           
            var count = getSCAttachmentCount();
           
            if (count <= 0) {
                alert(alertWord);
                return false;
            }
        }
    }

 

Regards
Harish

View solution in original post

7 REPLIES 7

The alert is coming as null. Can you check the variable name is correct?

Regards
Harish

Salah2
Tera Contributor

@Harish KM You have made my day!  Thank you brother!  Fixed!

Glad to know it is fixed 🙂

Regards
Harish