Making Attachment Mandatory in Table Form and workspace

MYousaf111
Tera Contributor

Hi guys, Can you help me out with this? I am working on the CSM workspace and we know that the workspace UI is a table form and if we make changes in the form it should reflect in the workspace. My requirement is to make attachment mandatory if the document field is 'Yes' in the workspace but I can't find a way to do it. I have seen tutorials but they are not working. Even the business rule is not a good option to use in fact it gives an ugly error (Invalid Insert) which is probably caused by using SetAbortAction. I just need a solution using Clientscript and scriptinclude or what ever you suggest the best way.

4 REPLIES 4

Paul Curwen
Giga Sage

Take a look at option 5 here:

 

https://www.servicenow.com/community/developer-articles/possible-ways-for-making-an-attachment-manda...

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

I don't want to apply this on the catalog item but on the form. Take incident form as an example, if the category is selected to software then attachment should be mandatory. I have code written lemme show you. The problem is its all working fine except one thing, and that is when the attachment is attached then the form does not submit.
Client side:

function onSubmit() {
    var cat = g_form.getValue('category');
    var sysId = g_form.getUniqueValue();

    if (cat == 'software') {
        var ga = new GlideAjax('decMandatory');
        ga.addParam('sysparm_name', 'demoTest');
        ga.addParam('sysparm_Id', sysId);
        ga.getXMLAnswer(function(response) {
            var anstrue = response === 'true';
            var ansfalse = response === 'false';
            if (ansfalse) {
                g_form.addErrorMessage('Attachment is required for software category.');
                // g_form.submit(false);
                return false;
            } else if (anstrue) {
                // g_form.submit(true); 
                return true;
            }
        });
        return false;
    }
    return true
}.

and this is server side
var decMandatory = Class.create();
decMandatory.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    demoTest: function() {
        var docSysId = this.getParameter('sysparm_Id');
        var gr = new GlideRecord('sys_attachment');
        gr.addQuery('table_sys_id', docSysId);
        gr.query();
        if (gr.next()) {
            return true;
        } else {
            return false;
        }

    },
    type: 'decMandatory'
});

Sid_Takali
Kilo Patron
Kilo Patron

HI @MYousaf111 Create onSubmit Client Script and use below code. I have tested and it's working fine

 

 

function onSubmit() {

    if ((g_form.getValue('u_document') == 'true') || (this.document.getElementsByClassName('get-attachment').length > 0)) {
        g_form.addErrorMessage(getMessage('Attachment is required when the field is set to True.'));
        return false;
    }
}

Sid_Takali_0-1720202399226.png

 

 

 

 

Please mark my response Correct/Helpful

Regards,

Sid

It does show an error when submitted without an attachment but when the attachment is attached the form does not get submitted.