#18 ServiceNow Developer Training | WORKFLOW in ServiceNow with Scripting
[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 this is follow-up session of servicenow developer training in this session we will learn about workflows and how you can do scripting in workflow this session will majorly focus on scripting in workflow as this is for developer training we will start with what is a workflow workflow is an automated sequence of activities it can run repetitively that means if you have some actions or steps which you want to run repetitively in that case workflow is supportive in that case in that scenario it performs activities as per the activity defined in sequence so if you need to automate a process you can do it with the help of workflow service catalog items are the best example where you mostly use workflows so if you have some steps maybe some kind of automation which you want to run in sequence maybe update of a field then creation of some task record sending an email in those cases you can use workflow which will automatically run each action in sequence and that's what workflow is baseline workflow service now has out of the box workflows which have been created for different functionalities like change service catalog or sla as you can see on the screen we have workflows for service catalog item request which is specifically for service catalog item then we have knowledge instance publish which is for knowledge management and we have some other workflows which have been created directly from servicenow and these are all out of the box however servicenow developers and administrators can create custom workflows as per the requirement from their customer and client workflow application and editor service now has a separate application to create and manage workflows here you can see the application called workflow which is the application where we have different modules to manage workflows as well now the major module the main module of this application is workflow editor which is the module which opens the editor for workflow where you can see existing workflow and you can also create new workflows or maybe you can edit existing workflows workflow editor components workflow editor interface has different components in service now the first section we have is canvas which is the main part of editor then at the top you have canvas tabs where multiple workflow can be opened in tabs then there is a title bar which shows the details about selected workflow as you can see it is showing the name of the workflow and it is also showing whether that particular workflow is published or not in the right hand side we have palette where you have list of workflows and activities and at the top you have parrot tabs where you can see two different tabs one is workflows which will show you the existing workflow and then the second tab basically shows you all the activities available for a workflow while you will create a new workflow or maybe open existing workflow workflow activities in order to process the automation you add different activities in workflow for example if condition running a script sending an email or ask for an approval activities are added and removed on workflow canvas for a workflow you can also define the transition between different activities in the workflow from start till end so these are the workflow activities so the first workflow activity we have have is approval activities in which you have approval group approval user approval action and then we have condition activities where you have if conditions switch condition wait for condition that means if there's a condition is being met till then it will wait and then we have notifications in which we have create event activity and we also have notification which will send an email scripting in workflow developers can also perform scripting in workflow a lot of activities have a script field which gives ability to perform custom scripting and logic as per your business requirement so these are the activities where you can do scripting and the activities are approval user approval group if wait for condition activity notifications activity create event activity rest message activity run a script these are just example there are other activities as well where you can do scripting in servicenow and workflow practical demo now in this section i will create workflow and i will try to add different activities which we talked about in workflow and that will happen practically in my personal developer instance so this is my personal developer instance where i will create a workflow and i will add different activities now in order to show you this demo we will just take the scenario of incident so what we will do we will create this workflow for incident table now as you know that we can create workflow in any table for any table you want as per your business requirement so in this case in this scenario we are going to create this workflow for incident table so what i will do before that i will just quickly show you the workflow application we have so if i type workflow over here so i can see this workflow application and i have this module workflow editor if i will click workflow editor as you know it will open workflow editor in new tab where i can see the list of workflows we have that also published and we have checked out as well and here also we have list of workflows in one tab and then we have the score tab as well where you can see the activities now what i will do i will create a new workflow for that i have to click on this button new workflow now this session is basically related to scripting in workflow so i'm not going to talk much about workflow because you must have learned about workflows in your administration section or administration learning but this particular session is specifically for developers because this is part of developer training so how exactly you can do scripting in workflow that is what you are going to learn so in that case what i will do i will quickly create a workflow on incident table so here i will do maybe sas now youtube demo let's say i'm just giving a random name now you can definitely give a logical name um i will select the table which will be incident this is selected i will go little bit bottom here it is asking me conditions for conditions i can give run the workflow always and i can click on submit once i will click on submit this will create the workflow but as of now workflow is not yet completed because we have to add different activities as per the requirement we have so what i will do you can see here we have begin and end this is something which gets created automatically when you create a new workflow and this is also showing you the transition but what i will do i will just delete this transition because we have to add different activities between begin and end activity so i will put it right here and now what i will do i will start with first activity that is if activity so if you will click on core tab you will see all the available activities which you can add here and just run the workflow so we will start with if so here we have in conditions you can see if i will click here and i will drag it if you will drag it on your canvas it will open up the racket that means that activity racket and then you can create it so in that case maybe i will do check priority now what exactly we are doing here i am going to checking the priority of the incident created and that's how i will run the workflow so in this case what i will do i will maybe so now i'm going to tell you one thing that you can achieve or you can do or you can create workflows and you can add activities with the help of drag and drop option as well so here for example you can definitely select the priority field because as i mentioned the the requirement basically we have for priority so if the priority is p1 then only it should do is it should basically go for the next action else not and we will see what actions we are talking about so i can definitely select that field but it it might happen that you you don't have that particular field available maybe you can't access or maybe you have a complex logic in that case you cannot use directly this condition a condition section so in that case what you can do you have to write a script so as of now i'm not writing a complex script because in that case you won't be understand better because this is just a developer training so i just want to make sure that you are able to get it what exactly we are talking about so as of now i'm not putting that condition over here what i will do i will check this box advanced so once you will click on this advanced check box this will open this script field and here you can write the script which you exactly want to consider as a condition because you have to evaluate whether that particular that particular condition is is being met or not how exactly you can do that what exactly we need to do we have to check the priority if priority is one then follow one one way if we have the priority is not one it's not critical then it should just shouldn't do anything that's what we want so in that case what i will do i will type here so answer now this is the basically here you can see the example this one now this is the syntax of of your script so that's i think i would say it's a really great feature that servicenow is providing they are giving you the help that what kind of syntax you have to write here so that it can run so what exactly it is doing you have to basically uh push the answer um basically the value the the value of that particular uh basically uh condition whether it's being met or not that's something you can you have to push it and answer so what i can do i can just run this basically function i can call that function how will i do that i will do check i will do check priority semicolon i will go little bit bottom here and now i will create that function so we have function check priority we will create this and now i will do if current dot priority equal to equal to if it's one now the back end value for high the critical choice is one and if that's the case i will do return now that return yes or no will be in the string so i'm doing return yes else it will return no and that is also in the string and then i'm putting semicolon what i will do now so our script is ready it is saying okay so this is the mistake we have done there should not be semicolon here so our script is ready now i will submit it when you will click on submit you will see the activity is created so i can just put it right here what i can do i can just and basically connect this begin with this one that means once workflow will be triggered it will directly go to this step first that means it will check the priority now once we have added this if answer is yes our answer is no then you can drive the whole workflow what we will do now we are going to add another activity and that will be in utilities we have run a script so i will add it here now what i will do if priority is critical one in that case we will set the value of category field maybe uh to software if it is something else maybe in query we will make it software now it is just a demo i'm just showing you so that you understand scripting so run script is basically basically it runs at server side first of all and it is kind of a free form of scripting so anything you want to run at server side you can definitely use this run script so what i will do i will put it here set category and you can provide the name as well so i will do set category now here i can do current dot category equal to now this is the choice value back-end value of software category and i can just current.category i can just click on submit that's it so i have written that script basically and it will automatically push that value now what exactly we can add now so let's add another activity that is approval so maybe it will set that particular value but we will also send it for approval so if you will send it for approval let's see let's see what exactly happens so what i will do i will add this approval user now where exactly we want to send this approval which user should you should approve it basically this this particular racket so what i can do now you ha you do have fields like uh caller or maybe you also have assignment group you also have a signing but maybe just an example uh you want if callers you want to send approver approval to the to the users who are from the same department of caller so whatever caller is being selected so approval should go to those users who have the same department now this is something you cannot select with drag and drop or this manual configuration this is not possible you can see that it's not possible because there are multiple rackets because i have to pick those rackets uh all those who have uh department as as uh same as caller uh caller uh feed so in that case what exactly you can do i will just mention set the approval as caller department users now what i will do i will check this advanced check box because that's how if you will click on this advanced check box then you will see the screen where you can write the script and it says additional approvers script so i'm not adding anything over here i'm just mentioning the script here so here you can write the script it is also showing you the syntax what syntax you can use so what i can do i can quickly do here answer i think that's the same syntax so you cannot change it we will do answer equal to now you can see that it is basically making an empty array and you have to push the rackets into this array and that's how that particular approval will happen so what i will do we will basically glide racket because we have to check the department of the caller we have on this incident racket and then we have to glide record all those users who have same department so i'm sure we have to do this glide racket so i will do where gr equal to new glide record and here i can put the table name that is says underscore user semicolon here i can do gr dot add query and i can do department now department is the field name on user table because we have to check the department and i can just do comma and now here i can put let's say current dot color underscore id that's a field we are capturing the user data which is in the caller field so i got the society but now you i have to dot walk it and i have to get the department of the caller id and i can just do it with the help of dot walk that is dot department semicolon and here i can do gr dot query and i can do like this now i can do while because it has to run the loop because you might get multiple users with the same department so you have to run the loop and then i can do gr dot next and then i can write the script over here now here i have to add semicolon now what exactly value you have to push now we have to basically push it like this answer dot push and here you have to push the society of that user now i can definitely get that society with the help of like this gr dot says underscore id if i will do this it will definitely return me the society of the user which is coming in the loop but there are multiple users because you might have multiple users in that department how can we push multiple users so that it goes for approval to all the users in that case what you can do let's create another variable here i will do where a maybe just a random variable and i will make it blank now here what i can do a equal to i can do a plus now how you have to push this value in the array that's something we have to do with comma so i will do this comma and then i will do this plus we are pushing society i have this answer dot push and now i can do this a so what exactly this a will capture i can tell you right away so i will put it as a comment so that i don't want to run that code if you will do that this a equal to will be i will quickly show you what exactly it will capture it will be like for example you have multiple society now this is a society one society and then you have another society that's what it will push because it is keep on capturing that gi dot society that means the user id user society basically for each user you have so in that case you will able to capture in the a and i will just delete it for now and we will do this answer dot push a and i am going to submit it so it is submitted you can see we have this approval user and now what exactly this approval for why we are doing this approval so and i will make this note to end and we will we will make this yes to run script so that it can update the category which we just mentioned over here and then what i will do and i will also added this transition so we'll make this transition over here a little bit down here and let's say this one here and like this so now it will ask for approval to all these users now if i open this approval user quickly so what we will do as part of the getting the approval so we will just make it wait for anyone to approve and we will make it uh when anyone rejects so it will reject the approval that means it won't be it would be approved so in that case it will follow these two paths approved or rejected now what exactly action you want to take once once these are approved or once these are rejected so in that case what i will do i will maybe add another script here run script let me add that one because we will write a script here and what we will do we will set the description this time so if set description so if it is approved i will do current dot description equal to now our description will be mem ticket now that's a p1 ticket so let's say make it major incident management ticket mem ticket and we will add as a prefix with the current description we have so we will make it current dot description uh we will do like this current dot description mem ticket plus current dot description that's it i will click on submit so you have created this script now this is for approval so i will just add it here now what about rejection so if somebody basically rejected rejects this particular approval what exactly you want to do what we want to do we want to basically uh change the category let's say so you ha if you have uh the category then then we can just change the category in that case so what i will do i will but how would you come to know that what was the previous category so what you can use now here and that's called you i think if you have learnt about scratch pad so now i'm going to use a scratch pad here because i want to store the previous value which we had for category because here we are changing it so how i will come to know in this step this category should not be changed it should be changed the previous value but i don't know the value of right now because that has already been updated so in that case now i will remove this one here i will write another run script and i will capture that value so i will make it like uh capture category so i will just capture the value of category how can you do that now when you create the scratch pad for workflow scratch pad basically it's kind of a placeholder where you can basically store that value so that value will be stored during this workflow so what i can do i can do workflow dot i can write scratch pad dot here you put any variable you want so maybe i can put short d and now i can what value i have to store current category so i can do current dot category so here i have mentioned like this i will just copy this one because i have to use it in the another activity now this particular activity will just store the value and then it will go to the next step so i will go here now if it is rejected so i will add another run script here now i will use that particular value which have been stored in scratch pad so i will mention here set category and here i can do current dot category equal to this is i think we have used short d i don't know i think we have to make it as a category not short d uh maybe i can just do cat for now so we have current dot category and this is workflow.scratchpad.cat now you will say it is not the same name so i will change it here quickly and i can make it cat and i can update it so it is done now i will make it rejected here and i will add it to the end i will connect this to the end that's it that means the category will be changed if it is not approved and if i validate it it is validated no warnings no errors i am closing this and now i am going to publish this and this time we will test this workflow so if i go to incident now so let me go to incident now what is the condition of triggering that workflow the condition uh for triggering that workflow is that priority should be one so in that case i will select any caller let's say this one apple tutor and i will keep this category in query as it is i'm not going to change it and i will make it high and i will make it like this and i will do test short description and i can also do here test description and if i save this let's see what happens it is saving the racket and you can see that it has set the category value to software but it has definitely send it for approval as well so if i go a little bit bottom so i don't have any approval related list but if it should have sent that approval for sure so how can i know that i can just maybe add a related list approvers related list so that i can see it so i have this approvers i will click on save and if i go little bit bottom you can see that we have 32 approvers how it took the approver so what i will do i will quickly show you so that you understand how exactly it is picking up the approvers you can see we have 674 rackets but out of 674 it only selected 30 uh two or 34 rackets so i will just quickly drag the department here is the department so department for able tutor was product manager and if i do show matching so you can see we have 32 rackets that means this approval was sent to all these users now if i go to that incident again so i have this test short description over here and if i let's say i approve this now if i approve this so description should be updated so let's say i'm approving this i'm an admin right now so i can definitely approve it directly from this screen you can see that here it has added mim ticket as a prefix and then it is showing the description which was earlier but that's how your workflow works and you can do scripting but now we will see the rejection as well so in that case i will create another ticket and here i will do test short description and i will do test description and i can just select the same one so that it can select the department accordingly i will make it critical and i am going to save it if i'm saving it you will see that category will be changed and we have short description description and if i go at the bottom you still have those approvers this time i'm going to reject it if i click on reject you will see difference you will see that we have back to our same category which we had earlier in query and help so that's how you can use the power of workflow now this is just a demo as part of the example for this developer training but workflow has really great power so that you can utilize you can automate a lot of things a lot of process a lot of i would say even if you want to do some automation work that's something you can achieve with the help of workflow now before i end this video i also want to highlight one important part that servicenow has also a great feature that feature is flow that comes with the flow designer so similarly like you create workflow you can also create flows as well but the only difference is you will need less scripting over there so whatever we created this time over here you can achieve this with the help of flow as well and that is also pro that is also without scripting i will try to do one thing i will try to maybe publish another session with the flow without doing any kind of scripting so i hope you like this session the whole training we have for developer training please provide any feedback please post any comment like share my videos as well and subscribe to my channel thank you and have a great day
https://www.youtube.com/watch?v=FwqfZWDwHSI