BR not triggering correctly in SOW

mcroxall
Tera Guru

 

I have created a BR to track our FCR in the incident table. The BR executes as expected on Default view, however when i create a incident record in the SOW it sets the wrong value. I can't figure it out why since all conditions are met. The field will correct update when the record goes from Resolved to closed, but from insert or update to Resolved it sets the value to "FCR not achieved" which should since the criteria are the same.

2025-04-08_22-56-18.png

Here its my script for the BR:

(function executeRule(current, previous) {

// Define state constants
var STATE_NEW = '1';
var STATE_IN_PROGRESS = '2';
var STATE_ON_HOLD = '3';
var STATE_RESOLVED = '6';
var STATE_CLOSED = '7';
var STATE_CANCELED = '8';

var state = current.getValue('state');

// Do not update FCR if state is New, In Progress, or Canceled
if (state == STATE_NEW || state == STATE_IN_PROGRESS || state == STATE_CANCELED) {
return;
}

var fcrValue = 'FCR Not Achieved';

var openedBy = current.getValue('opened_by');
var resolvedBy = current.getValue('resolved_by');
var assignedTo = current.getValue('assigned_to');
var reassignmentCount = parseInt(current.getValue('reassignment_count') || '0', 10);
var reopenCount = parseInt(current.getValue('reopen_count') || '0', 10);

var isFinalState = (state == STATE_RESOLVED || state == STATE_CLOSED);

// Check for On Hold history
var wasOnHold = false;
var auditGR = new GlideRecord('sys_audit');
auditGR.addQuery('documentkey', current.sys_id);
auditGR.addQuery('fieldname', 'state');
auditGR.addQuery('newvalue', STATE_ON_HOLD);
auditGR.setLimit(100);
auditGR.query();
if (auditGR.hasNext()) {
wasOnHold = true;
}

var isAssignedExternally = assignedTo && assignedTo != openedBy && assignedTo != resolvedBy;

if (
isFinalState &&
!wasOnHold &&
openedBy &&
resolvedBy &&
openedBy == resolvedBy &&
reassignmentCount === 0 &&
reopenCount === 0 &&
!isAssignedExternally
) {
fcrValue = 'FCR Achieved';
}

current.u_first_contact_resolution = fcrValue;

})(current, previous);

 

1 ACCEPTED SOLUTION

Vasantharajan N
Giga Sage
Giga Sage

@mcroxall - Please set the order for your business rule to 999 and check.

 

When the BR order is 100 then it is not setting the resolved by value when this BR is triggered from SOW.  Please check. 


Thanks & Regards,
Vasanth

View solution in original post

11 REPLIES 11

Sandeep Rajput
Tera Patron
Tera Patron

@mcroxall You have an if check at the bottom of your script.

 

if (
isFinalState &&
!wasOnHold &&
openedBy &&
resolvedBy &&
openedBy == resolvedBy &&
reassignmentCount === 0 &&
reopenCount === 0 &&
!isAssignedExternally
) {

 

The fcrValue = 'FCR Achieved'; is only set once the above if check evaluates to true. 

 

In the above condition var isFinalState = (state == STATE_RESOLVED || state == STATE_CLOSED); is only set to true when the state is either Resolved or Closed. This will simply hold a false value for other states, due to this your if check is failing and the value is set to FCR not achieved.

 

Hope this helps.

but why does it work when i create a record using default view and does not work when creating a record in the SOW?

I'll try your your suggestions! thank you

Ankur Bawiskar
Tera Patron
Tera Patron

@mcroxall 

can you try this and debug as logs are added?

(function executeRule(current, previous) {
    // Define state constants
    var STATE_NEW = '1';
    var STATE_IN_PROGRESS = '2';
    var STATE_ON_HOLD = '3';
    var STATE_RESOLVED = '6';
    var STATE_CLOSED = '7';
    var STATE_CANCELED = '8';

    var state = current.getValue('state');
    gs.log('Current state: ' + state);

    // Do not update FCR if state is New, In Progress, or Canceled
    if (state == STATE_NEW || state == STATE_IN_PROGRESS || state == STATE_CANCELED) {
        gs.log('State is New, In Progress, or Canceled. Exiting.');
        return;
    }

    var fcrValue = 'FCR Not Achieved';

    var openedBy = current.getValue('opened_by');
    var resolvedBy = current.getValue('resolved_by');
    var assignedTo = current.getValue('assigned_to');
    var reassignmentCount = parseInt(current.getValue('reassignment_count') || '0', 10);
    var reopenCount = parseInt(current.getValue('reopen_count') || '0', 10);

    gs.log('Opened by: ' + openedBy);
    gs.log('Resolved by: ' + resolvedBy);
    gs.log('Assigned to: ' + assignedTo);
    gs.log('Reassignment count: ' + reassignmentCount);
    gs.log('Reopen count: ' + reopenCount);

    var isFinalState = (state == STATE_RESOLVED || state == STATE_CLOSED);
    gs.log('Is final state: ' + isFinalState);

    // Check for On Hold history
    var wasOnHold = false;
    var auditGR = new GlideRecord('sys_audit');
    auditGR.addQuery('documentkey', current.sys_id);
    auditGR.addQuery('fieldname', 'state');
    auditGR.addQuery('newvalue', STATE_ON_HOLD);
    auditGR.setLimit(100);
    auditGR.query();
    if (auditGR.hasNext()) {
        wasOnHold = true;
    }
    gs.log('Was on hold: ' + wasOnHold);

    var isAssignedExternally = assignedTo && assignedTo != openedBy && assignedTo != resolvedBy;
    gs.log('Is assigned externally: ' + isAssignedExternally);

    if (
        isFinalState &&
        !wasOnHold &&
        openedBy &&
        resolvedBy &&
        openedBy == resolvedBy &&
        reassignmentCount === 0 &&
        reopenCount === 0 &&
        !isAssignedExternally
    ) {
        fcrValue = 'FCR Achieved';
    }

    gs.log('FCR Value: ' + fcrValue);
    current.u_first_contact_resolution = fcrValue;

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@mcroxall 

Did you see by using above script and the logs?

Did you check BR itself is triggering or not?

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