Issue in getting all users manager details to trigger a notification

Aditya253
Kilo Guru

Hi All,

 

Actually, I need to send an email notification to the 'assign to' user managers on incident form if the incident state is in progress at every Monday. For this, I have used run script scheduled job in the script I have mentioned the below code. Please help me on this.

 

In the array I am getting repeated email ID's of managers need to remove that, one email is enough to one particular manager. But by using the below code manager receiving emails repeatedly from all incidents. Please help me on this.

 

Scheduled script:

 

var emailList = [];

var incGr = new GlideRecord('incident');

incGr.addQuery('state','3');

incGr.query();

while(incGr.next()){

var userGr = new GlideRecord ('sys_user');

userGr.addQuery('sys_id', incGr.assign_to);

userGr.query();

if(userGr.next()){

var managerEmail = userGr.manager.email;

emailList.push(managerEmail);

gs.event('user_manager_emails', incGr, emailList);

}

}

 

1 ACCEPTED SOLUTION

Thanks you Khushboo

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

Hello @Aditya253 ,

 

Please add this "if" condition after the declaration of  variable managerEmail:

if (emailList.toString().indexOf(managerEmail) == -1) {
emailList.push(managerEmail);
}

 

@Aditya253Please mark my answer as "Accept as Solution" and "Helpfuls." If it works for you.
Thank You!

Thanks you Khushboo

Martin Ivanov
Giga Sage
Giga Sage

Hello. There are multiple things to be corrected in your script, starting from the fact that InProgress state OOTB has a value of 2, not 3.

Take a look at the script below.

NB. I do not know how your email notification is set up. I assume that you want to send One notification with multiple recipients (the ones from the 'emailList' array). If this is not the case and you want to fire a separate event for any incident, then you need to rethink the whole thing.

 

 

var emailList = [];
var arrayUtil = new ArrayUtil();

var incGr = new GlideRecord('incident');
    incGr.addNotNullQuery('assigned_to');
    incGr.addQuery('state','2');
    incGr.query();

while(incGr.next()){
    
    var managerGr = incGr.assigned_to.manager;
    if(managerGr && managerGr.email){
        
        if(!arrayUtil.contains(emailList, managerGr.email)){
            emailList.push(managerGr.email);
        }   
    }
}

gs.event('user_manager_emails', incGr, emailList);

 

 


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

@Aditya253 if my guidance has helped you resolve your issue, consider makring it Correct. Thanks!

 


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024