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.

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

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

Salah2
Tera Contributor

@Harish KM tried following the doc but still not getting the expected result.  Not sure what went wrong ....

UI Scrip

ScripName: GlobalCatalogItemFunctions

var x_aigg2_pax_appl_0 = x_aigg2_pax_appl_0 || {};

 

x_aigg2_pax_appl_0.GlobalCatalogItemFunctions = (function() {
    "use strict";
    return {
        setAttachmentMandatory: function(isMandatory) {
            var isSuccess;
            try {
                angular.element("#sc_cat_item").scope().c.data.sc_cat_item.mandatory_attachment = isMandatory;
                isSuccess = true;
            } catch (e) {
                isSuccess = false;

            }
            return isSuccess;
        },
        type: "AttachmentUtil"
    };
})();

 

Catalog Client Script: Did not change anything, just wanted to see if it's going to work. 

 

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;
            }
        }
	}
}

 

 

JS Include on S Portal Theme: 

Salah2_0-1695959425483.png

 

 

The theme you added. Is that the same using on the Portal?If yes add the alert and check the value below

 

 var getID = g_form.getValue('id_required_for_every_employer');
alert(getID);
var alertWord = "Please use the paperclip icon below to attach documents before submitting the request.";
if (getID == 'No') {

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

Salah2
Tera Contributor

This is pop-up came up

Salah2_0-1695960865433.png