show incidents only for few groups in list view !

Barathk
Tera Contributor

I have created BR in task table to show the incidents for the specific groups.
the groups are added in sys property.
the script is not working can some one help to resolve quickly.


(function executeRule(current, previous /*null when async*/) {
 
var userGroups = gs.getUser().getMyGroups();
var grp1 = gs.getProperty('Lock_Incidents').split(',');
var grp2 = gs.getProperty('Collection_Incidents').split(',');
var lock = false;
var collection = false;
for (var i = 0; i < grp1.length; i++) {
if (userGroups.indexOf(grp1[i]) !== -1) {
lock = true;
break;
}
}
for (var j = 0; j < grp2.length; j++) {
if (userGroups.indexOf(grp2[j]) !== -1) {
collection = true;
break;
}
}
if (lock == true) {
current.addEncodedQuery('short_descriptionNOT LIKE "Lock"');
}
if (collection == true) {
current.addEncodedQuery('short_descriptionNOT LIKE "Collection"');
current.addEncodedQuery('short_descriptionNOT LIKE "collection"');
}
})(current, previous);
5 REPLIES 5

Priti_S
Tera Expert

Hi @Barathk 
You can try the below script

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

   
    var userGroupIds = [];
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('user', gs.getUserID());
    gr.query();
    while (gr.next()) {
        userGroupIds.push(gr.getValue('group'));
    }


    var grp1 = gs.getProperty('Lock_Incidents', '').split(',');
    var grp2 = gs.getProperty('Collection_Incidents', '').split(',');

    var lock = false;
    var collection = false;

    for (var i = 0; i < grp1.length; i++) {
        if (userGroupIds.indexOf(grp1[i].trim()) !== -1) {
            lock = true;
            break;
        }
    }

    for (var j = 0; j < grp2.length; j++) {
        if (userGroupIds.indexOf(grp2[j].trim()) !== -1) {
            collection = true;
            break;
        }
    }


    if (lock === true) {
        current.addEncodedQuery("short_descriptionNOT LIKELock");
    }

    if (collection === true) {
        current.addEncodedQuery("short_descriptionNOT LIKECollection");
    }

})(current, previous);

Thank you.

If my response was helpful, please mark it as correct and helpful.

Ankur Bawiskar
Tera Patron
Tera Patron

@Barathk 

why did you create business rule on task table?

There is already an out of the box "incident query" business rule on incident table

You can enhance that

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

SakthivelS
Giga Contributor

Hi @Barathk ,

Please find the below solution for your query

 

(function executeRule(current, previous /*null when async*/) {
 
var userGroups = gs.getUser().getMyGroups();
var grp1 = gs.getProperty('Lock_Incidents').split(',');
var grp2 = gs.getProperty('Collection_Incidents').split(',');
var lock = false;
var collection = false;
for (var i = 0; i < grp1.length; i++) {
if (userGroups.indexOf(grp1[i]) !== -1) {
lock = true;
break;
}
}
for (var j = 0; j < grp2.length; j++) {
if (userGroups.indexOf(grp2[j]) !== -1) {
collection = true;
break;
}
}
if (lock == true) {
current.addEncodedQuery('short_description!=Lock');
}
if (collection == true) {
current.addEncodedQuery('short_description!=Collection');
current.addEncodedQuery('short_description!=collection');
}
})(current, previous);
 
If this solution works please mark as helpful.

SakthivelS
Giga Contributor

Hi @Barathk ,

 

Please find the solution for you query,

 

(function executeRule(current, previous /*null when async*/) {
 
var userGroups = gs.getUser().getMyGroups();
var grp1 = gs.getProperty('Lock_Incidents').split(',');
var grp2 = gs.getProperty('Collection_Incidents').split(',');
var lock = false;
var collection = false;
for (var i = 0; i < grp1.length; i++) {
if (userGroups.indexOf(grp1[i]) !== -1) {
lock = true;
break;
}
}
for (var j = 0; j < grp2.length; j++) {
if (userGroups.indexOf(grp2[j]) !== -1) {
collection = true;
break;
}
}
if (lock == true) {
current.addEncodedQuery('active=true');//Shows if the incident is active
}
if (collection == true) {
current.addEncodedQuery('active=true');//Shows if the incident is active
}
})(current, previous);
 
If this solution works please mark as helpful.