Need Help: Make Attachment Mandatory on submission Only When Subject Person’s Country = India

NowNinja727
Kilo Contributor
Hi Community,

I’m working on a requirement for a record producer in Service Portal where:
  • There is a reference variable called Subject Person (points to sys_user).
  • If the selected user’s country = India, then at least one attachment must be added before submission.
  • If the country is not India, submission should proceed without attachment.

 

What We Tried So Far:

  1. OnSubmit Client Script using getReference()
    • We fetched the country from the Subject Person record and then checked attachments using Angular DOM (scope.attachments.length).
    • Issue: Works partially, but fails when attachments are added (still blocks submission).
  2. DOM Manipulation Approach
    • Used document.getElementsByClassName('get-attachment') for Service Portal and $j("li.attachment_list_items") for native UI.
  3. GlideAjax + Script Include
    • Created a Script Include (CheckAttachment) to check attachments and country.
    • Tried multiple patterns (pause submission, resubmit after validation).
    • Issue: Logic works for blocking when no attachment, but still blocks even after attachment is added.
  4. System Property Approach
    • Attempted to make country configurable via system property and validate in Script Include.
    • Issue: Still facing the same problem with attachment validation.

Where We Are Stuck:

  • We need a robust solution that works in Service Portal and Mobile UI.
  • Ideally, we want one GlideAjax call that:
    • Gets the country.
    • Checks if attachments exist for the current record.
    • Returns true/false so we can allow or block submission.

 

Any sample code or guidance would be greatly appreciated!

Thanks!
6 REPLIES 6

pavani_paluri
Tera Guru
Tera Guru

Hi @NowNinja727 ,

 Please try below code and let me know if that works

var bypassCheck = false;

function onSubmit() {
    if (bypassCheck) {
        return true; // Allow submit after Ajax passes
    }

    if (g_form.getValue('u_type_mitarbeiter') == 'u_neuer') {
        try { // Works in non-portal UI
            var cat_id = g_form.getValue('sysparm_item_guid');

            var ajax = new GlideAjax('CatalogUtilsAjax'); // your Script Include
            ajax.addParam('sysparm_name', 'hasAttachment');
            ajax.addParam('sysparm_cat_id', cat_id);

            ajax.getXML(ajaxResponse);

            // Stop first submit attempt; wait for async
            return false;
        } catch (e) { // For Service Portal
            var count = getSCAttachmentCount();
            if (count <= 0) {
                alert('You must attach a document to submit');
                return false;
            }
        }
    }
}

// Callback at root level
function ajaxResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');

    if (answer == 'false') {
        g_form.addErrorMessage('You must attach a document to submit');
    } else {
        bypassCheck = true; // prevent infinite loop
        g_form.save();      // submit the form
    }
}
 
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

John Gilmore
Giga Guru

Your use case isn't simple given that you need the functionality in both Service Portal and Mobile UI which don't support the same methods. So you would need to approach this in a way that doesn't use many of the standard functions you'll find related to Service Portal.

 

In order to apply this logic across these two UIs you will need to use a business rule with the when to apply set to before-insert. I don't know of another way to apply it to both UIs with one configuration. You would start with a  condition that Subject Person's country is India. If this evaluates to true then check the attachments.

You stated this is occurring on a record producer so the query on sys_attachment works since you are in a before-insert business rule. You would need to query sys_attachment where table_name is your table and table_sys_id is current.sys_id. This would return a count of attachments which could then be used inside the if condition to either cancel the insert or allow it to proceed.

Thanks for the reply @John Gilmore , please refer my reply below from @Sandeep Rajput 's thread and suggest me anything if you have anything, thank you so much!

Sandeep Rajput
Tera Patron
Tera Patron

@NowNinja727 Ideally a combination of GlideAjax + Script include should work here. You can simply check the subject person country and see if they uploaded an attachment by querying the sys_attachment table. However, if this is not working then there must be something wrong with the logic here.

 

Could you please share your client script and script include code here to suggest the improvements.