Community Live Stream - Code Decoded - Mail scripts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] hmm [Music] so [Music] so [Music] so [Music] now [Music] so [Music] so [Music] so [Music] so [Music] [Music] good morning good afternoon good evening wherever you are and whenever you are welcome to the community live stream my name is chuck tomasi senior developer advocate at servicenow this is december 3rd 2020 and our topic today is mail scripts this is where you want to get a little more complex in your mail hello to everybody who's joining us live on youtube thank you very much for doing that if you do so well you will get wonderful notifications on your device like that if you subscribe and turn it well looks like i'm also subscribed and notified to simon's cat so have to watch simon's cat later if you don't watch that fun cartoon i don't know about you but that music at the beginning kind of wakes me up it's my morning caffeine to get me in the mood for this it certainly gets stuck in your head it may be monotonous but it's a whole lot of fun at least it is now so i hope you're enjoying that in the countdown timer to help you get notified and ready and let's get going so subscribe like turn on those notifications for youtube you know what to do we also do this on twitch as a backup plan and as a backup to our backup we have a local copy so in case my internet service provider decides that this is the time that things should go poorly we will be ready we have a backup plan that we can upload that mp4 and you will still get your information although it won't be live with the chat because i love the people that are in chat right now because it is it is a an exchange of information there are times in this show where i get a little stuck they have some ideas that i hadn't thought of and this is truly a live community so thank you looking at the names checking in good morning john and sir rob and i know dhruv is out there steve oh thank you the thing i really hate is cc is repeatedly not available we need to write a script i'm assuming you're talking about closed captioning you know what i will try to slow down a little bit i get a little excited a little passionate and i may talk a little fast i apologize about that so if your question goes beyond mail scripts today consider putting something into the community community.servicenow.com put it in the right forum if you're asking questions about secops there's a sec ops if you're asking questions about reporting and performance analytics there's a place for that by putting it in the right place you get the right audience and there are tens hundreds of thousands of people in the community that are eager and willing and looking forward to helping just checking the chat every once in a while sometimes i forget to unmute the microphones but i do see meters moving and i do see looks like things are working so youtube streaming machine is recording we're off to a good start right let's keep going then uh we invite you to go over to the developer community like i do every other episode if you haven't already heard this may be your first so if you're watching for the first time thank you very much for joining developer.servicenow.com is where you can get a free personal developer instance free learning plans take your career from admin to developer and developer to better developer and continue to learn and explore and try things out the api documents there's blog entries i just wrote one a couple of days ago some of you may know about that i'll get to that in just a second all right then where are we we are on to breakpoint the developer podcast if you're not watching that or excuse me not watching it's not a watching thing it is a listening thing if you're not listening to that podcast it is audio only there are no powerpoints no demos no update sets and we have discussions with developers in the community we have develop discussions with product managers we're going to have discussions with all kinds of people and there are many many more planned out every other wednesday that's every fortnight wednesday we come out with a new episode go over to bitly slash sn breakdash point and get that information in less than two weeks we will be covering instance data replication which is the easy way to replicate entire tables or filtered set of records from one instance to another that i know and craig stepp will be telling us all about that most people don't know much about email layouts i could do email layouts and templates and whatnot as well so that's a lot of fun good suggestion we will be putting that in a follow-up episode of this lots lots more coming in fact we are planning out what the agenda for 2021 looks like more of a road map but uh pick now look forward to more tech now in 2021 that's all i'm going to say about that but if you want to watch this one the last one we'll be doing in 2020 over to bitly slash tn 82 reg tuesday september december september december 15th at 8 00 a.m you can read why am i reading this lie to you we will be doing a little bit of scripting today and if you want to get more um what's the word i'm looking for it's hard to think of words at six a.m seven a.m excuse me 6 a.m pacific but i'm not at pacific time the if you want to get a little more accustomed uh comfortable with javascript go over and check out the sn learn js bitly link as well to learn more about that which brings me to oh where's the drum roll for this i have a rim shot but i don't have a drum roll uh we have a new series launched on december 1st yes the integration series is finally out or at least the first chunk of it is you're going to get this in installments because i didn't want you to wait a year and a half for me to produce all of these so as they are built and ready to go i will ship them this is just in time video production actually it's not just the time because you probably needed some of this stuff a while ago this is a way to make servicenow the platform of platforms by integrating those third-party services and importing and exporting data i will take you through everything there is to think about when it comes to integrating check out the first 15 episodes from the overview to data sources transform maps planning there's a bunch of stuff over there at bitly sn learn integrations not very not very good with the naming scheme on those bitly links but they are memorable aren't they i was thinking about shortening that one up and i went nah people will forget what the short link is so that will take you to the community link there is a blog post on the developer blog as well and with that i think it's time to go you know we didn't talk about we didn't talk about meetups there are a whole bunch of meetups next week let me bring up the meetup link i somehow skipped over that meetup.com pro service.dev program uh we've got washington dc we've got phoenix we've got vancouver there are a bunch of these next week if you're not part of the meetup scene go intermingle with other developers and learn and share and see what they're working on and see what they're interested in some of them go into topics about the platform some of them go into projects they're working on some of them are just ideation and there's a lot of stuff check out the different agendas there's actually a conflict between dc and phoenix next thursday so you get to pick what you want to look for what do you want to look at okay let's turn off the titles let's get into the topic of the day and that as i mentioned is male scripts okay i think i did that in seven minutes that's a record time and i got a new segment in there for the integration series quick sip of tea in the servicenow mug not too much branding going on on this show is there that's the fun part okay a mail script allows you to inject javascript code actual server-side logic into a mail file normally your notifications would say when should i send this who should i send it to and what it will contain and what it will contain can contain dynamic pills now keep in mind mail scripts are only available i'm going to say this more than once probably only available in the notifications that are in the notifications table so if i go to notifications up here i'm going to zoom in notifications okay in the little search filtery thingy down here under there's a lot of things that say notifications under system notification email notifications this table is where you can inject those mail scripts now you may see some legacy scripts from many moons ago that have angle bracket male underscore script angle bracket they have a different format that's the older legacy one i think we deprecated that in eureka fuji 2014 2015 sometime about five six years ago we said hey wait a minute there's a better way to do this because this is it you need to lend for reusability it would you put your script in the mail notification and if you had a common footer that you wanted to reuse over your copying code and it didn't work so we abstracted that to a different table to hold the mail scripts and then you call them from within the script let me get let me show you what i'm talking about see if i don't know if i can find a legacy one in here there might be one still hanging around i'm sure there's got to be an old itsm one that says and what is the not the send wind what's the field that contains the stuff i gotta look i remember the field names on these things now that was a poorly planned demo wasn't it let's just pull up a random one program updated that's what we're going to be taking a look at in just a little bit this what it will contain field message html there we go so let's see if i can find an older one just to show you what we're looking at from the legacy standpoint message html there's a message text as well but most of the modern ones will use message html contains mail let's just look for will it do this mail run don't see one maybe there's an inactive one don't see one maybe it was a poorly configured script of course i'm going to find everything that has a description field in it now yeah that's a little too much anyway can't find one right now but if you see the angle bracket there will probably be a url or excuse me a related link in the ui actions on the form that says convert this to a modern format and what it will do is extract that script and put it in the other table and reference it that's the way that this mail scripts are being done now what i've got right now i'm going to go back to my good old cls app that has courses and programs and whatnot and i want to put out a notification that says this program program contains a number of courses just like requests contains the number of request items and give a mail to whoever the program owner is when a course is added or deleted or something let's go take a look at that notification i am going to use studio for this and under studio we've got notifications there is my program updated notification i already built this earlier so you don't have to watch me build a notification when to send whenever a record is inserted or updated i turned off inserted and i put in updated so only when this is updated who will receive it me what it will contain i am going to use the general unsubscribe and whatnot preferences i think i hit a button on that one i'm not sure why but something happened subject and look down here in the script mail a message html field i've got this little insert here that says dollar curly brace mail underscript colon course list curly brace this is the name of the mail script i'm going to where did that come from glad you asked it's one of the few things unfortunately that you cannot access build modify in studio so if you look for mail script you'll find under system notification email notification email scripts very handy click that and up comes a whole bunch of records and i thought i had something in here for recently updated if it were up to me updated would be on the default list layout of pretty much every table let's sort by oldest to newest can't click that menu for whatever reason a male script is if you want to dynamically populate the content inside the email yes you can put records i should have should have pointed that out on the notification you can use this little field picker over here to put dynamic content in for example if i wanted to put the program name i see i have the name i can click that and it will put in name colon dollar name that's how you reference the fields on the current record and if it's a reference field yes you can dot walk in here as well contact business phone name into the assist user table login gender home phone somewhere in here there's a name field middle name name okay and that will put in a dot walked version of that so contact name is excuse me location contact name is dollar curly race location.contact.name not too hard to figure out how to put these in that's dynamic content but that is related to the current record in this case the program record what if i need to go look something up and do something different through a glide record query well that's where we're going now over in the email script is my course list and sort of built this up for you know what i'm going to look at it in vs code because that will be just easier and i will have this code available in the github repo that i think i've skipped over but we'll have github repo code in bitly slash sn-cls look for today's date 2020-12-03 and you'll find it there okay moments after that i hit stop on the record and everything so what we've got is a typical glide record query i'm going to look it up in the course table don't get bent out of shape i know i had somebody asked this before what's constants.coursetable it's a script include that holds the name of my course table so i don't have to type in x underscore something something something under scope under you know i get a little crazy about trying to remember what instance am i on what's the scope name here what's the scope name there i put it into this constants table and i'm happy for the rest i can move this around i can change the table names doesn't matter so the course table is the course table just a think of it as a pound define if you're an old c programmer or something okay i add a query go find all the records that are related to this program so current is the current record that i'm working on in the program table how does it know that well because in the notification remember we said what table is it on right there so any current record you get a number of objects in this mail script from the template that's automatically built in this function called run mail script we've got current ta-da and it even tells you this is a glide record right there at the top it says template this is a template printer class okay very handy i don't know all the methods in there there's two that i'm aware of that i'll show you in just a sec email is an outbound is the outbound email object you can tweak this if you want with changing the two from or whatever it is uh email action is the email action is see if i get back to my browser this record this is the email action you could pull information off of this record and put it into the email script for maybe some reason you want to know what email template put that in the body of your message this message was fired by the email template blah blah blah andy i don't know if it's actually useful but maybe it is to you and you've also got the event an event triggered this whether or not you used an event to trigger this notification and named event out of the registry is up to you but an event did trigger this it could just be the standard one that says hey the record was updated go trigger go check and see if any notifications need to react to this okay another good use for mail scripts is to include variables from a request yes it is that is a very common ask question i like to use it to go get related records what all when when i was first started as a customer we were getting approvals on every request item and we'd have a new hire and the manager would get about seven emails they said no no i don't want line item approval and denial i just want to do the whole thing at once and they could set up a preference to do that which way it would go well if you wanted approval at the request level you needed to go gather all those related request items to that request this is the same kind of thing we're doing with the courses to a program go look up all the child records this is the child record lookup boom okay if there are no records and i say if the get roll count is zero then i do a template.print template.print is the way we inject that text into the outbound email message that's the only time i think i've ever seen template.print used so it says no courses found in this program with a br so that a next line doesn't wrap otherwise go through every record you found and tell me what is the course for this program i'm not i'm not in putting any links or any actions or anything in here it's just displaying some stuff that's going to go get all the child records and include them in this message easy to test because we've already got it built over here same script didn't change anything if you create a new one i should have backed apologize if you create a new mail script it will give you this template for you and say add your code here that's what i did okay you want to test that you want to put that code in a script include you want to do something else that's great just make sure that you're using template.print to put your code in there into the email message and if you use template.printer descript include that kind of makes it exclusive to email messages only otherwise that template about print is gonna go what's that okay be careful of where your objects are and where it's recognized and what you're using so for this case i am going to go test that and fortunately right up here on the preview notification on the email notification record i've got a preview notification button click that i can pick a record a program record a record from the program table and it says there are no courses found for this program were there not i just want to make sure if this is a data issue or programming issue so rorack has oops i chose courses my fault well i don't see any courses related to rorek which is interesting i must have set something up here cls programs okay rorec open that up and i see no it doesn't have any courses perfect we know that that one case is in fact working however if i check if if it has five courses and i go in here and say fff which in old algebra course meant if and only if i do that you can see down here that i do have all of these they're not sorted they're not uppercased they're not linked they're just displaying okay nothing fancy there okay which service now would adopt the vs code monaco editor for script editing stay tuned there are some cool things coming for developers in upcoming releases i am currently going through as much quebec stuff as i can i can't give away any hints yet but there are some things coming and i know that script editor is an area of focus for our developer team i'm not going to tell you i tell you what's in and what's out of scope but there you have an example of what you can do with a male script i had a slightly different example or a use case a couple of years ago tech now those are all where we do we had the promo for tech now episode 82 web series comes up monthly admins developers that kind of thing when we do that we use a third party service to do our broadcast and you can join ask questions live sometimes we get more questions than we have time to answer so we collect those questions and import them into a tech now app okay this is my dev instance make that a little bigger yeah a little bigger this is my dev instance for that app and what i wanted to do was at after we've answered all of the questions and answers and post them back to the community send an email with links to your questions so if you've done this and you've you join our webinar you ask five questions one of them gets answered four of them are still outstanding we will come back and answer those and i want to send a message to you that says hey thank you for joining we've answered all your questions here's the links now if i went through the q a table you would get potentially four more email one skipping the one we've already answered live four with links to it's like nobody wants four emails what if i asked 27 questions that would be really annoying okay so let's consolidate this so what i did in that case is what the goal is send out one message with multiple links sound familiar we just had one program with multiple courses we've got one request with multiple request items you get the idea it's a common use case but to put the links in there was a little trickier of course that involved me writing some actual real good html that could link right to your question so when you click the link in the email will take you right to that blog post in the community to your question that's some deep linking but completely doable good discussion going on about visual studio code in uh yeah there is an extension you can use visual studio code for modifying stuff in the uh uh on the platform we're going to do that i think there's a live coding happy hour where we do that almost regularly but anyway back to my use case i'm going to bring up studio and step one was create some script include functions to gather that data i want to know if steve was on episode 81 and asked three questions what were the three questions he asked and let's generate some stuff out of there so in studio i go to server development script includes question util because this is where all the question stuff happens usually around the question table and i created a method a function called is that the one it gets the last one in here yes send question emails and i pass in the episode because the questions are related to the episode i send in the episode glide record everything you need to know about this episode the title the name the date who hosted it all that good stuff okay so say let's go count how many questions are in this table and then for each one i'm going to get the attendees for that so i've got to make sure that there's actually some real data there and if you had three questions i'll go get those three questions if you had no questions i will get no questions so a couple of parameters out there interesting usage so i'm not doing a glide record within a glide record i'm going to glide aggregate and if i get a result i go and do that so in here i put together who is the attendee what's the cis id what's their email address what's their name and two additional things to put in the object the list of questions asked by the attendee that is one of my longer function names get questions by attending episode so what questions did steve ask in episode 81 and then i generate the email based off of that list so this is going to give me back a list of objects which is up here okay get questions by attendee does another glide record look up you'll find a lot of my methods in my script includes are just glide record lookups so that i don't have to have these all over the place it goes into the question table says tell me what questions steve had for episode 81 then generate the email the html for those so if it's empty uh oh something's wrong why did you pass me an empty list if it's not it goes through it generates the html for each one of those with the html in there now okay now i've got this object of who it's for what's their email address what's the content i want to include i've got everything in this nice array of objects of what i need to send what good does that do me well it's triggered from i don't know if i have the demo data available here for it here's my episodes episode tech now episodes maybe there's something incomplete this hasn't been updated in quite some time about six months i've got episode 76 back here click i click and down here i've got a ui action that says trigger q a notifications you see that i look at that ui action very simple it does a couple of things okay first calls my send question emails next says i triggered them because this all happens in the background you want something and stay on the current page not a bad ui action is it because all the blood and guts is in the question util which i was showing you over here send question emails after it gathers up all that data it says go stringify it tell you why and then i'm going to cover this in an upcoming episode more about events and queuing events and whatnot but what it's doing is it's passing this entire payload to the email says i am going to generate an email for this person so if you look at question posted i should have a notification down here called question posted and i do the job of this is now pretty simple it says you tell me you want an email generated so every person gets this event generated and the payload contains all the html and everything else that they all already need okay who's it for it's going to trigger when the event is fired if i can zoom in on that a little when the event is fired question posted that was done by the script include it says got one for you here you know it's like somebody loading up a box and then saying here email here's the box of stuff you need to send this so i'm using a very complex parameter in that event again i'll talk about that in a little bit but it doesn't have to just be a simple string this is a json stringified object very stringified json i don't know which way that it's supposed to go who will receive it whoever's in the eventparm1 so i'm passing the recipient because i really don't know it's not part of the queue it's not part of the the record this is being triggered on this is being triggered on the episode table i don't know the the person's name i don't have a can't dot walk to it because it's in a related list and what it will contain if you look at this is okay hello male script name from payload i'm pulling the name out of the payload there's actually multiple scripts going on here thank you for your question during texas tech now episode blah blah blah down here is your list of questions pulled from the payload and my favorite one of my favorite parts is another one techno episode host signature so i've got three scripts running in this one message it's not one and done let's uh unzoom out of that and go look at some of those scripts if i go to mail script and i get mail from payload payload the header says hello whatever your name was member in the script include i was putting together that payload object the name was in there i don't know it when i get to the episode record but i do know it says go take parm2 out of the event parse that into a json object and simply print the attendee name i've already sent it to you it's it's like trivial at this point i've got this box and i'm pulling things out of the box and go oh look i got a name oh look i got a list of html things oh look so it's it's a lot easier to do this the other one was i can't remember the name the other one's named question list not very imaginative again but because it's a scoped app i don't have to be question list says go get that same object turn it into an object from a string and print out the html i've already made the list i've already made the html links i don't have to do that in my mail script let's put the ugly business logic stuff into the script include now this is a 5050 because it is it does have html and it could be ui but hey if i want to use that exact same list in multiple notifications i don't have to write the html my job is to write code once and reuse it not keep replicating it all over the place and the other one was something about the hosts there it is episode host signature when i made the initial template i would put on the bottom thank you from your hosts craig chuck jeremy i think when i wrote this it was craig chuck dave why do i want to go back to dev to modify an email template when we change hosts in the program that's a dynamic thing that's like changing users in a group that was way too hard coded i said wait a minute i've got that information from the episode record i know who's hosting i know who our guests are i want to put their names on the bottom of that email message so that's information i can easily look up this one has a little more script in it but again still using current template email email action and event okay let's go create a list of started out with empty uh this was before i started using a constant script include obviously and go look up the order go look up the episode so i'm going to the episode host many to many table pulls out chuck craig jeremy plus chuck greg jeremy yaron that kind of thing puts that into a list and then prints that list out with a join so craig chuck easy okay again i don't have to modify my template when the lineup of hosts changed it's just a matter of let the data drive the output that's what you wanted in mail notification so mail scripts very powerful you can put your big glide record lookups in here if you think you want to reuse it put in a script include call that okay set up the object pass that back to the mail script done i do want to share with you a couple of docs pages that i find very referenceable one is the mail script api in fact if you look under creating an email notification there's a lot of sub links that go under here notification preferences channels preview email email digest there's embedded video possibly narrated by yours truly i don't know did i do this one can't hear nope maybe i have that's all right not going to worry about it and in this sub menu over on the left there are some other things preview email notifications didn't talk about uh yeah i did i did show a preview but i didn't go into the outbound mailbox because they didn't actually send anything there is scripting for email notifications right there scripting for email note check that page out because it includes javascript and email mail script api which tells you about the template and the email action and those objects i'll share something with you don't let me forget that mail script api page some examples very similar to what i showed you good stuff in the docs pages so people may uh may pine for the wiki there we go if you manually enter a male script bounded by male script and male script in the body first of all i believe it doesn't let you do this in the new modern crete from new one you can modify an old one that says ah that's the old way to do it okay so try to avoid that old male underscore script stuff and use the dollar curly brace notation so you can reference that other file i know it feels like why am i creating this only to use it once over here kind of annoying but makes it much more reusable think about uh a common header and footer in the same message and you have one script that drives them both it's got your company logo and maybe a couple of useful links you want to put it at the top and the bottom hey only got to write it once right if you had that in the mail underscore script fashion you'd have to write it twice not reusable okay male script api i just discovered something shortly before the show started we showed you template dot print to print out some sort of message from a variable from a hard code thing from a gs.get message okay i also found template dot space which prints out a number of spaces kind of interesting because if you think about it if you wanted to do uh things that were aligned but the left side had variable string length like a table of contents okay this string is six characters but i want to make sure there's enough spaces to get everything lined up at column 80. this string is 32 characters line up at column 80. you could take the string length minus 80 print out that number of spaces and now get some alignment without having to resort to tables okay assuming you had a fixed width font okay we'll get into that that's all mail formatting stuff uh anybody have any questions out there looking through the chat real quick talking about gs.event queue wonderful thing i will be talking about that a little bit more because it does have some parameters and triggers and we'll get into that this was all about mail scripts and what you can do you can access from them and part of what you can access is passed in from gs.event queue if that's how you choose to trigger your email there's a number of ways you can trigger those we have to back up even further on how you can trigger emails all right that pretty much takes care of it i think just looking at my notes i want to make sure i got everything opened up the map oh other things you can do with mail scripts besides looking up related records to a record like we did with program and course like you might do with request and request item like i did with episode and q a that's a way of doing that first use case another one would be language translation think about that if the body of your message is in logical blocks maybe paragraphs or sections you could put into your mail script a gs.get message and say i am now going to maintain the content of my mail body in the messages table let me show you where that is real quick that would be over in underneath system ui messages it's easy because you just go all the way to the bottom after typing messages the name of the table assist under ui under message and you could put in here this is this message for english this is the same message for spanish this is the same message for german you could put your translations in with all the other system translations then when you add a new language to your organization you're doing that same export you get your field labels you get your error messages you get your email content as part of that export and import process to do the translation now your emails if they call a mail script and the mail script does a gs.get message it will get the appropriate language translation that you indicate okay that's one way to do that you could do language translations another way i'm not going to get into that at the moment you could also as i mentioned put in standard headers and footers so if you've got 84 different email messages in your organization and they say we need the corporate logo at the top of every one of these oh my gosh i go to modify 84 notifications yes you do but you only have to modify them that one time because you can put a mail script up that at the top that says here's my standard header and that mail script will contain the logo and as you and i know companies change branding they get acquired stuff happens right it's an organic organization those two words are related by the way you could put into that mail template go fetch the logo now when somebody says guess what we've just been acquired by great big company and great big company wants their logo at the top of all these messages all you got to do is upload a new image possibly change the mail script and away you go you don't have to go modify those 84 mail messages again because nobody wants to do that say great i'll take care of it i'll have it done in six weeks right it's a lot i hear it messages i gotta go update those no you don't don't send bay you you you work like you think you should ethically work but that's an easy way to reduce the maintenance is let the male script handle all that stuff okay the next time that brings us to the end thanks for joining i'll get big on the screen again next time monday december 7th 2020 i'm going to be talking about something that uh often what's the word of the gore causes some consternation some frustration with scoped apps i'm going to show you how you can create a wrapper so you can use a global only api in a scoped app okay i resisted this for the longest time saying i don't need it i don't want it it means it's a little bit of extra work but i finally got to a situation where i had to use it for someone else's work we brought in an update set or something and it had a call it was in the global scope and they needed to use it and it was global only it went rats so my scoped app had to call their stuff i'll show you how to put a wrapper around that so join me monday it's probably going to be a pretty short episode there's not really much to that but hope to see you there this is this will help you take that next step into scoped apps when you run into these uh challenges shall we call them thank you for joining me i'll see you back again monday till then stay safe teach something learn something share something and have a great weekend talk to you later [Music] bye [Music] you
Info
Channel: ServiceNow - Now Community
Views: 1,280
Rating: 5 out of 5
Keywords:
Id: bN0o3x_AZZU
Channel Id: undefined
Length: 52min 34sec (3154 seconds)
Published: Thu Dec 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.