Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help on adding additional comment via script

Tanya10
Tera Contributor

Hi,
I have written this BR which is before on sys_attachment table, restricting user from deleting an attachment once the request is submitted, I want to updated an additional comment which is not happening at this moment

(function executeRule(current, previous /*null when async*/ ) {
    var parentTable = current.getValue('table_name');
    var parentSysId = current.getValue('table_sys_id');

    // Only block deletion on RITMs
    if (parentTable == 'sc_req_item') {

        var grRequest = new GlideRecord('sc_req_item');
        if (grRequest.get(parentSysId)) {

            var catalog = grRequest.getValue('cat_item');

            if (catalog == 'sys_id of item') {                
                    gs.addErrorMessage("You are not allowed to delete attachments for this item.");
                    grRequest.comments = "test";
					grRequest.update();
                    current.setAbortAction(true);
                
            }
        }
    }
})(current, previous);
4 REPLIES 4

trinadh4
Tera Contributor

Hello ,

 

Try below Code and please explain more about how you would like to update additional commments ? 

 

(function() {
    var PRIVILEGED_ROLES = ['admin'];
    for (var i = 0; i < PRIVILEGED_ROLES.length; i++) {
        if (gs.hasRole(PRIVILEGED_ROLES[i])) return;
    }

 

    // Find the parent attachment
    var attId = current.getValue('attachment');
    if (!attId) {
        gs.addErrorMessage('Unable to verify attachment. Deletion blocked.');
        current.setAbortAction(true);
        return;
    }

 

    var sa = new GlideRecord('sys_attachment');
    if (!sa.get(attId)) {
        gs.addErrorMessage('Attachment not found. Deletion blocked.');
        current.setAbortAction(true);
        return;
    }

 

    var parentTable = sa.getValue('table_name');
    var parentSysId = sa.getValue('table_sys_id');
    var ENFORCE_ON_TABLES = ['sc_request', 'sc_req_item'];

 

    if (!parentTable || ENFORCE_ON_TABLES.indexOf(parentTable) === -1) return;

 

    var parent = new GlideRecord(parentTable);
    if (!parent.get(parentSysId)) {
        gs.addErrorMessage('Unable to verify parent record. Deletion blocked.');
        current.setAbortAction(true);
        return;
    }

 

    var submitted = false;

 

    if (parentTable === 'sc_request') {
        var state = (parent.getValue('request_state') || '').toLowerCase();
        submitted = (state !== 'draft');
    }

 

    if (parentTable === 'sc_req_item') {
        var stage = (parent.getValue('stage') || '').toLowerCase();
        var submittedStages = [
            'submitted',
            'awaiting_approval',
            'approved',
            'work_in_progress',
            'closed'
        ];
        submitted = (submittedStages.indexOf(stage) !== -1);
    }

 

    if (submitted) {
        gs.addErrorMessage('Attachments cannot be deleted after the request is submitted.');
        current.setAbortAction(true);
    }
})();

Ankur Bawiskar
Tera Patron
Tera Patron

@Tanya10 

since the BR is on sys_attachment your user won't see the message

but yes you can update the comments

try this

(function executeRule(current, previous /*null when async*/ ) {
    var parentTable = current.getValue('table_name');
    var parentSysId = current.getValue('table_sys_id');

    if (parentTable == 'sc_req_item') {
        var grRequest = new GlideRecord('sc_req_item');
        if (grRequest.get(parentSysId)) {
            var catalog = grRequest.getValue('cat_item');
            if (catalog == 'sys_id of item') {
                // Add error message and abort deletion
                gs.addErrorMessage("You are not allowed to delete attachments for this item.");

                // Use GlideRecord API for journal comments
                grRequest.comments.setJournalEntry('test');

                grRequest.update();

                current.setAbortAction(true);
            }
        }
    }
})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hi @Ankur Bawiskar 
I tried this but it is not working

@Tanya10 

did you debug and see if it's going inside that IF?

if you get the color log which I added then it should update the comments field on RITM

(function executeRule(current, previous /*null when async*/ ) {
    var parentTable = current.getValue('table_name');
    var parentSysId = current.getValue('table_sys_id');

    if (parentTable == 'sc_req_item') {
        var grRequest = new GlideRecord('sc_req_item');
        if (grRequest.get(parentSysId)) {
            var catalog = grRequest.getValue('cat_item');
            if (catalog == 'sys_id of item') {
                // Add error message and abort deletion
                gs.addErrorMessage("You are not allowed to delete attachments for this item.");

                gs.info('Inside IF record found');

                // Use GlideRecord API for journal comments
                grRequest.comments.setJournalEntry('test');

                grRequest.update();

                current.setAbortAction(true);
            }
        }
    }
})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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