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.

Script written in below Business rule not getting triggered properly

Community Alums
Not applicable
 var durationProd = 0; // Total time in milliseconds
        var lastProdChangeTime = '';

        // Query the sys_audit table for changes to the "Next Action" field
        var auditGR = new GlideRecord('sys_audit');
        auditGR.addQuery('documentkey', current.sys_id);
        auditGR.addEncodedQuery('fieldname=u_next_action');
        auditGR.orderBy('sys_created_on');
        auditGR.query();

        while (auditGR.next()) {
            var newValue = auditGR.newvalue.toString();
            var changeTime = new GlideDateTime(auditGR.sys_created_on);

            if (newValue == 'Resolved Pend Cust Handshake') {
                if (lastProdChangeTime) {
                    gs.info('lastProdChangeTime:' + lastProdChangeTime);
                    gs.info('changeTime:' + changeTime);
                    var diff = GlideDateTime.subtract(lastProdChangeTime, changeTime).getNumericValue();
                    gs.info('diff:' + diff);
                    durationProd += diff;
                    gs.info('durationProd:' + durationProd);

                }
                lastProdChangeTime = changeTime; // Update the last change time to "Prod"

                gs.info('lastProdChangeTime:' + lastProdChangeTime);
            }

            var r = durationProd / (1000 * 60);
            var removedecimal = Math.floor(r);
            current.u_rpchs_duration = removedecimal + ' Minutes';
        }




    }

HI Team,

 

I am facing an issue wherien I have written the below BR with trigger condition as next action changes but it does not trigger immediately.

I am trying to update the number of minutes a field "u_next_action" holds the value 

"Resolved Pend Cust Handshake". I am quering the sys_audit table to check the newvalue as same. 
Issue is suppose  for the first time I put the next action field as "Resolved Pend Cust Handshake"
, system does not update "u_rpchs_duration" field. It updates after 3 times I change the value to "Resolved Pend Cust Handshake" , it reflects the total duration.

Can you tell how to fix this?

 

 

 

var durationProd = 0; // Total time in milliseconds
        var lastProdChangeTime = '';

        // Query the sys_audit table for changes to the "Next Action" field
        var auditGR = new GlideRecord('sys_audit');
        auditGR.addQuery('documentkey', current.sys_id);
        auditGR.addEncodedQuery('fieldname=u_next_action');
        auditGR.orderBy('sys_created_on');
        auditGR.query();

        while (auditGR.next()) {
            var newValue = auditGR.newvalue.toString();
            var changeTime = new GlideDateTime(auditGR.sys_created_on);

            if (newValue == 'Resolved Pend Cust Handshake') {
                if (lastProdChangeTime) {
                    gs.info('lastProdChangeTime:' + lastProdChangeTime);
                    gs.info('changeTime:' + changeTime);
                    var diff = GlideDateTime.subtract(lastProdChangeTime, changeTime).getNumericValue();
                    gs.info('diff:' + diff);
                    durationProd += diff;
                    gs.info('durationProd:' + durationProd);

                }
                lastProdChangeTime = changeTime; // Update the last change time to "Prod"

                gs.info('lastProdChangeTime:' + lastProdChangeTime);
            }

            var r = durationProd / (1000 * 60);
            var removedecimal = Math.floor(r);
            current.u_rpchs_duration = removedecimal + ' Minutes';
        }

 

1 REPLY 1

Community Alums
Not applicable

Can you show the trigger of your BR ? As well as the entire script of your BR - screenshots are most welcome.