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

Haha thats Okay, my above comment was only meant as a joke!

 

Glad it helped

HI Gabor,

 

The above requirement should be implemented on change form.

The requirement is slightly changed like 

We need to check that all approver groups are valid or not but the action to take when an invalid approver group is identified will be:

  • Display error message on the form so that the user can't proceed with the change
  • Send notification to the configuration manager of the CI stating which approver group is invalid

Please help with script

Hello Rajamouly,

I'd need a bit more specifics in order to properly help you. A screenshot and some help text visualizing your requirement would be useful.

 

Without any further specifics, I would probably advise to move the provided code from the Scheduled Job into a Business Rule that is running on Change Request table. You will have to add an addQuery() command to the first function in the provided code

Thanks Gabor for your continuous response, i got help from colleague hence responding you