Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to limit number of attachment

sparkles
Tera Contributor

Hi All,

I am looking for a way  to restrict the user from adding more than one attachment. its for costume application and I am using attachment variable 

So, If there is more than one attachment, the user should receive a message saying " Only one attachment allowed, do you want to replace the existing one? — Yes/NO"

If the user clicks on "Yes" it should override with the new attachment.

Any help would be appreciated.

Thanks

S

2 REPLIES 2

HrishabhKumar
Kilo Sage

Hi @sparkles ,

You can check that by using a before business rule. Implementation is given below try that and see if it works:

  1. Create a Business Rule to Handle the Attachment Restriction:

    • The business rule will check if there are multiple attachments and prompt the user to confirm whether they want to replace the existing one.
  1. Navigate to Business Rules:

    • Go to System Definition > Business Rules.
    • Click on New to create a new business rule.
  2. Configure the Business Rule:

    • Name: Restrict Single Attachment
    • Table: [Your Custom Table]
    • When: before
    • Insert: Checked
    • Update: Checked
    • Filter Conditions: [Your Condition to Check if the Record Has Multiple Attachments]

 

 

(function executeRule(current, previous /*null when async*/) {

    // Check the number of attachments
    var attachmentGR = new GlideRecord('sys_attachment');
    attachmentGR.addQuery('table_sys_id', current.sys_id);
    attachmentGR.query();

    var attachmentCount = 0;
    while (attachmentGR.next()) {
        attachmentCount++;
    }

    if (attachmentCount > 1) {
        // If more than one attachment, show confirmation dialog to the user
        var gs = GlideSystem.getMessage;
        gs.addInfoMessage('Only one attachment allowed, do you want to replace the existing one?');
        
        var answer = gs.confirm('Only one attachment allowed, do you want to replace the existing one?');

        if (!answer) {
            // If user chooses "No", prevent form submission
            gs.addErrorMessage('Please remove extra attachments before submitting the form.');
            current.setAbortAction(true);
        } else {
            // If user chooses "Yes", delete existing attachments
            attachmentGR.query();
            while (attachmentGR.next()) {
                attachmentGR.deleteRecord();
            }
        }
    }

})(current, previous);

 

 

 

Thanks,

Hope this helps.

If my response turns helpful please mark it helpful and accept the solution.

 

Thanks @HrishabhKumar for your quick reply, I need something from client side. I believe Business Rules for server side only, please correct me if I am wrong.

 

Thanks,

S