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

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.


That looks like a pretty straightforward solution. What would be the best way to accomplish Step 2? My script-fu is pretty lacking currently..


(function() {


var gr = new GlideRecord("sys_user");


gr.setWorkflow(false);


gr.autoSysFields(false);


gr.query();


while (gr.next()) {


  gr.u_welcome_email_sent = true;


  gr.update();


}


})();



Should do the trick.   It just "checks" the checkbox field without updating the system fields.


Would I run that in Scripts - Background?


Correct.   Sorry, should have mentioned that.