How to display knowledge base hierarchy

Recently I was asked by Knowledgebase Manager why she was not able to remove an unnecessary category in ServiceNow. The answer was simple: there is an ACL which checks if a category has a child, either article or other category. We found a retired article which was still linked to this category. Because of that, it was impossible to delete this category.

Continue reading How to display knowledge base hierarchy

Error: “sc_req_item_stageGetChoices” is not defined.

A few days ago I found a really weird bug while I was writing a small application in ServiceNow. I noticed following warning in logs:

org.mozilla.javascript.EcmaError: "sc_req_item_stageGetChoices" is not defined.
Caused by error in sys_script_include.0236e5eddbd22300b5919fd2ca9619c2.script at line 223

Continue reading Error: “sc_req_item_stageGetChoices” is not defined.

ATF – Database View to summarize information about Test Suits, Tests and Test Steps

Recently I was asked for providing report with summary information about configured tests.
I’ve created a Database View to have it ready for the future requests (I’m expecting that sooner or later I’ll be asked again for the same).
The scheme is very simple:

The update set with this Database View:
Download

How to start with TDD in ServiceNow

This time I would like to present how to create very first unit test and adapt TDD for creating scripts in ServiceNow.
I’m going to create two very simple script includes: Course and Student.
The course will be implemented three methods:
– addStudent
– getStudent
– removeStudent

Continue reading How to start with TDD in ServiceNow

How to install MidServer on Linux hosted on VirtualBox

In this article I’m going to show how you can install MidServer on linux (CentOS7) using for that VirtualBox.
I’ll skip the VirtualBox installation. During the Centos installation you can choose default values if it’s not mentioned differently.

Continue reading How to install MidServer on Linux hosted on VirtualBox

How to get a list of all roles assigned directly and indirectly (inherited) to group

List of roles assigned to group

Some time ago I needed to retrieve complete list of roles assigned to group from ServiceNow. I wanted to know which roles were granted by which group. I quickly found out that there is no easy solution for roles that are inherited from other roles. I prepared script (can be execute in Background Script or in Fix Script) which helped me to retrieve this list:

function checkIfGroupHasRoles(groupRecord){
    var grGHR = new GlideRecord('sys_group_has_role');
    grGHR.addQuery('group', groupRecord.sys_id);
    grGHR.query();
    
    return grGHR.hasNext();
}

function checkIfRoleContainsRoles (checkingRole){
    var grRC = new GlideRecord('sys_user_role_contains');
    grRC.addQuery('role.name',checkingRole);
    grRC.query();
    
    return grRC.hasNext();
}

function getArrayOfChildRoles(parentRole){
    var grRC = new GlideRecord('sys_user_role_contains');
    grRC.addQuery('role.name',parentRole);
    grRC.query();
    
    var resultArray = [];
    
    while (grRC.next()){
        var childRole = grRC.getDisplayValue('contains');
        resultArray.push(childRole.toString());

        if( checkIfRoleContainsRoles(childRole) ){
            var arrayOfChildRoles = getArrayOfChildRoles(childRole);
            resultArray = resultArray.concat(arrayOfChildRoles);
        }
    }
    return resultArray;
}

function getArrayOfAllRolesForGroup(groupRecord){
    var resultArray = [];
    
    var grGHR = new GlideRecord('sys_group_has_role');
    grGHR.addQuery('group', groupRecord.sys_id);
    grGHR.query();
    
    while(grGHR.next()){
        var role = grGHR.role.name;
        resultArray.push(role.toString());
        
        if ( checkIfRoleContainsRoles(role) ){
            var arrayOfChildRoles=[];
            arrayOfChildRoles = getArrayOfChildRoles(role);
            resultArray = resultArray.concat(arrayOfChildRoles);
        }
    }
    return resultArray;
}

function printRolesForGroups(){
    var grGR = new GlideRecord('sys_user_group');
    grGR.query();
    
    while (grGR.next()){
        if ( checkIfGroupHasRoles( grGR ) ){
            var arrayOfAllRolesForGroup = [];
            arrayOfAllRolesForGroup = getArrayOfAllRolesForGroup(grGR);
            
            var au =  new ArrayUtil();
            arrayOfAllRolesForGroup = au.unique(arrayOfAllRolesForGroup);
            
            var message  = grGR.getDisplayValue() + ': ' + arrayOfAllRolesForGroup.join(',');
            gs.print(message);
            }
        }
}
printRolesForGroups();

Testing ServiceNow Rest Api using CURL

Sometimes you can find very helpful to leave rest api explorer in ServiceNow and use some external tools like Postman or Curl to send some request to your instance. In this post I’m going to present some functionalities of curl.
If your OS is Windows you have to download curl from: https://curl.haxx.se/
Or you can use Git Bash: https://git-scm.com/download/win

Continue reading Testing ServiceNow Rest Api using CURL