Sending a welcome email to imported users

Tom Alday
Mega Guru

Through our LDAP connection all accounts created in AD are instantly created in ServiceNow. I had planned to leverage this process to send a kind of "Welcome" email to all new employees/students as they are added to the sys_user table, but I've hit a snag because even though their AD usernames are created their actual email addresses aren't created until midnight when a report is run somewhere by the AD admin and it creates an email account and attaches it to their username.

 

So what I need to do now is schedule something that checks for newly created accounts in the sys_user table and sends this "Welcome" email at like 4am everyday. I assume it needs to be done maybe through the event registry and the system scheduler, but I really haven't messed with those to features at all so I need some help...

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

This is what I would do:


1. create a new "Welcome email sent" True/False field on the User table


2. set the new field to "True" or checked for all your current users


3. create a new Event called "u_send.welcome.email"


4. create a new Scheduled Job (System Definition \ Scheduled Jobs) and select "Automatically run a script of your choosing" in order to create a new "Scheduled Script Execution" record.   Set it to run when you want it to.



Script for the Scheduled Job:


(function() {


var gr = new GlideRecord("sys_user");


gr.addEncodedQuery("active=true^emailISNOTEMPTY^u_welcome_email_sent=false");


gr.query();


while (gr.next()) {


  gs.eventQueue("u_send.welcome.email", gr);


  gr.u_welcome_email_sent = true;


  gr.update();


}


})();



That way you don't have to worry about when the record was created and you can easily tell with the new field if the email was sent or not.


View solution in original post

20 REPLIES 20

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You could do something like this with a scheduled job that runs daily and queries all users that were create on the previous day (or last 24 hours or something) and have an email address, then loop through each of the users and fire an event for each user. Maybe start from something like this. You will have to register the event and then create a notification that fires on your newly created event.


So the scheduled job should look something like this, right?



var queryString = 'sys_created_on<javascript:gs.daysAgoStart(1)';


var user = new GlideRecord('sys_user');


user.addEncodedQuery(queryString);


user.query();


gs.eventQueue('welcome_email', user);


As this is running on the server you do not need the "javascript:" part.


Remove it and you should be fine


That's close, but I don't think that query string is quite right and you need to iterate. I added a couple of query strings to choose from as well as a few more conditions you might want to consider.



var queryString = 'sys_created_onRELATIVEGE@hour@ago@24'; //created in the last 24 hours


//var queryString = 'sys_created_onONYesterday@javascript:gs.daysAgoStart(1)@javascript:gs.daysAgoEnd(1)'; //created on yesterday


var user = new GlideRecord('sys_user');


user.addEncodedQuery(queryString);


user.addActiveQuery(); //only grab active users


user.addNotNullQuery('email'); //make sure they have an email address


user.query();


//need to iterate through and fire the event for each user


while (user.next()) {


  gs.eventQueue('welcome_email', user);


}