Multiple Email Notification

Ni_a Sy
Tera Contributor

A notification that will remind users that haven’t login to their account for the last 4 months and a scheduled job that will trigger the notification created above every 15th of the month. Another 2nd notification that will inform the users that received the 1st notification. A Scheduled job that will trigger every 30th of the month with the following: 
Store the roles of the user (haven’t login to their account for the last 4 months) in custom table named Archived User Roles (u_ar_sys_user_has_role)  Trigger the notification mentioned and remove roles of the user/s.
What I did:

I  created event:remind.user.login
I created email Notification: Eval Notification- haven’t login
I created Schedule Job:

===

var gr = new GlideRecord('sys_user');
gr.addNotNullQuery('last_login_timeRELATIVELT@month@ahead@4');
gr.query();
var gdt = new GlideDateTime();
gdt.addDaysUTC(-360);
while(gr.next())
{
if(gr.getValue('last_login') < gdt.getLocalDate())
{
gs.eventQueue('remind.user.login', gr, gr.user_name, gr.email);
}
}

===

Not working. What I am missing? Thank you.

3 ACCEPTED SOLUTIONS

Muhammad Khan
Mega Sage
Mega Sage

@Ni_a Sy 

 

I have tried to implement your requirement and it seems to be working for me. Below are the steps which I have used for 1st notification.

  1. Created an event for 1st notification which will be triggered on 15th of every month. MuhammadKhan_0-1665309468021.png

     

  2. Created 1st Notification which will be sent when 1st event will be triggered.  MuhammadKhan_2-1665309583985.pngMuhammadKhan_4-1665309653506.pngMuhammadKhan_6-1665309708613.png
  3. Created system property which will store emails of those users to whom the 1st notification was sent. 

    MuhammadKhan_7-1665309840318.png
  4. Created 1st scheduled job which will trigger the 1st event. 

    MuhammadKhan_8-1665309965031.png

 

var grUser = new GlideRecord('sys_user');
grUser.addActiveQuery();
grUser.addEncodedQuery('last_loginRELATIVELT@month@ago@4');
grUser.query();

var emails = [];
while(grUser.next()){
	if(!grUser.email.nil()){
		gs.eventQueue('community.answer.last.login.notification', grUser, grUser.getValue('email'));
		emails.push(grUser.getValue('email'));
	}
}

if(emails.length > 0){
	gs.setProperty('community.answer.last.login.notification', emails.join());
}else{
	gs.setProperty('community.answer.last.login.notification', '');
}

 

 

 

 

Output/Result:

MuhammadKhan_9-1665310129153.png

 

 

Below are the steps which I have used for 2nd notification.

  1. Created 2nd event for 2nd notification which will be triggered on 30th of every month. MuhammadKhan_10-1665310243569.png
  2. Created 2nd Notification which will be sent when 2nd event will be triggered. MuhammadKhan_11-1665310318630.pngMuhammadKhan_12-1665310346774.pngMuhammadKhan_13-1665310394132.png
  3. Created 2nd scheduled job which will trigger the 2nd event. 

           MuhammadKhan_14-1665310493110.png

 

var grUser = new GlideRecord('sys_user');
grUser.addActiveQuery();
var emails = gs.getProperty('community.answer.last.login.notification');
grUser.addEncodedQuery('emailIN' + emails);
grUser.query();

while (grUser.next()) {
    gs.eventQueue('community.answer.last.login.2nd.notifica', grUser, grUser.getValue('email'));
	
	/*var grGroup = new GlideRecord('sys_user_grmember');
	grGroup.addQuery('user', grUser.getValue('sys_id'));
	grGroup.query();
	while(grGroup.next()){
		grGroup.deleteRecord();
	}*/
		
}

gs.setProperty('community.answer.last.login.notification', '');​

 

 

 

 

Output/Result:

MuhammadKhan_15-1665310554290.png

 

 

Above steps might help you fulfil your requirement.

View solution in original post

Hello,

 

As the script is written in scheduled job you can use execute now to trigger the schedule job

 

Saurav11_0-1665320965279.png

 

So first do the execute now for 1st scheduled job and check if the notification got triggered for users by

 

Going to sent in Navigation:-

 

Saurav11_1-1665321114008.png

 

And then see if all the notification go triggered to the users or not

 

Saurav11_2-1665321147515.png

 

Then once you confirm that you can trigger the second scheduled job by clicking on execute button

 

Saurav11_3-1665321193229.png

 

And then again go to sent and see if the second notification got triggered or not:-

 

Saurav11_5-1665321290545.png

 

 

Please mark my answer as correct based on Impact.

 

View solution in original post

I see that you have written the System property name as  system.property.last.login.notification

 

and in the script you are using last.login.notification, please check if you are using the correct name

View solution in original post

11 REPLIES 11

Hello,

 

Can you create the above as a new post/question. Happy to help on that new post.

 

Thanks.

@Ni_a Sy 

Something like below in your 2nd Scheduled Job script might help you resolve your query.

 

var grUser = new GlideRecord('sys_user');
grUser.addActiveQuery();
var emails = gs.getProperty('system.property.last.login.notification');
grUser.addEncodedQuery('emailIN' + emails);
grUser.query();

while (grUser.next()) {
    gs.eventQueue('last.login.2nd.notif', grUser, grUser.getValue('email'));

    // To remove roles from user.
	var grRole = new GlideRecord('sys_user_has_role');
	grRole.addQuery('user', grUser.getValue('sys_id'));
	grRole.query();
	while(grRole.next()){
            // To add roles in the custom table.
	        var grArchive = new GlideRecord('u_ar_sys_user_has_role');
            grArchive.initialize();
            // Assuming that "User" and "Role" fields are reference fields to sys_user and sys_user_role tables respectively.
	        grArchive.setValue('<backend_name_of_role_field>', grRole.getValue('role'));
	        grArchive.setValue('<backend_name_of_user_field>', grRole.getValue('user'));
	        grArchive.insert();
		grRole.deleteRecord();
	}
}

gs.setProperty('system.property.last.login.notification', '');