Comments updating multiple times when changes happen in related list records.

AB7098
Tera Contributor

Hi all , In related list of  sc_req_item table I added ,my custom table as related list which have ui action button (Approve)
and that table records have status field in it.
I have requirement when list of records are selected in custom table and custom UI action button (Approve) is clicked, those selected records status should change to approved and state of respected ritm should be changed to closed complete and comments in RITM should be updated as 'Entitlement appoved' after 5 seconds.
so i tried with schedule trigger and After update BR.
But now the comments and state are updating multiple times. Like if i selected 5 records and click approve , 5 times state and comment are updating in RITM. But it should be only one time. How to achieve this?

Below is the after update BR i used:

(function executeRule(current, previous /*null when async*/) {
    if (current.u_status == 'Approved') {
        var ritmSysId = current.u_ritm;

        // Create a scheduled job to auto-close the RITM after approval
        var scheduledJob = new GlideRecord('sys_trigger');
        scheduledJob.initialize();
        scheduledJob.name = 'Autoclose RITM After Approval Bulk Entitlement';
        scheduledJob.script = 'var ritm = new GlideRecord("sc_req_item"); ' +
                              'if (ritm.get("' + ritmSysId + '")) { ' +
                              '   if (!ritm.comments.includes("Entitlement Approved.")) { ' +
                              '       ritm.state = 3; ' +
                              '       ritm.approval = "approved"; ' +
                              '       ritm.comments = "Entitlement Approved."; ' +
                              '       ritm.update(); ' +
                              '       var request = new GlideRecord("sc_request"); ' +
                              '       if (request.get(ritm.request)) { ' +
                              '           request.state = 3; ' +
                              '           request.update(); ' +
                              '       }' +
                              '   }' +
                              '}';

        var nextActionTime = new GlideDateTime();
        nextActionTime.addSeconds(5);
        scheduledJob.next_action = nextActionTime;
        scheduledJob.trigger_type = 0;
        scheduledJob.insert();
    }
})(current, previous);
Screenshot (30).png
1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @AB7098 ,

Try this script in your business rule.

 

(function executeRule(current, previous /*null when async*/) {

    if (current.u_status == 'Approved') {

        var ritmSysId = current.u_ritm;

var ritmGr = new GlideRecord('sc_req_item');

if(ritmGr.get(ritmSysId){

var comments = ritmGr.comments.getJournalEntry(-1);

if(comments.indexOf('Entitlement Approved.') == -1){

ritmGr.setValue('state', 3);

ritmGr.setValue('approval', 'approved');

ritmGr.comments = 'Entitlement Approved.';

ritmGr.update();

 

//request

var reqGr = new GideRecord('sc_request');

if(reqGr.get(ritmGr.getValue('request'))){

reqGr.setValue('state', 3);

reqGr.update();

}

 

}

}

}

}

 

Please mark my answer helpful 👍 and accept as a solution if it helped you.

 

Thanks,
Anvesh

View solution in original post

4 REPLIES 4

Brian Lancaster
Tera Sage

This is because the br is running on each record that gets set to approved. I think maybe a flow would work better that waits for all approvals and then updates the RITM. In the flow there is a timer so you can get ride of your scheduled job as well.

Brad Bowman
Kilo Patron
Kilo Patron

Add a line to your GlideRecord before the get:

 

ritm.addQuery('state', !=, 3);

 

so that only Open RITMs are updated.  The first updated custom table record for each RITM will add the comment and close it, then the rest for that same RITM will not.

AnveshKumar M
Tera Sage
Tera Sage

Hi @AB7098 ,

Try this script in your business rule.

 

(function executeRule(current, previous /*null when async*/) {

    if (current.u_status == 'Approved') {

        var ritmSysId = current.u_ritm;

var ritmGr = new GlideRecord('sc_req_item');

if(ritmGr.get(ritmSysId){

var comments = ritmGr.comments.getJournalEntry(-1);

if(comments.indexOf('Entitlement Approved.') == -1){

ritmGr.setValue('state', 3);

ritmGr.setValue('approval', 'approved');

ritmGr.comments = 'Entitlement Approved.';

ritmGr.update();

 

//request

var reqGr = new GideRecord('sc_request');

if(reqGr.get(ritmGr.getValue('request'))){

reqGr.setValue('state', 3);

reqGr.update();

}

 

}

}

}

}

 

Please mark my answer helpful 👍 and accept as a solution if it helped you.

 

Thanks,
Anvesh

AB7098
Tera Contributor

@AnveshKumar M  thakyou. its working.