update all approvals as a single comment

Community Alums
Not applicable

Hi,

I want to add all approvals to the additional comments of ritm as a single comment. I tried writing Business rule on sc_req_item with below code.

 

(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord("sysapproval_approver");
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state','requested');
    gr.query();

    var pendingApprovers = [];
   

    while (gr.next()) {
        pendingApprovers.push(gr.approver.getDisplayValue());
    }
       
    if (pendingApprovers.length>0) {
        var comment = 'Approval requested for: ' + pendingApprovers.join(',') +'.';

        if(!current.comments){
            current.comments = (current.comments || '') + comment;
       
        current.update();
    }}
 
The approvals are adding as below format 
Shannu_0-1732209154179.png

The approvals which are in requested state should be added as a single comment only but it is triggering the number of times the approvals are requested. I want to add all the approvals in requested state in single additional comment and approval in approved or rejected in another additional comment. Can someone pls suggest what's wrong in the script. Thanks

1 REPLY 1

Najmuddin Mohd
Mega Sage

Hi @Community Alums ,
Additional comments are of type Journal. So, whenever you use it, it will hit and go down and create a record in sys_journal_field table.

So, if you want to append to the same one, you need to GlideRecord sys_journal_field filtering with element comments and Element ID with sys_id of the current record, which is Highly discouraged 

and edit the same one.

GlideRecord sys_journal_element is not a good practise. It holds all the comments of all the records.

It's better to push back the requirement or to accept the current acceptance.

If the above information helps you, kindly mark the above information Helpful or Accept the solution.
Regards,
Najmuddin.