Before closing a change ticket, it should be mandatory to attach evidence of change.

PhanitaM
Tera Contributor

 

There is a requirement where, upon clicking the Review button on the change form, a pop-up message should appear reminding users to attach evidence of change.

popup message:
"Please attach proof of change completion."

If users have already uploaded the evidence previously, they should simply re-attach the file.

 

I have implemented the logic below, but the pop-up keeps looping and repeatedly asks for the attachment. As a result, the ticket is not transitioning to the Review state.

Could someone please assist with resolving this issue?

 

UI Script

 

function moveToReview() {
    var ga = new GlideAjax('CheckChangeAttachments');
    ga.addParam('sysparm_name', 'validateAttachment');
    ga.addParam('sysparam_change_id', g_form.getUniqueValue());

    ga.getXMLAnswer(function(response) {
        if (response === 'no_attachment') {
            alert(" Please attach proof of change completion.");
        } else if (response === 'not_recent') {
            alert(" Please re-attach proof of change completion.");
        } else {
            // Proceed with review
            g_form.setValue("state", "0"); // Set your appropriate state value
            gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_review");
        }

        if (typeof window == 'undefined')
            setRedirect();

        function setRedirect() {
            current.update();
            action.setRedirectURL(current);
        }
    });
}
 
 
Script include:
var CheckChangeAttachments = Class.create();
CheckChangeAttachments.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 validateAttachment: function() {
        var changeId = this.getParameter('sysparam_change_id');
        var gr = new GlideRecord('sys_attachment');
        gr.addQuery('table_name', 'change_request');
        gr.addQuery('table_sys_id', changeId);
        gr.query();

        if (!gr.hasNext()) {
            // No attachments at all
            return 'no_attachment';
        }

        // Check if any recent attachments (within 5 minutes)
        var recentThreshold = new GlideDateTime();
        recentThreshold.subtract(new GlideDuration('00:05:00'));

        var recentGR = new GlideRecord('sys_attachment');
        recentGR.addQuery('table_name', 'change_request');
        recentGR.addQuery('table_sys_id', changeId);
        recentGR.addQuery('sys_created_on', '>=', recentThreshold);
        recentGR.query();

        if (!recentGR.hasNext()) {
            // Has attachments, but none are recent.
            return 'not_recent';
        }

        return 'valid'; // At least one recent attachment found
    },
    type: 'CheckChangeAttachments'
});
 
 
Thanks!
1 REPLY 1

J Siva
Tera Sage

Hi @PhanitaM 

May I know from where/how you are calling the UI script.

Also, try the below line of code in your script to filter the attachments which're created in last 5 mins .

recentGR.addEncodedQuery('sys_created_on>=javascript:gs.minutesAgo(5)');

Regards ,

Siva