I have a Business Rule on Group Roles (sys_group_has_role) to Set Group Type on Group form(Sys_user_group) as ITIL License when the Group gets added by itil role. Now my new requirement is to set Fulfiller Group as Type on Group form when a any role

divyavani1
Giga Contributor

I have a After Business Rule on Group Roles (sys_group_has_role) to Set Group Type on Group form(Sys_user_group) as ITIL License when the Group gets added by itil role. Now my new requirement is to set Fulfiller Group as Type on Group form when a any role of this roleset(asset,catalog_item_designer,Change Management,change_coordinator,change_manager). 

Now i am trying to create another similar After BR to set Type as Fulfiller Group on group form but it is not working as expected.

Your suggestion helps me alot

Below is the existing BR to set ITIL License.

(function executeRule(current, previous /*null when async*/) {

// Add your code here
var licenseType = [],
allTypes = [],
count_of_itil_group,
finalValue,
flag = 0;
var itil_license = gs.getProperty('itil_license_type'); //Property value of Type = ITIL License

var grpID = current.group;
var grGroup = new GlideAggregate('sys_group_has_role');
grGroup.addQuery('group', grpID);
grGroup.addQuery('role', gs.getProperty('itil_role_access')); //Property value of ITIL role
grGroup.addAggregate("COUNT");
grGroup.query();
while(grGroup.next()){
count_of_itil_group = grGroup.getAggregate("COUNT");
}


if(count_of_itil_group > 0){
var grGrpType = new GlideRecord('sys_user_group');
grGrpType.addQuery('sys_id', grpID);
grGrpType.query();
if(grGrpType.next()){
if(grGrpType.type == ''){ //When Type is empty
licenseType.push(itil_license);
finalValue = licenseType;
}
else{ //When Type contains certain existing values
allTypes = grGrpType.type;
var splitTypes = allTypes.split(',');

for(var i = 0; i < splitTypes.length; i++){
if(splitTypes[i] == itil_license){ //checking if the Type already has ITIL License value
flag++;
}
else{
flag = 0;
}
}

if(flag == 0){ //If Type already doesn't have ITIL License value
licenseType.push(itil_license);
finalValue = splitTypes.concat(licenseType);
}
else{ //If Type already has ITIL License value
finalValue = splitTypes;
}
}

grGrpType.type = finalValue.toString();
grGrpType.update();
}
}

})(current, previous);

----------------------------------------------------------------------------------------

BR to set Fulfiller Group as Type

(function executeRule(current, previous /*null when async*/) {

// Add your code here
var licenseType = [],
allTypes = [],
count_of_fullfiller_group,
finalValue,
flag = 0;
var fulfiller_license = gs.getProperty('fulfiller_license_type'); //Property value of Type = Fulfiller Group

var grpID = current.group;
var grGroup = new GlideAggregate('sys_group_has_role');
grGroup.addQuery('group', grpID);
grGroup.addEncodedQuery('role.nameIN asset,catalog_item_designer,Change Management,change_coordinator,change_manager');
grGroup.addAggregate("COUNT");
grGroup.query();
while(grGroup.next()){
count_of_fullfiller_group = grGroup.getAggregate("COUNT");
}

gs.log("count_of_fulfiller_group==new=="+count_of_fulfiller_group);

if(count_of_fullfiller_group > 0){
var grGrpType = new GlideRecord('sys_user_group');
grGrpType.addQuery('sys_id', grpID);
grGrpType.query();
if(grGrpType.next()){
if(grGrpType.type == ''){ //When Type is empty
licenseType.push(fulfiller_license);
finalValue = licenseType;
}
else{ //When Type contains certain existing values
allTypes = grGrpType.type;
var splitTypes = allTypes.split(',');
gs.log("has value " +splitTypes);

for(var i = 0; i < splitTypes.length; i++){
if(splitTypes[i] == fulfiller_license){ //checking if the Type already has Fulfiller Group value
flag++;
}
else{
flag = 0;
}
}
gs.log("flag==new=="+flag);
if(flag == 0){ //If Type already doesn't have Fulfiller Group value
licenseType.push(fulfiller_license);
finalValue = splitTypes.concat(licenseType);
}
else{ //If Type already has Fulfiller Group value
finalValue = splitTypes;
}
}
gs.log("finalValue.toString()==new=="+finalValue);
grGrpType.type = finalValue.toString();
grGrpType.update();
}
}

})(current, previous);

1 ACCEPTED SOLUTION

Hi, please update the code in the business rule to this. And please mark my comments helpful/resolved if they indeed were.

(function executeRule(current, previous /*null when async*/ ) {

    var fulfillerRoles = gs.getProperty("test.role.names");
    var groupTypes = gs.getProperty("test.group.types").split(",");
    var groupId = current.getValue("group");
    var roleName = current.getDisplayValue("role");

    //The IF blocks are like this because I understand that a group can have both ITIL Licence and Fulfiller Group types
    //If groups can only have 1 of these types then just add an else if instead of the bottom IF

    if (fulfillerRoles.indexOf(roleName) > -1) {

        addGroupType(groupId, groupTypes[1]);

    }

    if (roleName == "itil") {

        addGroupType(groupId, groupTypes[0]);

    }


    function addGroupType(group, grType) {
        try {

            var flag = isTypeEmpty(group);

            var gr = new GlideRecord("sys_user_group");
            gr.get(group);

            var existingType = gr.getValue("type");

            if (flag) {
				gr.setValue("type", grType);
            } else if (existingType.indexOf(grType) > -1) { //If there is already a type defined on this record
                //do nothing
            } else {
                gr.setValue("type", existingType + "," + grType);
            }
            gr.update();


        } catch (e) {
            gs.log(e, "[TEST]");
        }
    }

    function isTypeEmpty(group) {

        var gr = new GlideRecord("sys_user_group");
        gr.get(group);

        if (gr.getValue("type") == null) {
            return true;
        } else {
            return false;
        }

    }

})(current, previous);

View solution in original post

7 REPLIES 7

harun_isakovic
Mega Guru

-Create a system property with names of roles you want to be categorized as "Fulfiller" type.

find_real_file.png

 

-Create another system property with sys_Ids of group types(make sure to keep the order in mind)

find_real_file.png

 

-Create an After Insert business rule on "sys_group_has_role" table. Tick the advanced button and paste this in the script, it will do what you need.

NOTE: This business rule will be responsible for adding the ITIL LIcence type and Fulfiller group type, so no need for a 2nd BR.

(function executeRule(current, previous /*null when async*/ ) {

    var fulfillerRoles = gs.getProperty("test.role.names");
    var groupTypes = gs.getProperty("test.group.types").split(",");
    var groupId = current.getValue("group");
    var roleName = current.getDisplayValue("role");

    //The IF blocks are like this because I understand that a group can have both ITIL Licence and Fulfiller Group types
    //If groups can only have 1 of these types then just add an else if instead of the bottom IF

    if (fulfillerRoles.indexOf(roleName) > -1) {

        addGroupType(groupId, groupTypes[1]);

    }

    if (roleName == "itil") {

        addGroupType(groupId, groupTypes[0]);

    }


    function addGroupType(group, grType) {

        var gr = new GlideRecord("sys_user_group");
        gr.get(group);

		var existingType = gr.getValue("type");
        if (existingType == null) {

            gr.setValue("type", grType);

        } else { //If there is already a type defined on this record

            gr.setValue("type", existingType + "," + grType);

        }

        gr.update();

    }


})(current, previous);

 

Please mark correct/helpful if it indeed was. Thanks!

Hi Harun,

Thanks for your help. It is working fine for insert.

I would also require to remove Type, if that particular role is been deleted for that group.

Say for example, If ITIL role is removed from the group, it should also remove ITIL License from Type field.

 

Please help me out.

 

Thanks,

Divya

For this you can create an After Business rule that is triggered on Delete (there is a tickbox on the BR Form after ticking the Advanced box).

After that you can take a bit of an inspiration from the provided codes on how to remove the mentioned tag.

Hi Harun,

I tried your code, i could see If i add asset and change_manager roles it is adding Fulfiller Group twice in Type field.It should add only once. on removing fulfiller role to the group it should also remove Fulfiller Group from Type field.

Please suggest.