Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Attachment mandatory when the field is appear on the change Form

MonikaRajuC
Tera Contributor

I have a requirement related to Risk Assessment and Change Management.

When a user submits the Risk Assessment, if they answer “No” to one of the questions, a follow‑up question is displayed. The value from that follow‑up question is then copied to the Change form (under the Planning tab).

Now, based on the value copied to the Change form (either Yes or No), we need to make attachments mandatory on the Change form.

We currently use a Business Rule to enforce the mandatory attachment requirement, and it mostly works.
However, there is one issue:

If the user adds an attachment before submitting the Risk Assessment, and then completes the assessment afterward, the field value on the Change form becomes “Yes” or “No”, but the Business Rule does not trigger because there is already one attachment on the form.
So the attachment requirement does not enforce correctly in this scenario.

How should I handle this situation so that the attachment is always mandatory when the field value is “Yes” or “No”, regardless of when attachments were added?

 

Please find the attached file for more details.

 

Thank you

12 REPLIES 12

MonikaRajuC
Tera Contributor

Thank you everyone for your responses. The issue regarding the attachment mandatory is resolved.

 

Thanks,

Monika

Hi @MonikaRajuC , What approach did you used? How were you able to resolve the requirement?
Regards,

Saurabh V.

Hi Saurabh,

 

(function executeRule(current, previous) {

    var lastTestPlanUpdate = null;

    // Get last Test Plan update timestamp from sys_audit
    var auditGR = new GlideRecord('sys_audit');
    auditGR.addQuery('documentkey', current.sys_id);
    auditGR.addQuery('fieldname', 'test_plan');   // <-- replaced risk field with test_plan
    auditGR.orderByDesc('sys_created_on');
    auditGR.setLimit(1);
    auditGR.query();

    if (auditGR.next()) {
        lastTestPlanUpdate = auditGR.sys_created_on.getValue();
    }

    // If Test Plan was never updated → allow (your original logic)
    if (!lastTestPlanUpdate) {
        return;
    }

    // Only enforce when State changes
    if (!current.state.changes()) {
        return;
    }
 var attGR = new GlideRecord('sys_attachment');
    attGR.addQuery('table_sys_id', current.sys_id);
    attGR.orderByDesc('sys_created_on');
    attGR.setLimit(1);
    attGR.query();

    var lastAttachment = null;

    if (attGR.next()) {
        lastAttachment = attGR.sys_created_on.getValue();
    }

    // No attachments at all
    if (!lastAttachment) {
        gs.addErrorMessage(
            "Please include a comprehensive pre-test plan with ESTABLISHED and ATTACHED documented test results. This includes the availability of a suitable testing environment where outcomes can be effectively VALIDATED."
        );
        current.setAbortAction(true);
        return;
    }

    // Attachment must be newer than test_plan update
    if (lastAttachment <= lastTestPlanUpdate) {
        gs.addErrorMessage(
            "Please include a comprehensive pre-test plan with ESTABLISHED and ATTACHED documented test results. This includes the availability of a suitable testing environment where outcomes can be effectively VALIDATED."
        );
        current.setAbortAction(true);
        return;
    }

})(current, previous);
 
I used this logic, it will check the audit . If no Attachment is added when the field appears it will trigger the error message. If attachment is added also it will trigger the error when the particular field is appear on form.
Hope this logic will useful for others. 
Thanks,
Monika