- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2014 06:21 AM
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...
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2014 08:20 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2014 11:38 AM
Awesome. I got this all set up in Dev, we'll see how it goes. Thanks for the help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2014 03:52 PM
You are welcome. I was thinking you may not want to run that script on the weekends, so add this script to the Condition field (select the "Conditional" field):
(function() {
var day = new Date().getDay();
return (day != 0 && day != 6);
})();
It just checks to make sure the current day is not Sunday nor Saturday.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-13-2014 06:42 AM
Thanks, I'll add that in too.
Is there any reason doing it your way would cause the the conditions I have set on the email notification to be ignored? I set it up in Dev and I have a condition on the email notification not to send if certain fields have some data (basically I don't want to send this email to new fac/staff, only students) but after it ran this morning I show it caught a new staff member and would have sent her the welcome email too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-13-2014 08:05 AM
Is the condition on the notification correct? You may want to add the extra conditions you have on the notification directly in the script so you filter them out right at the source. No point looping through them and firing an event if you do not want to send them an email.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-13-2014 09:28 AM
Yes, those conditions are on the email template itself.
Would I just add the conditions to the "gr.addEncodedQuery" line in the scheduled script code?