Coverting below OOB Global BR to Script Include

ankitaankit
Tera Contributor

Hello all,

I have a global BR, which is OOB by ServiceNow name: incident functions, with not conditions band below is the code.

// populate the default value for the caller
function incidentGetViewName() {
  if (gs.hasRole("itil") || gs.hasRole("sn_incident_read"))
     return;
  if (view.startsWith("ess"))
     return;
  if (view == "sys_ref_list")
     return;

  answer = "ess";
}

function incident_listGetViewName() {
  incidentGetViewName();
}

// for ESS type users, use the logged in user
// for itil users, if the parent had a user, use that person
function incidentGetCaller() {
    if (gs.hasRole("itil")) {
        var action = gs.action;
        var table = action.get("sysparm_collection");
        if (table != "sys_user")
            return null;

        var userID = action.get("sysparm_collectionID");
        return userID;
    }

    return gs.getUserID();
}

// get an email address from the sys_user file
function sys_userGetEmailAddress(sys_id) {
    // danger, at this point, the current record switches to sys_user
    var prev = current;
    var emailAddress = null;
    var user = new GlideRecord("sys_user");
    user.addQuery("sys_id", sys_id);
    user.query();
    if (user.next()) {
                                if (user.notification == 2)
           emailAddress = user.email;
    }

    current = prev;
    return emailAddress;
}
 
I need to understand it basically what it is doing, and then nee dot convert it to Script Include, so that it does not impact the system performance.
Please help with the same.
 
Thanks!!

1 ACCEPTED SOLUTION

@ankitaankit 

you can create script include and necessary functions

But you need to determine from where all the functions from this global business are called

Something like this you can create script include and then invoke it like this

new IncidentFunctions().incidentGetViewName();

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

    incidentGetViewName: function() {
        if (gs.hasRole("itil") || gs.hasRole("sn_incident_read"))
            return;
        if (view.startsWith("ess"))
            return;
        if (view == "sys_ref_list")
            return;

        answer = "ess";
    },

    incidentGetCaller: function() {
        if (gs.hasRole("itil")) {
            var action = gs.action;
            var table = action.get("sysparm_collection");
            if (table != "sys_user")
                return null;

            var userID = action.get("sysparm_collectionID");
            return userID;
        }
        return gs.getUserID();
    },

    type: 'IncidentFunctions'
};

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

View solution in original post

6 REPLIES 6

Weird
Mega Sage

What exactly are you trying to achieve?

Basically a lot of servicenow scripts are very old and are used in certain ways that differ from what one might think logical, but they're very helpful.

For these BR functions they're basically available globally. For example you might find one of those functions used in some dynamic filter for example.
Inactivating the BR might break a lot of not so obvious stuff, so I wouldn't bother doing that.

monalipatil
Tera Contributor

Hi,

 

It seems BR gives some view access based on roles, notification functionality also configured in the script which may called from other scripts. Its always a good practice to connect with ServiceNow support before disabling OOTB functionality. And they can help to understand purpose of this BR

You can also check from where this BR is called from studio

 

Thank you

Ankur Bawiskar
Tera Patron
Tera Patron

@ankitaankit 

that is an old global business rule created in 2019.

Why to create script include?

what's your business requirement?

You can easily understand what each function does within it.

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

Hello @Ankur Bawiskar

As it's impacting the health scan results, so we are trying to implement the same via doing server calls that is Script Include as best practice suggested by ServiceNow.

Regards

Ankita