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.

Optimize the below Business rule

Pooja Khatri
Tera Contributor

Hi All ,

 

I want to optimize the below Business rule , I have two business rules which use the below highlighted code : 

 

var user = new GlideRecord('sys_user');
        user.get(gs.getUserID());
        if (user.web_service_access_only == true)

 

I want to create a script include for this part of code and then call the function in the condition of the business rule.

2 REPLIES 2

SN_Learn
Kilo Patron
Kilo Patron

Hi @Pooja Khatri ,

 

Please try the below:

 

Script Include

var UserUtils = Class.create();
UserUtils.prototype = {
    initialize: function() {
    },

    isWebServiceUser: function(userId) {
        var user = new GlideRecord('sys_user');
        if (user.get(userId)) {
            return user.web_service_access_only == true;
        }
        return false;
    },

    type: 'UserUtils'
};

 

Business rule:

(function executeRule(current, previous /null when async/) {
    var userUtils = new UserUtils();
    var isWebServiceUser = userUtils.isWebServiceUser(gs.getUserID());

    if (gs.nil(current.work_notes)) {
        if (!gs.getSession().isInteractive() && isWebServiceUser) {
            gs.addErrorMessage('Work notes are mandatory');
            current.setAbortAction(true);
            return;
        }
    }

    if (current.impact == 1 && current.urgency == 1) {
        if (!gs.getSession().isInteractive() && isWebServiceUser) {
            gs.addErrorMessage("Cannot be High simultaneously.");
            current.setAbortAction(true);
            return;
        }
    }
})(current, previous);

 

Please consider marking my reply as Helpful and/or Accept Solution, if it helps. Thanks!

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Anusha Reddy VK
Mega Guru

Hi @Pooja Khatri ,

 

Try below:

 

Script Include:

var UserUtils = Class.create();
UserUtils.prototype = {
    initialize: function() {},
    
    isWebServiceAccessOnly: function(userId) {
        var user = new GlideRecord('sys_user');
        if (user.get(userId)) {
            return user.web_service_access_only == true;
        }
        return false;
    },
    
    type: 'UserUtils'
};

 

In Business rule, Replace the code provided by you with below code:

 if (new UserUtils().isWebServiceAccessOnly(gs.getUserID())) {
        // Your existing business rule logic
    }

 

If you find my response helpful, please consider selecting "Accept as Solution" and "Helpful" .

Thanks,

Anusha