logo

NJP

Using ServiceNow Mail Scripts for Stylish Notifications

Import · Jun 01, 2022 · video

hey everyone welcome back to my channel it is so good to have you here in the last servicenow toolbox we talked about how to put pretty hyperlinks and formatted text into your work notes and somebody asked me if that's possible to do in emails as well as a matter of fact it's a little bit easier and we're going to show you how so to set this up i've just created a notification record that gets triggered off an event and i put an event in the event registry called programs.weekly.summary now if you're not familiar with system events and what they're good for check out in the top right hand corner there's going to be a link to a video i did it's also going to be in the description below also if you're unfamiliar with mail scripts this is going to be the episode that's finally going to teach you how to do scripting and then embed the results into emails that you're gonna send so as i said we have a notification here and that notification triggers on the eventprogram.weekly.summary i've also gone ahead and fired that event because what we want to do is use this preview notification ui action so we're not having to walk through logic or deploy events again and again and again just to test all we need to do is save our mail script and look at the output so here we are in the mail script and let's just walk very gently from less complicated to more complicated in our scripts so the first thing we're going to do is a male script that builds in a line for every project in a given program here we are inside the mail script so i've got a variable called line builder and that's just going to be the thing that creates the big collection of lines that we're going to post to the email then we have a variable called prjlinks that's going to be a glide record to the project table and we're going to add a line where the primary program is event.parm2 and this is one of the cool things you can do with notifications that are event driven is if you're building mail scripts off of them you can feed it uh properties of the event so when i actually triggered the event to launch this notification i gave it the event name i sent it the program object i sent the email i wanted to send to and i sent parm2 as program.sysid okay then i'm just going to print out a title it's going to have template.print open project summary and then in the while loop we are just going to make the line builder have whatever the number of the project link is plus a space in a dash in space plus the short description of the project in that iteration and then of course we add a plus slash n to make sure that the next line is put in the line below we're going to template print that's male script speak for actually write it down and let's see what this does we're going to save it i'm going to come back to my notification and hit preview notification and we're going to use an existing event i'll pick it from the list of events that have been fired and it's got the title and a list of the projects we should have a slash n on that title because it's just written that project right after it come back here i'll add a slash n save that and when i rerun the preview it looks a lot better okay let's start jazzing up the title and doing some text formatting here we are at the second iteration of this script and the only change i've made here is within the template print for the title we've gone ahead and put some html tags in there for the text so formatting text with html within a mail scripts is as easy as adding the html codes directly into the template print you don't have to use that code and code format that you did when you were trying to do this in work notes so let's save this i'll go back and preview notification again and we see that the title is a lot better looking it's got a header to format to it and it's underlined exactly as we wanted okay ramping up the difficulty a little bit now we're going to embed hyperlinks for each of those projects it's not enough that we tell somebody an email here's a list of projects that are related to this record that we're sending you an email about let's embed the hyperlink so that they can go right from email into servicenow and see the records in question i think embedding hyperlinks into text and emails is probably one of the most underrated things you can do to improve the quality of notifications in servicenow okay so what we've done here is i've started breaking out line builder truthfully you can put this all on one line i just like doing uh different things in different lines and adding them all together with plus equals it just makes it feel cleaner and easier for me so it's kind of doing one thing at a time so the first line is just saying hey let's dump the number add a space dash space that's all i'm going to do then immediately we're going to add to it the hyperlink now i'm going to spend a little bit more time and break down how i do hyperlinks because it's a little bit tricky when you're looking at all these quotes and double quotes and and pluses and all this stuff so let's actually break it down so let's just imagine let's just write out a full uh ah reference as if we were coding in html so let's just go ahref equals and i want to send this thing to a relative url that is the project's direct link so if i do this and i know i want uh a big long society i'm just gonna fake it and then i know i want to say target equals blank and then i want the thing to be the short description i want that link i want the words to be short description and i want the link embedded into it and then slash a so this is all the stuff i know i'm going to have to do in order to formulate that url so now what we have to do is break it up because really it's just a chunk of text right now in the code and it's going to render it as html so i know i'm going to have to get this cis id from somewhere right so let's wrap single quotes and this editor drops double quotes at a time so i'm so we've wrapped single quotes around that href and so it's going to be this block of text plus something and that something is going to be uh the project society right so let's just give some space for that then let's wrap the rest of what we have in single quotes and the reason i'm being real specific about single quotes is because to actually write out the html we need double quotes for this blank part right so we got to make sure we're using a different type of quote around the outside of that let's go in here and drop our project cis id project links dot sys id that's the project sys id that's gone through this while loop from this glide record now it's going to take that project society blast it into this line of text it's going to contain the uh relative url now let's continue on with the rest so we have this block of text that's target equal blank and i know i want to put my own short description in there so let's end the let's end the quotes so this is its own thing and i know i'm going to put a plus and something in there let's take out the short description and we know we're going to have to make this its own block and what do i want to put in here i want to put the project links dot short description it's a bit of a pain in the butt but you gradually get used to where you got to put the quotes in to break it all apart to make sure that the specific information you want is inserted in so let's clear that little exercise out we've got line builder here it's starting off by saying just get me the this project's number and break it with a space dash space then it's adding to that and saying add in this hyperlink it's going to use a relative url we're going to put in the project cis id and also the project short description at various points in that ahref so that it renders as the short description looking like a hyperlink and then we are going to print this thing so let's save this here we are back at the notification let's preview the notification okay for the next scenario we want to put in little colored emojis to tell us the status of the project in each of those lines that we're putting to the email so you'll notice i've created a new variable called status dot it's just undefined for right now when we get into the while loop status dot calls a function called get status color and it feeds it the status of whatever project we're in in this iteration of the loop so let's see what that get status color function is here we are at the function it is going to receive something called that we're going to call status so that's the parameter of the function and it's going to create an object here called status color map and it's just going to be name value pairs and what we want to do in this function is return the object in status color map at the status that we give it see square brackets mean at a certain position an array would be like position a number a position zero one two three for an object it's the position of the property so let's grab um whatever the status we give so it'll be a color it'll be red yellow or green and it will send us back one of these emojis if you want to know where to get good emojis i go to get emoji.com and that's got nice big pictures of every valid emoji in our case i looked up circle as a search term and we got a wide range of circles including green orange red whatever we want okay so let's save this mail script let's go back to our notification and hit preview notification and now we've got beautiful emojis on the lines that we've printed inside that email looking better already okay the final scenario we'll run through is we want to put formatted text on the end of each line that talks about when the last time each of those projects was updated the idea is the program manager is going to look at this email they'll know exactly which projects they want to click on and go to based off of its status and when the last time it was updated so we see in line 29 here that i've got a day since update variable declared and undefined and then once i get into the while loop it's going to call a function called get days since update and it's going to feed that function projects dot sis updated on which is the last updated date of the project of the project that we're currently iterating through on this while loop okay let's see what that function does the first thing it's going to do is declare now as equal to a new glide date and time if you just say new glide date time with no parameters inside there it just creates right now as a date time record then because i'm sending this function an object called last update i'm never ever quite certain what i'm being passed so i like to be extra careful about the data type and so i'm going to force that last updated even when i'm expecting a date and time i'm going to force it to be a new glide date and time via this variable called force date time it's going to be a new glide date time with the parameter of last update forcing it to be a date time then we're going to declare a variable called dur duration and it's going to be a glide date time that subtracts for state time from now then what we're going to do is declare a variable called days part and that's going to be duration dot get day part so i'm expecting something that is expressed in days highly encourage you guys to check out the glide date time api documentation on developer.servicenow.com or on docs if you can find it i'll have links in the description below but there's all kinds of goodies in there some of which i've only uh learned about in the past few days all right then we're going to ask a question hey is is days part greater than 5 if so we want to return this text not updated in and then it's going to create an html tag span where the style is the color tomato and then we're going to put our day's part in there as the number that's actually going to render in that style then we are going to close those style tags and add the word days and then we are going to return days apart and then the way that's going to look like is inside of our line builder we're going to have our hyperlink with our project society our target of blank our short description and then it's going to have our day since update and then it's going to force a new line then it's going to print it all off let's update this mail script and let's go to our notification it strikes me now that i've gone all the way through this video and i actually haven't shown you how this notification calls the mail script which is super important so let's go to the what will it contain tab and there you'll see you can write whatever you want and then whenever you want to create a mail script like let's just put this text in here this is the result of our mail script see below so i'm going to have that text and you call mail scripts by going dollar sign mail underscore script colon and then whatever the name of your mail script is in our case it's project status links let's save this and now let's preview the notification all right so we see the text that we added above the mail script we've got the title exactly how we formatted and we've got the project numbers our emoji pips our embedded urls and we've got that block of text where it says 11 uh 42 days on just the projects that hadn't been updated in that amount of time for the rest of them i just had zero probably could have designed text better than that but you get the idea and there you have it folks the same thing we did in the last video where we beautified our work notes a little bit can be done in emails and this is really really really handy if you're sending emails that have kind of a collection of records in them and this happens all the time in spm here's all the risks issues decisions actions you might want to deal with here's all the projects tasks within your project that you might want to deal with here's all the projects in your program that you might want to deal with so i find this invaluable hope you learned a thing or two in this as well and we will see you on the next one if you're a servicenow expert looking for better opportunities but maybe your resume or linkedin profile isn't doing you justice reach out to me via linkedin or the email pictured here as i offer both career coaching and recruitment services and if you're a servicenow customer or partner you heard that right robert fedoric now does servicenow recruiting with a 1 500 subscriber youtube channel and mailing list and thousands of linkedin followers let's make sure your open positions get first go with the prodigious pool of servicenow resources reach out via the emailed picture here

View original source

https://www.youtube.com/watch?v=PpWMFORUsv0