faccing an issue in business rule script. script will calculate the time when state changes

MuhammadM
Tera Contributor
(function executeRule(current, previous ) {
    if (current) {

        if (current.state.changes()) {

            var previousState = previous.state;
            var previousUpdateTime = new GlideDateTime(previous.sys_updated_on); // Get the time of the previous update
            var currentUpdateTime = new GlideDateTime(current.sys_updated_on); // Get the time of the current update

            // Calculate the time difference in milliseconds
            var duration = currentUpdateTime.subtract(previousUpdateTime); // Get the duration as GlideDuration
            gs.info("duration" + duration);
            var timeDiffInMillis = duration.getTimeInMillis(); // Get duration in milliseconds
            gs.info("timeDiffInMillis" + timeDiffInMillis);

            // Convert milliseconds to hours (1 hour = 3600000 milliseconds)
            var timeSpentInHours = timeDiffInMillis / 3600000;
            gs.info(" timeSpentInHours" + timeSpentInHours);
            // Update the appropriate custom field based on the previous state
            if (previousState == 'New') {
                current.u_time_in_new = (current.u_time_in_new || 0) + timeSpentInHours;
            } else if (previousState == 'In Progress') {
                current.u_time_in_progress = (current.u_time_in_progress || 0) + timeSpentInHours;
            } else if (previousState == 'Resolved') {
                current.u_time_in_resolved = (current.u_time_in_resolved || 0) + timeSpentInHours;
            } else if (previousState == 'Closed') {
                current.u_time_in_closed = (current.u_time_in_closed || 0) + timeSpentInHours;
            } else if (previousState == 'Pending') {
                current.u_time_in_pending = (current.u_time_in_pending || 0) + timeSpentInHours;
            } else if (previousState == 'On Hold') {
                current.u_time_in_on_hold = (current.u_time_in_on_hold || 0) + timeSpentInHours;
            } else if (previousState == 'Awaiting User Info') {
                current.u_time_in_awaiting_user_info = (current.u_time_in_awaiting_user_info || 0) + timeSpentInHours;
            } else {
                // Handle any unexpected or new states here
                gs.info('Unexpected state encountered: ' + previousState);
            }
            gs.info("previous State " + previousState);
            gs.info("previousUpdateTime " + previousUpdateTime);
            gs.info("currentUpdateTime  " + currentUpdateTime);
            gs.info("currentUpdateTime " + currentUpdateTime);
        }
    } else {
        gs.info('Script triggered on a null current record.');
    }


})(current, previous);



above is my script i am trying to calculate the time when state changes. it show me an error that current is null anyone help me to solve this issue 

2 REPLIES 2

HIROSHI SATOH
Mega Sage

The error you are encountering, where current is null, can happen in several situations, such as if the script is triggered in the wrong context or if the current object isn't properly passed.

Here are some possible solutions:

  1. Check the Business Rule Conditions:
    Ensure that the Business Rule is set to run on the correct table and that the "When to run" conditions (before/after, insert/update/delete) are appropriately configured. The current object should be available when the script is running on insert or update.

  2. Validate Triggering Conditions:
    Double-check that the script is only being triggered when there is a valid current object. For example, ensure that the script is not running on a delete operation unless explicitly intended.

  3. Add Debugging Information:
    You can add some gs.info() statements at the start of the script to log the contents of current and previous objects to help diagnose when and why current might be null.

     

 

gs.info('Current object: ' + JSON.stringify(current));
gs.info('Previous object: ' + JSON.stringify(previous));

 

  • Ensure current is Passed Correctly:
    In the script include or any other place where this function might be called, ensure that current is passed correctly.

  • Script Logic Improvements:
    It's also a good idea to check if the current.state.changes() logic is being correctly evaluated. Sometimes, depending on the Business Rule timing, changes() might not behave as expected.

Example of Debugging:

Try adding these checks at the start:

 

 

(function executeRule(current, previous) {
    if (!current) {
        gs.error('Current is null in the business rule.');
        return;
    }

    if (!previous) {
        gs.error('Previous is null in the business rule.');
        return;
    }

    if (current.state.changes()) {
        // Your logic here
    }
})(current, previous);

 

 

 

Mark Manders
Mega Patron

Isn't this creating custom code to the same result as the output of the 'state'  duration metric definition?

And if you really need it on the form for whatever reason (reporting can be done from the metrics), you could copy it from the metric to the form. That saves the system doing the calculation twice.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark