Script include to check the validation of groups table and scheduled job to run daily

Rajamouly
Tera Expert

HI All,

My requirement is to validate the groups table, either the group is active or inactive and Should have at least 2 group members.

if any condition is not matching we will trigger the notifications to group manager.

We need to write script include which should call in scheduled job and run daily basis. Please assist me with script include and scheduled job scripts.(As i am new to development work)

 

 

1 ACCEPTED SOLUTION

Gabor10
Mega Guru

Hey there!

Nice to see new people joining the wonderful world of ServiceNow development.

For this specific case, you don't need a script include, but you could also copy over some of my provided code into a script include if that's your thing.

You'll need the following:

  • A Scheduled Job that will check the Groups and the Memberships and schedule an Event if necessary.
  • An Event in the Event Registry. We'll use this Event to trigger the notification.
  • A notification. This will define what content you'll send out to who in your outbound email. 

 

I've uploaded the Update Set I've used while creating the components for this answer, you may import it to your PDI to see what's inside.

 

Else, here's the my script for the Scheduled Job:

var goodGroups = getGroupsWith2orMoreMembers();

if (goodGroups.length > 0) {
    triggerNotificationForBadGroups(goodGroups);
}

function getGroupsWith2orMoreMembers() {
    var gr = new GlideAggregate('sys_user_grmember');
    var arr = [];
    gr.addAggregate('COUNT', 'user');
    gr.groupBy('group');
    gr.addHaving('COUNT', '>=', '2'); //some inverse logic to fetch every group that has 2 or more members
    gr.query();
    while (gr.next()) {
        arr.push(gr.getValue('group'));
    }
    return arr;
}

function triggerNotificationForBadGroups(goodGroups) {

    var gr = new GlideRecord('sys_user_group');
    gr.addQuery('sys_id', 'NOT IN', goodGroups); //query for groups that are not goodGroups -> they are bad groups
    gr.setLimit(goodGroups.length);
    gr.query();

    while (gr.next()) {
        gs.eventQueue('badGroupNotificationEvent', gr, gr.getValue('manager')); //in the first parameter use the event name you've ended up creating
    }
}

View solution in original post

8 REPLIES 8

Gabor10
Mega Guru

Hey there!

Nice to see new people joining the wonderful world of ServiceNow development.

For this specific case, you don't need a script include, but you could also copy over some of my provided code into a script include if that's your thing.

You'll need the following:

  • A Scheduled Job that will check the Groups and the Memberships and schedule an Event if necessary.
  • An Event in the Event Registry. We'll use this Event to trigger the notification.
  • A notification. This will define what content you'll send out to who in your outbound email. 

 

I've uploaded the Update Set I've used while creating the components for this answer, you may import it to your PDI to see what's inside.

 

Else, here's the my script for the Scheduled Job:

var goodGroups = getGroupsWith2orMoreMembers();

if (goodGroups.length > 0) {
    triggerNotificationForBadGroups(goodGroups);
}

function getGroupsWith2orMoreMembers() {
    var gr = new GlideAggregate('sys_user_grmember');
    var arr = [];
    gr.addAggregate('COUNT', 'user');
    gr.groupBy('group');
    gr.addHaving('COUNT', '>=', '2'); //some inverse logic to fetch every group that has 2 or more members
    gr.query();
    while (gr.next()) {
        arr.push(gr.getValue('group'));
    }
    return arr;
}

function triggerNotificationForBadGroups(goodGroups) {

    var gr = new GlideRecord('sys_user_group');
    gr.addQuery('sys_id', 'NOT IN', goodGroups); //query for groups that are not goodGroups -> they are bad groups
    gr.setLimit(goodGroups.length);
    gr.query();

    while (gr.next()) {
        gs.eventQueue('badGroupNotificationEvent', gr, gr.getValue('manager')); //in the first parameter use the event name you've ended up creating
    }
}

Rajamouly
Tera Expert

Thanks Gabor for quick reply, will check your code and updateset 

Edit: Joke about marking answer as correct which clearly did not land well

Sorry Gabor, i am new to community forum so not knowing much about it

Your code has helped me a lot, It was really helpful.

Thanks so much for your help its really appreciated