Mandatory confirmation on attachments

Carol2
Tera Contributor

Hi All 

 

I have a requirement I need assistance with "Mandatory confirmation on attachments for Implementation Plan, Test Evidence, Business Approval Pre and Post Implementation on all Changes" what could be the best way to create this confirmation?

 

User must be able to somehow tick that 3 attachments have been added to the change request before submitting. 

 

 

 

9 REPLIES 9

Runjay Patel
Giga Sage

Hi @Carol2 ,

 

Best way to achieve this to write onsubmit client script. 

  1. Create one onsubmit client script.
  2. Create one script include.
  3. Call script include from clinet script to get the number of attachment.
  4. Apply the logic and return false to abort the action if count is not more than or equal to 3.

Client side code:

function onSubmit() {
    
    if (!g_form.hasAttachments()) {
        g_form.addErrorMessage("You must add at least one attachment before submitting.");
        return false; // Prevent form submission
    }

var recordSysId = g_form.getUniqueValue();

    var ga = new GlideAjax('AttachmentCount');
    ga.addParam('sysparm_name', 'getAttachmentCount');
    ga.addParam('sysparm_recordSysId', recordSysId);

    ga.getXMLAnswer(function(response) {
        var attachmentCount = parseInt(response.responseXML.documentElement.getAttribute("answer"), 10);
       if(attachmentCount <3){
g_form.addErrorMessage("You must add at least three attachment before submitting.");
return false;

}
    });




    return true; // Allow form submission
}

 

Server side code:

getAttachmentCount: function(recordSysId) {
        var count = 0;
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_sys_id', recordSysId);
        attachmentGR.query();
        while (attachmentGR.next()) {
            count++;
        }
        return count; // Return the count of attachments
    },

 Make sure script include is client callable.

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Hi Runjay 

 

The record sys id in the script include is it for change request table or the attachment sys id?

 

Regards

Ca

Hi @Runjay Patel 

 

I also get this error when i test MessageonSubmit script error: TypeError: g_form.hasAttachments is not a function, how do i resolve this issue?

 

Regards 

Ca

Sysid of change request.

you can remove first validation from top, it will be taken care by script include.

Let me know if still face any issue