How to send dynamic email notification in ServiceNow | EMAIL NOTIFICATION SCRIPT
[Music] in order to subscribe to my channel please click here or click here please share comment and like my videos and channel hello guys welcome to sas word service now in this session we will talk about notification email scripts servicenow can send email notifications on the basis of different events but if you have a custom requirement which cannot be achieved with notifications configuration then email script comes into the picture what is email script email scripts are used to change the output of email sent from servicenow instance based on different criteria that is also dynamically you can perform a lot of tasks like displaying incident data or querying a table and displayed the results in email body that you can achieve with email script email script module there is a module under system notification for email scripts called as notification email scripts this is showing you the list of email scripts available in the instance one of the important point of email script is that it is not triggered automatically you need to create the script and you have to call it from notification with the help of a syntax which you will see later this is email script form where the only main field is script accept name where you have to write the script to get the output which you want to embed in notifications so whatever notifications you want to send and you want to embed some data that's something you can achieve with this form with this creep field male script syntax like other type of scripting in service now there are some variables available with email script functionality which you can use in your scripting in order to achieve the dynamic requirement from your customers and client the first variable we have is template which is used to print and it is used with print method so the overall the variable we use it template dot print and this is used to print output in email body of notification so whatever data you will get as part of the output in that email script if you have to print that you have to embed that data into the email notification body then you have to use template.print then we have email underscore action that is email action now this is used to access notifications data the notification which is going to be created and you want to use some of the fields of notification then you can use this variable and this is optional if you don't want to use it that's okay if you don't have that kind of requirement that you can you should not use it so it's totally up to you as per the requirement you get but if i talk about in a general uh in general case of creating basically these email scripts mostly you will use template dot print event now this is used to access event if your notification is generated via events like you have used one event and you want to access the details of that event then you can use this variable as well and this is also optional then we have email variable which is an email outbound object and used with these methods to set some value in some of the fields so these are the methods which you can see it is used with email object and you have methods like email dot add address which is used to set cc or bcc of the notification then you have email dot set from which is used to set from email address then you have email dot set reply to which is used to set reply to email address when user reply to the notification then you have email dot set subject now this is used to set the subject of the email notification and then we have the last one email dot set body which is used to set the whole body now all of these methods with variables we're talking about this will basically override the existing value in the notification so even if you have used something in notification but these values if you will write the email script and you have used that you have called it from the notification then whatever body that notification had it will replace that even its body when it's from email address even bcc even cc even the reply to and even the subject these are these are the basically values which will be replaced with these methods calling mail script in notification so once you are done with creation of mail script you need to call it from notification in order to embed the output in the notification now in order to call it you have to use this syntax which starts with dollar then you have to put curly brackets you have to open the curly bracket and then you have to mention mail underscore script you have to put colon and then after colon you mentioned the name of email script which you have created and then you can close the curly bracket but this is how you can call it in the notification you have to mention this particular mail script this particular syntax which i'm talking about in notification body let's create a sample script in my personal developer instance and the scenario which we are going to take today is when email is sent to assignment group members for the assignment of incident then it should also send the count of open incidents assigned to the same group and if user click on that count then system should open service now with same list that means i should be able to see the same list where i have these open records of incident table but it should be assigned to the same group which we are talking about basically which has got that particular email that means whatever incident was sent uh whatever assignment group is populated on that particular incident it should only uh show the list of those open brackets for that particular group now how can you achieve this requirement now if i talk about the notification configuration you cannot achieve this for sure so if you get this kind of requirement from your customer it is not possible to just configure the emails that is the notification feature we have in service now you cannot achieve it you can only achieve it with the help of email script that means you have to do some customization so you have to write a custom script to basically achieve this requirement so let's go to my personal developer instance so this is my personal developer instance now the notification i'm talking about what i will do i will show you first that one so i will go to notifications maybe naughty vacations and here we have notifications and we will look for incident notification and i will just try to just show incident notification show matching and we are looking for basically the assignment incident assigned to scientific group there should be a group one as well incident assigned to my group now if i open this notification let's check it so i'm just opening this notification in the meantime i will also open my gmail so that you can see that notification so this is the notification we have for incident assigned to my group now if i come over here and if i see what data it is sending so you can see this is the subject which is pretty much dynamic you can see it is sending the number here is the assignment group and you can see it has not used any body it is still using the whole content is being sent with mail script and i will show you quickly i think this is this can be a best example as well we will still achieve the requirement which we are talking about showing the count of incidents open incidents for that particular group by the same time i will show you that how exactly it is making the whole content of the email so in that case i will click on create new over here so i will create a new incident and i will just assign to a group so let's say this one i will select the caller i will just mention test for male script now here i will select a group let's this time you will select network now why i'm doing that because i have very less number of open tickets for network team so i really want to show you this example so that it can it can basically populate the right count so that we understand how exactly it is evaluating and checking the number of rackets in the system and then populating it as well so in that case i will just maybe uh select over here network so i will select network so i'm going to assign this incident to network team so i'm going to click on that so incident is created now this will also send a notification that's what the feature is you can see email is sent and if i go to my gmail account you can see this is the email which we are talking about and incident has been assigned to network now the requirement is basically at the bottom of the email basically there's this body there should be a count now with that count if i click on that count it should open the same list of records so if if the user of this particular group will receive this email for sure and if they click on that count they should be able to route to the same list which has open records for that particular group so how can we achieve it now i will show you quickly so in that case what i will do i will go to email scripts over here so this is the module i'm going to click on this and you can see now it is it has opened the list of existing email scripts in the system so i will click on new so that it can create a new form of email script so as i mentioned earlier that there are only two main fields name and script as well so i will give it a name maybe i will mention sas now dot demo dot mail and i will just do mail script this is the name we are giving now here is the place where we will write the script so i will make it large full of screen and i will also make it big so that you can see the script properly so i will start writing the script now what exactly i have to do the requirement is that i have to fetch i have to query incident table fetch the number of rackets that how many number of records are there for that particular group open brackets and then i have to display it and i have to also make it hyperlink so that if i click on it if user click on it user should be able to route to the same list of records so in that case how exactly we will do that now before mentioning that script i still want to first check how what kind of query we can we can put over here so maybe what i will do i will quickly go to the list of incidents so that we can just understand so maybe i will just quickly go to here so here if i show you you can see these are the four incidents assigned to network team now these are all open so what i will do i will just check the query now the query should be state state is not one-off and that one-off is so should not be resolved it should not be closed it should not be cancelled else all other incidents should be counted and it should display in the email body that's a requirement so what i will do now and i will also mention and i will just select assignment group is network yeah and if i click on run i will only see those four records you can see we only have these four rackets which are open so what i will do maybe i will just um i will close one of them so i will make it state equal to i will maybe resolve it now then this particular one will not come so maybe i have to basically put the mandatory fields that's the reason i won't be able to close it so i will put the mandatory fields quickly and i will resolve it that's it and i'll put it right here i'll just put any random code save it this time okay i have to put caller as well that's okay and i will click on save this will be saved and if i come to my list again and i refresh it now we only have three so that's what i want to see i want to populate in the email notification how will i do that so i will go to now back to the same one notification email scripts and let me see i will just add the updated field as well so these are all out of the box so i will click on new because i'm going to create a new one so because the previous one was lost because we did not save it so i will do sas now dot demo dot mail script so that's the name we are selecting over here and then i will make a large screen and i will make it a little bit big as well now i'm going to write the script so as i mentioned we have to read the count of records open brackets so for that what i will do i will right here rare gr so we will create a glide racket object then we will do new glide racket now here i will do incident that's what we have to do then i will do gr dot add encoded query because we have to query the record that's how i can populate the count count of records now in this query we have to definitely put the encode query how exactly i will get the query that is definitely over here so what i will do i will open that list of incidents assigned to network team with the same query which we were talking about i will just do it again state and you will also learn that how exactly we make the queries so is not one off i will mention resolved closed cancelled i'm just pressing shift button on my keyboard to just select all of these and then i can just do and and i can do assignment group and here i can select network and i click click on run and i can copy the query i will copy it which i have to use it over there and what i can do i will create a variable called as query that's what i have to put into encoded query string so here i can put let's say if i will put like this but it has to be string so i will start with a string and then like this i will just do like this so that you can see the whole script and then i will do like this but this assignment group should be dynamic because that's what the requirement is we can't make it static so for that what we will do now till here we will keep as it is so what i will do i will just put it like this and we will add the dynamic value what is that dynamic value that dynamic value is current dot assignment underscore group that's it you're done this will automatically populate the society of the current assignment group that means the incident notification which was sent for that particular incident that particular society of the group will be populated over here uh so in that case i think i'm done with this query and what i will do i will just put query here that's it and then you will do gr dot query and i will mention semicolon and what i will do if even if i will get a single racket i will do gr dot next and even if i will get single racket i really just want to save the count of records how can i do that for that i will store that count of records in a variable that is count and i will do gr dot get row count that's what we have semicolon now i have to print basically i have to populate the count of rackets but it has to be a hyperlink now how can i make a hyperlink so in order to make hyperlink in html you have to use html tag that is the a tag so how can i do that so maybe let's let me create a string so we will name it as an str just a string i'm just using now i will start with this and i will start with h-r-e-h-r-e-f now this is the syntax of html hyperlink uh html link that's how you create a link with html tags that is a so i will do equal to now here i have to put it into quotations what what exactly i have to put it here i have to put the url what that url should be i will show you that quickly but before that i will use escape characters over here and then what i will do i will put basically the url url which i have to use so what i can do i can start with h t https so h t t p s colon colon colon and then i can do just slash slash and now this this one you can still leave at it as it is as of now you can just leave as it is so maybe i will just do what i can do i can just remove it for now and i can just keep like this because my first part will be like this now after https it has to be instance name how will i get the instance name because i am just generating the same url now what url i'm generating i will i'm talking about this one over here so i am just trying to generate this url maybe i will just copy it so that we understand what kind of url i'm trying to make i will put it at the bottom and i will also make it as a comment so what i'm doing i'm just trying to create basically this url so that when user clicks on that count this automatically ur becomes like it automatically routes to the same url which we have right here so what i will do i will just put it here starting with so i have i will do https then we have to put the instance name what we have to make this one so till here it is saved over here i have mentioned the string now i have to get this value how can i make it dynamic because you don't want to make it static so for that i can do gs dot get property now here i have to put the name of the property where we basically store the name of the instance that is instance underscore name now this will automatically store this value then i will do plus now the next string we have is this one so i can just copy here and i can just do maybe till here equal to i think we can also loop equal to as well and i can just maybe just copy this and i can put over here so this is saved now this is the another part of the string we have like this now after the sysquery we just need to put the same query that's what we need so that it can display the list of same rackets so whatever count i'm doing whatever encoded query so you can see now we we with this query now with this just one query i can use it over here i can use it over here as well that's what you can do so what i can do i can quickly just mention plus now the query variable which you have stored the whole string that query we have stored then i will do plus again now i have to once you you're done with the query now this is the end of the whole uh whole url i would say now what when you when you mention this href so this is the first quotation now you have to put the second quotation for second quotation maybe i will just quickly do like this escape character this will automatically add quotation again now i can do plus after this escape characters once you are done you just have to do like this so that means you are ending that reference tag a and then after this you can put because i have to now show the count how exactly i will show the count now the count is count variable that's something we have stored in the count variable so for that case what i will do i will again do plus plus and i will use like this and here i will mention slash a this is the end of a tag so we are done with the making the url and i will use semicolon if i will use that no errors now your url is ready and you will see the count will be displayed you can also do one thing that for example we are creating this str you can do one thing more let me show you that so maybe uh maybe i will just do quickly like this count off open records a sound assigned to your team yep i think this is the string we are using over here so we are ready with the complete url now how exactly we are as of now we will we will basically save that output in string in this str variable but how exactly i will print it in the body for that you have to do like this template dot print and in this particular uh basically as a parameter the the parameter we have to provide over here that's a message what message we have already stored in this tr variable and that's it so sgr nothing else you don't have to do anything else maybe i can just remove it i don't need this anymore and i can just save it so the email script is created now so with the same script it is created so what i can do now i can just but before that let me just quickly check the script if you have done any mistake uh i think yeah we should be good yeah i think seems like everything is good so what i will do now i will just make the screen a little bit smaller now and we will go to our notification so we'll go to notifications over here and we will do make some changes but let me just copy the name of the script we had this one mail script i will copy this i will go to the notifications now so this is the one incident assigned to my group i will open this one and now i will just add my mail script over here nothing else what i will do i will put dollar curly brackets mail underscore script colon and i will paste the name of my script that's it and i will save it this time you will see magic it will be totally different so what i will do i will go over here and i will create a new incident so i will click on create new and i will assign to the same network team because that's what we know that overall data so if i do like this i will just select caller this is demo of mail email script 2. that is part 2 and i will assign it to network team and i will just click on save and we will see what kind of output we will get in our notification you can see now that email is sent and that is this one and let's open over here first absolutely you can see that count of open brackets assigned to your team and it is showing four that's what we have overall we have four rackets because if i come over here and i refresh the list we have four the fourth one is which we just created that's the reason it is showing four rackets now if i will click on this hyperlink will it take me to the different url maybe let's let's open in our gmail account the email real email so let's say user will receive this kind of email that hey it is your email you have received to network team and you can see here we have this count of account of open records assigned to your team and if i click on this because this is now you can see it became hyperlink and i can click this and if i click on this it is going to open absolutely you can see that it has opened the same list of records that's what so this you might get this kind of requirement now as of now i have already logged in but it might happen that if users are not logged in they have to definitely logged in first but if they are already like idle users and they keep on working on the incidents then they will just click on that email notification and they can automatically be routed to this particular list so this is how you create email script to achieve dynamic requirements so if you will get any kind of requirement like this from your customer or clients when they want to embed something in the notification which you can cannot achieve with just direct notification configuration then you can use email script and do glide record check different data populate the data print the data and use it call it in the notification and send it in the notification body so i hope you like this video please like share and comment on my video and do not forget to subscribe to my channel thank you and have a great day
https://www.youtube.com/watch?v=KCzCO0VDm5A