Why Doesn’t My “onBefore” Business Rule Prevent Record Modifications?

jordimsant
Kilo Sage

 

Hi everyone,

 

I’ve created a Business Rule in ServiceNow that is supposed to prevent users from modifying resource assignment records if they aren’t part of a validation hierarchy of the resource (I have a reference field u_validator on each user, that's the hierarchy I am referring). Here’s the Business Rule code I’m using:

 

(function executeRule(current, previous) {

    gs.log('JORDI: START BR');

    if (current.user_resource.nil()) {
        gs.log('JORDI: user_resource is empty');
        return;
    }

    var allocatingUserID = current.user_resource.toString();
    var allocatingUserName = current.user_resource.getDisplayValue();
    var sessionUserID = gs.getUserID();

    var validatedUsers = new TMNUtils().getValidatorHierarchyDown(sessionUserID);

    gs.log('JORDI: validatedUsers raw: ' + validatedUsers);
    gs.log('JORDI: type: ' + Object.prototype.toString.call(validatedUsers));

    if (!validatedUsers) {
        validatedUsers = [];
    }

    validatedUsers.push(sessionUserID);

    allocatingUserID = allocatingUserID.toString();
    validatedUsers = validatedUsers.map(function(u){ return u.toString(); });

    gs.log('JORDI: FINAL validatedUsers: ' + validatedUsers.join(','));

    if (validatedUsers.indexOf(allocatingUserID) === -1) {

        var msg = gs.getMessage(
            'You are not in the validation hierarchy of user {0}, you cannot modify allocations related with this user.',
            [allocatingUserName]
        );

        gs.addErrorMessage(msg);

        current.setAbortAction(true);
        current.setWorkflow(false);

        throw new Error(msg);
    }

})(current, previous);

 

The BR parameters I’ve set are:

  • When: onBefore

  • Insert: checked

  • Update: checked

  • Delete: checked

  • Application: global

  • Table: sn_plng_att_core_resource_assignment

Despite this, when I try to modify a record as a user who is not in the validation hierarchy, the modification still goes through despite the error message pops up and the resource allocations are not being modified (as seen in the image).

 

jordimsant_0-1770998335079.png

 

Note that "TEST 1" is the new resource (the one that should not be saved) and "Jordi Martínez" is the old one. I’ve tried current.setAbortAction(true) and even throwing an Error, but nothing seems to prevent the update. This are the logs that I am getting:

 

jordimsant_1-1770998819906.png

 

The version of the script that you can see in this article is the most debugged one, which means that I have already tried using only current.setAbortAction(true), only the throw, the combination of all of them... even I tried to create a business rule in the SPM Planning Attributes Core, which is the application in which sn_plng_att_core_resource_assignment belongs.

 

Am I missing something fundamental about how “onBefore” Business Rules work? Am I not considering something more complicated that just business rules and workflows?

 

I’d really appreciate any insights on this, I am honestly so lost.

 

Thanks in advance!

1 REPLY 1

Tanushree Maiti
Mega Sage

Can you try with this:

If it works, revert back to your previous error message. else share the output.

function executeRule(current, previous) {
 
    gs.log('JORDI: START BR');
 
    if (current.user_resource.nil()) {
        gs.log('JORDI: user_resource is empty');
        return;
    }
 
    var allocatingUserID = current.user_resource.toString();
    var allocatingUserName = current.user_resource.getDisplayValue();
    var sessionUserID = gs.getUserID();
 
    var validatedUsers = new TMNUtils().getValidatorHierarchyDown(sessionUserID);
 
    gs.log('JORDI: validatedUsers raw: ' + validatedUsers);
    gs.log('JORDI: type: ' + Object.prototype.toString.call(validatedUsers));
 
    if (!validatedUsers) {
        validatedUsers = [];
    }
 
    validatedUsers.push(sessionUserID);
 
    //allocatingUserID = allocatingUserID.toString();
    validatedUsers = validatedUsers.map(function(u){ return u.toString(); });
 
    gs.log('JORDI: FINAL validatedUsers: ' + validatedUsers.join(','));
 
    if (validatedUsers.indexOf(allocatingUserID) === -1) 
{
         gs.addErrorMessage('You are not in the validation hierarchy of user {0}, you cannot modify allocations related with this user.');
        current.setAbortAction(true);    
    }
 
})(current, previous);
Please mark this response as Helpful & accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: