How to skip sending notification to snc_internal users.

raj149
Giga Guru

Hello Experts,

 

In my instance user table is having 100 user records.  50 users having snc_internal role and ITIL and ++ roles and remaining 50 members having only snc_internal role.

Now my requirement is :

I don't want to send a remainder notification to users who is having only snc_internal role.

I am trying by using below code but still it is sending notification to only snc_internal users.

 

var gr = new GlideRecord("sys_user");
 
gr.addEncodedQuery('last_loginRELATIVEGT@dayofweek@ago@77^last_loginRELATIVELT@dayofweek@ago@75^active=true');
gr.addEncodedQuery('u_type_of_userNOT INGroup,Service');
 
 
gr.query();
 
while (gr.next()) {
 
    var hasSncInternalRole = false;
    var userRoles = gr.getRoles();
 
    for (var i = 0; i < userRoles.length(); i++) {
        var role = userRoles.get(i);
        if (role == 'snc_internal' && userRoles.length() === 1) {
            hasSncInternalRole = true;
            break;
        }
    }
 
    if (!hasSncInternalRole) {
       
        gs.eventQueue('User_inactive_76_days_reminder', gr, gr.email, gr.user_name);
 
    }
}
 
Please correct the above script that will helpful for me.
 
Best Regards,
Raj

 

1 REPLY 1

Iraj Shaikh
Mega Sage
Mega Sage

Hi @raj149 

It seems like you're trying to send a reminder notification to users who have not logged in for a specific period and have roles other than just `snc_internal`. Your script is on the right track, but it might not be filtering out users correctly. Let's refine the script to ensure it only sends notifications to users with additional roles.

Here's a corrected version of your script:

 

var gr = new GlideRecord("sys_user");

gr.addEncodedQuery('last_loginRELATIVEGT@dayofweek@ago@77^last_loginRELATIVELT@dayofweek@ago@75^active=true');
gr.query();

while (gr.next()) {
    // Check if the user has only the snc_internal role
    var roles = gr.getRoles();
    if (roles.length == 1 && roles.indexOf('snc_internal') != -1) {
        // Skip this user as they only have the snc_internal role
        continue;
    }
    
    // Send the reminder notification to the user
    gs.eventQueue('User_inactive_76_days_reminder', gr, gr.email, gr.user_name);
}

 


Here's what I changed:

1. I removed the second `addEncodedQuery` line because it was filtering out users based on a custom field `u_type_of_user` which you did not mention in your requirement. If you need to filter by user type, you should adjust this query accordingly.

2. I simplified the check for the `snc_internal` role. Now, it checks if the user has exactly one role, and if that role is `snc_internal`, it skips sending the notification to that user.

3. I used `continue` to skip to the next record in the loop if the user only has the `snc_internal` role.

 

Make sure that the event `User_inactive_76_days_reminder` is correctly set up to send the notification and that the `last_login` field is being used and updated correctly in your instance. Also, ensure that the `active` field is set to `true` for the users you want to target.

 

Please mark this response as correct or helpful if it assisted you with your question.