- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2022 03:16 AM
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);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2022 10:07 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2022 10:07 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2022 04:33 AM
Hi,
Thanks for your response.
Your given code for After BR for insert is working fine .i have also checked Update checkbox in BR as after including After BR on Delete with below code it is removing the Type as per role removal. but if group has 2 roles among Fulfiller say asset and change_coordinator and if i remove only one role(asset) ,the type is getting removed with Fulfiller Group but it should still have Fulfiller group in Type as it has change_coordinator role. that is the reason i have checked update box for BR on insert but still it is failing in above scenario.
I have created below BR on delete to remove type if group has got deleted with that role.
Your help really matters me a lot
-----------------------------------------------------------------------------
(function executeRule(current, previous /*null when async*/) {
var allTypes = [];
var fulfillerRoles = gs.getProperty("fulfiller.role.names");
var groupTypes = gs.getProperty("test.group.types").split(",");
var groupId = current.getValue("group");
var roleName = current.getDisplayValue("role");
if (fulfillerRoles.indexOf(roleName) > -1) {
del(groupId, groupTypes[1]);
}
if (roleName == "itil") {
del(groupId, groupTypes[0]);
}
function del(group,grType){
grGrp = new GlideRecord('sys_user_group');
grGrp.get(group);
allTypes = grGrp.getValue('type');
var splitTypes = allTypes.split(',');
var indexValue = splitTypes.indexOf(grType);
if(indexValue > -1){
splitTypes.splice(indexValue, 1); // Removing the Type as ITIL License
grGrp.setValue('type',splitTypes.toString());
grGrp.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2022 09:36 AM
The Update checkbox is pointless for this business rule as the records created there are never updated. They are only inserted in case a role is added to a group or deleted in case of removing a role from a group.
In your delete BR I suggest you make a check before removing the Type to see if this specific group has multiple of the Fulfiller type of roles attached to it. If yes then just do nothing, if it only had that one role then remove the type.
Sorry I cannot review your script at the moment.
Would appreciate it if you marked my responses as helpful.