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

@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

@ankitaankit 

Ideally this is OOB business rule so you should also check with ServiceNow why they have created it if it's not best practice.

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