Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

attachment mandatory requirement

SD_Chandan
Kilo Sage
The requirement is to ensure that when attachments are copied from another record, they do not satisfy the mandatory attachment requirement. Users must upload at least one new attachment on the current record and should not be allowed to proceed with only the copied attachments.
Thank you
Chandan
2 REPLIES 2

Ankur Bawiskar
Tera Patron

@SD_Chandan 

which form is this? screenshots please

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Tanushree Maiti
Tera Patron

Hi @SD_Chandan 

 

Try this:

 

 

 

1: Flag Copied Files (Server-Side)

When your script or Flow copies attachments (using GlideSysAttachment.copy()), you must immediately add a marker to their description field. Update the Business Rule, Script Action, or Flow  that performs the copy to include this logic

 

var sourceTable = 'incident';

var sourceSysId = 'SOURCE_SYS_ID_HERE';

var forCopyAtt = new GlideSysAttachment();

forCopyAtt.copy(sourceTable, sourceSysId, current.getTableName(), current.getUniqueValue());

var attGr = new GlideRecord('sys_attachment');

attGr.addQuery('table_name', current.getTableName());

attGr.addQuery('table_sys_id', current.getUniqueValue());

attGr.addNullQuery('description');

attGr.query();

 

while (attGr.next()) {

    attGr.description = "COPIED_ATTACHMENT";

    attGr.update();

}

 

2: Create a Script Include (Server-Side Validation)

  • Name: AttachmentValidationUtils
  • Client Callable: True (Checked)

 

var AttachmentValidationUtils = Class.create();

AttachmentValidationUtils.prototype = Object.extendsObject(AbstractScriptProcessor, {

 

    hasNewUpload: function() {

        var recordId = this.getParameter('sysparm_record_id');

        var tableName = this.getParameter('sysparm_table_name');

       

        if (!recordId || !tableName) return 'false';

 

        var attGr = new GlideRecord('sys_attachment');

        attGr.addQuery('table_name', tableName);

        attGr.addQuery('table_sys_id', recordId);

        attGr.query();

        if (!attGr.hasNext()) {

            return 'false';

        }

        while (attGr.next()) {

            var desc = attGr.getValue('description') || '';

            if (desc.indexOf('COPIED_ATTACHMENT') === -1) {

                return 'true';

            }

        }

        return 'false';

    },

    type: 'AttachmentValidationUtils'

});

 

3: Create an onSubmit Client Script (Portal & Workspace Compatible)

  • Type: onSubmit
  • UI Type: All

 

function onSubmit() {

    if (g_form.isNewRecord()) {

        return true;

    }

    if (g_scratchpad.isAttachmentValid === true) {

        return true;

    }

    var ga = new GlideAjax('AttachmentValidationUtils');

    ga.addParam('sysparm_name', 'hasNewUpload');

    ga.addParam('sysparm_record_id', g_form.getUniqueValue());

    ga.addParam('sysparm_table_name', g_form.getTableName());

        ga.getXMLAnswer(function(answer) {

        if (answer === 'true') {

            g_scratchpad.isAttachmentValid = true;

            g_form.submit();

        } else {

            g_form.addErrorMessage('Submission blocked: You must upload at least one NEW attachment. Copied files do not satisfy this requirement.');

        }

    });

 

    return false;

}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti