logo

NJP

ServiceNow Scripted REST API demo | ServiceNow Create Approval API | ServiceNow integration

Import · Nov 29, 2022 · video

foreign [Music] so in today's tutorial we'll be looking on one of the use case I know a lot of people have afraid about the scripted rest apis okay and using this use case I will try to throw some light on how we can create a scripted rest API into it so all of us must be familiar with the table called role table okay because if all the approach which get generated across any task extended table get added to this table so today we'll be creating an API an approval API you can consider to approve a record and if you get this the inside how exactly it goes in a structured way okay so you can create your own API to re for the reaction also okay so let's get started without wasting a time so we will keep creating an API where we'll be passing a society of our Pro record and it will check whether that particular user who is actioning upon that particular record is their valid user and if it's a valid user then it will approve or approve the record else it will throw the header until we have started so first of all I will just give you a glance of the table on which we will be working okay so the table on which will be working is called sys is approval table and the backend name for the same is sys approval approval paper so okay dot left and if we see over you in this particular table so we can see here all the approach record get generated as you can see a pro record with respect to a change requests are there and we'll be focusing on the requested one because we don't want we how we can approve our record which is already my approved right it doesn't make any sense so we'll build a logic step by step will go in a pretty systematic way and get everything done okay so let's get started first of all when we want to create a scripted recipient so you have to go to a model and the service now called scripted rest API okay and we will be creating a new entirely from the scratch okay and over here I'll click new in this we'll be giving a name called approval EPA for instance okay and API is there okay I will just save it out once you save it out it will generate couple of information okay security related so here you can Define what kind of easy is it's acting upon so for example if you want to create an ACL I will just I will just remove this out okay and we'll create a new one foreign Access Control linear system right so you can use any one of them um okay or you can create your own so we'll keep it as is for now okay we'll not add this is here you can add the documentary what is the use of this API and all this so we can give this is come on APA okay and I will save it up now inside this inside this scripted rest API you can see the research and we'll be creating a resources only okay so you have to click new each resource acts like one API okay so as it's an approval API and we want to update certain things on the uh on the record okay so we can make this particular EPA as a post EPA or as a put API okay so I will be keeping it simple and I'll create a post API so the first one will be approved it will be of type post now whenever as I've said right we will be will be updating the record based on this ID right so you can modify the logic to a greater extent but for now I'll keep it like you have to pass this ID and based on the society you you will face the record which needs to be approved and you will approve it out so what we'll be giving we will give a relative path and the relative path what we want to give so thank you approve slash and then we want to pass an ID also right the ID will be this ID of this record you can you can consider in that way okay and this is the place where we will be writing all the script okay so I will save it out and now we'll start writing the code so what I generally do or when I create an EPA okay whenever I'm working on certain API so I will not write the code in in the script address resource okay because it makes it pretty lengthy so I will I am always focused on the functional programming uh in functional programming we you create a code into a modular format okay into small functions so that you can reuse that functions again again wherever it's required okay it's not necessary that that particular function is specific to this particular scripted area and you can't reuse it anywhere else so with that thought only let's create one script include and we will Define the entire function over there and then we will call that particular function from this side so I will go and I will create include and under system definition you can see the script include so I will do the shift tab we'll be creating a new script include and I will give it a name approval GPA the EPA okay we don't want this as client callable because it will be used on a script server side only you can provide the description it's always the best practice to provide description but as it's a tutorial we will skip this part to save some time and we will save it out okay so first and foremost thing we will be creating a parent functions okay and then based on our requirement we'll create a child functions for the same okay so let's create a function called um what task approval task task approved and if you are familiar about the approval things it requires to information we require only to information one is the Society of the record why Society because based on that only will we will be getting the information of the approval record right which approval record needs to be updated okay so that's the first thing and I will just keep put my laptop on charging adjustments foreign so that's the first thing and the second thing is the comment what comment you want to add to the approved record okay so we'll take only two attribute someone in this ID okay and the second one will be the comment okay now using this information we will proceed with the for the logic so as you know when you do the approval the comment is not mandatory so we will not have the check on the same so first of all if Society itself is empty so we can't close it right so we'll validate that out so if PS dot mil which checks whether the same certain things is empty okay or not different if the site is empty and what we have to do will simply throw an error so there is already a provision uh service I already created some out of the box tip status error okay so we can use it out so I will assign underscore W as underscore Yara which is which stands for error and then you have couple of Errors over here so here we didn't found a record right so we can go for not found error okay and we can see we can see the says ID press empty amazing right okay now the first validation is done now there is a possibility that this ID is given okay but that Society doesn't belong to any record on the approval table right so we have to verify this out how we can verify this house you can write the code over here but we will make it pretty systematic okay so what I used to do we'll create a separate function to validate whether this particular record is a valid record or not on the system so for that we will give a function called verify this ID and here we'll be verifying whether that particular record is a valid record or not so we need an input access ID why because we want to verify this ID only right so I will give this ID and then we can do variable we are approval equal to you know light record right record on which table as we are acting on this is approachable so it will be this of one underscore of true or right then the next line will be variable result will Define predefined the result and I will make it as false okay then we'll be like if record approval Dr Pro Dot get what says ID so Place ID if we are getting this ID and if we are not getting anything okay if it's there then we will return to then we will make result as true thank you foreign table with this particular society which we got from the EPA which we'll be getting from the EPA and we have verifying whether is there any record with that particular society if it's there then we are returning true as we are reacting false okay now we can add this logic this is how you should think this is how you should proceed when you are coding on any record okay on on on your system so now once this is done we can use this function out now in our code so if if this dot this signifies this particular script including which I am working okay this dot this verifies this ID this particular function it asks for one attribute which is the sys ID so we'll give Society right this is the advantage so you can if you want to use you can use this particular function also in isolation if you want to verify certain Society first is approachable isn't it is amazing that's why it does the power of functional programming so if Society if not so if it's not present so I will kill if it's not present then what we have to do then we will throw another message so I will simply copy this out copy copy copy paste and then we'll give all right card this is not found okay and there is some typo but we will skip it I think I hope it's clear now we have verified whether the suicide is empty if it's empty we are throwing error if the society is not valid then also we are throwing error so we have done up permutations for the same now what we have to do we have to check right that if a particular user who is doing this activity right okay he's a validator but he has um with for example I before that we will pitching now we got there is a possibility that we got a society with respect to that Society there is a record also but that particular upload record is not in requested set it's already being approved or it's already been rejected right there is a possibility of this also so we have to verify that also so what we want to verify that this particular suicide is the record which is belonging to this particular society is in the requested state pretty simple right so for that also we will be creating a record so hope function so we can give it a name called verify action record right card action and what it will it will again hold this idea simple and here what we'll be doing we will be verifying if that particular record is in requested set or not okay so I will just copy this out because I liked it I made a mistake over here you should not have spaces learning you should add comma at the end of any function not full stop a comma so glad record we have done now I will be adding an encoded query so I want a query so variable query equal to GS Dot get message and in message we'll be adding the encoded query you should always use um means I I generally use Okay add query whenever I want to perform certain string creation kind of thing query creation so we can go this and this code this is true all underscore 2 over Dot left we okay now here we can add a query so I can give what we want to see that the state should have been requested today is requested second thing you like the record Society so we will use this ID whatever Society will come so I will give just XYZ I will just run why I'm doing this this to get the imported query why I should create a query if if I can do this very right second go come back to the script include I will do this and I will paste it over here now you'll be thinking there is nothing called XYZ right I'm hardcoding the values no that's why I'm using get message so now I will replace this out and I will give the first parameter for my array which array which I will be defining oeo and here I can give than via approval dot add encoded query and you can give query query and now if there is enterocol existing exists with the society which we have provided and at the state requirement then we can proceed right so we can give if yeah approval next you can use has enough to also but I like to use next if it has any record so we can give let's define up per standards your variable result to pause and if it exists we will go to result equal to true and will return result so now we will call this function again I will copy this out will be like what in the similar way so if it's not false if it um what we can see how we can how we can Define The Logical over here if it's there right so if not okay dot verify action record ID and then we have to update the record right okay if record exists then we are returning true right so when true we want to update so it should be different if this dot verify records this ID if it's true that means the record exists and we can update it out my bad and in the else as we can depend first else means what he it's not that right so for that what we can do we can create dense custom error also okay let's create a custom error for this so variable for action I'm creating a custom error now Okay so so you know not out of the workshop how you can create a new underscore SN underscore WS underscore err right Dot service error okay because it's the service error right and we can give service error sorry record error I should give and then record error dot sets data I'll give it four zero three and and then this error record error Dot set message and we can give This Record is already and pause oh whatever okay and and you can return written in this particular record error so the else part is completed now we will look into the valid thing okay if the record is created and it's under this is how the structure goes okay so if the record is create a present with the requested State okay now we want to make it approved so then now next thing is like whether that particular user is a valid user or not right whether that particular user has an access to update that record right you can't give random users and you'll be like okay it should get approved no right so for that first of all we will be taking the information around the news I will use you is that ID equal to you must be knowing this APG is Dot get user ID so we got the login system user okay because he will be performing that actions right and then we can do variable record approval CR op equal to Nu you must be thinking a new client record Glide record but I'll be using grad across secure why secure because it checks whether that particular if there is any ecl okay on the record and whether that particular user whether that login logged in user has a right access on over that record or not so we'll be using says if you are using now light record for this kind of activity you should stop using it out says approval and this code approval okay and then we have to record we have to query it all this so we'll do gr approve um we can do add query and encoded query kind of combination so I will give add query what it will be first we want to check state as the question the backend name for the state is requested you should give back a name only the state is requested then how I came to know from here okay I'm not learning it out I am not creating any cheat sheet I have given away for you so that's why I record it from and then CR proof Dot DOT first ID for sorry add query and we will give this underscore ID whatever what's happening ID and it should be access idiom favorite Society for which we are doing all this blender okay then we want to check whether this particular user is value whether any record is there for this particular user or not okay if there is this record is triggered to this user or not so for that I I am not that smart okay I can remember this thing now so what I will be doing so I'll be doing a priority show matching a pro as David Lu okay all yeah we can give approval is this one and we are also considering this user ID right okay foreign you can create all kind of you must have given a previous condition loss over here but I forgot and I don't want to create now so let's yeah up to Dot and call it query and in that we will be giving this approval but we have user identity yeah so over here we will remove it out and we'll give it in the Legacy Way this condition and it will be like approve as where is that ID I have given user ID right user ID and and then we have to do CR record query and if a record exists that means is like there is upload record for the logged in user right so we can give CR approve if Chia approve next we have any record for that particular user then we'll be writing some logic if it's not so we will have this else Logic for that this is how you should write okay so that you you can consider each and every scenario if there is no record okay there is a record which is present at request State and all these things are there but there is no record for this particular approval then yeah there's nothing so we can simply copy this out again I will copy this entire thing paste it over here and now Ctrl a shift Tab and I will just change this message that can give a body you are not the right person to approve d recorder okay so this part is done right what happened over here record query okay not query is already it's an error but I can correct it out giving one one one okay now we have to update the record so how how will be updating the record so as you know we have a path to attributes which society and command so what we'll be doing in here true Dot comment comments dot set journal entry common is also General entry and we will give comments over here okay because we want to set the comments then we can give GR aph dot set value we are just setting the state now we can give did it is approved this is a backend name for the approved value approved Choice and then I think this is done and then you can simply provide a message so we can give it a return um what what is being approved [Applause] I think this is well we have to do if I'm missing something out just drop a comment on it under this is YouTube under this video so now we have completed the entire flow okay end to end now you can save this out first of all and then we'll be calling this out in the rust resource perfect so I will just copy this out says approval okay on let's do it so we'll come over here first now we have to pass two attributes from here one is a society and another one is a comment so let's focus on that how we can pass it to the script and how we can get the value pack so first of all variable ID I should get ID equal to request Dot path paragraph because it will be on ID will be on path para dot ID I think we again ID only write ID so we got ID this is ID of the record the second one is variable body repair equal to request dot body dot data so here we'll be getting the body of the record and it will be a Json okay so we'll be getting comment out of its variable common equal to body pattern Dot comment so this will be a Json payload key which we'll be using in the payload just keep in mind dot to string y to string because I want to be on a safer side so I will give two string and and then we'll be calling return while written because we will calling a scripting clue whatever will be the return of the script include from description we want to give the same return from the script to rest API easy to understand right so written new new what new this epna this one so I can give name this script includes dot the function name what is the function name task approve in this similar way you can create for project also it's done and well I have created scripted recipe without a pause and whenever you do this kind of stuff there is less possibility that will it will run in the first instance but fingers crossed so everything is done now we can test this API before that we can create a testing record so I will just give over here I will just play it out and there are only okay there are records right so my will be system administrator do we have anything it will not come like this Dot let's start with let's see I change the name oh am I so what is the profile system administrator I really it should give but it's not giving why I don't know so I can do one thing I can update the existing records show matching well Luke Wilson I will just replace a couple of this record oh I can't do that wow there should be some record with my name because I'm playing so much with this system what um approval is I paste my name is the administrator run nothing wow how do I get the record so I will I will simply go over here I will go here I will go it could be long into one thing it's weird I can tell first and go here we'll search for this system administrator not over here I can go and put it on the name or the easiest ways like I can remove that condition okay I can copy the sysid approval and we can remove this one I can take this up open this out right click open next door gr Dot something will be there equal to the process ID and then I can do update what is that field called Brewer hope it should work from you [Applause] approval run does it got updated yeah perfect now we got the record all right we have to do so many this kind of thing so I can open the rest AP Explorer this is kind of Postman from service to instance you can say where you can test all your IEPs so it's loading the EP is getting loaded so as you can see it's approved this will be the API it is post API we got path ID in idea as you know we have to pass the record ID which will be this and it's in requested state right so I will go over here and press the ID and we have to give a Json so I can give what it was comment right comment and it will be like through record and this okay and send waiting for the error and it keep 500. payload is not a valid Json who because because I give semicolon right no this is not right okay continue to do four zero four now csidy is empty where have given this Society right given this is ID then also [Music] right right right I made a mistake so I will go here I feel like it's task approved right but I didn't pass any values only right so first is ID second is command okay and I don't remember that's why I will just copy this out I'll save this out this is how you learn so I'll go here I will clear the response I will just cross verify it out once I will send waiting for another error this time 500. this is undiscuit ID is not defined is it not mad number 15 f11 [Music] series approval days this is how debugging is fun why because there is nothing called Society right it should be this underscore ID okay see you go back try again I will go clear response send okay four zero three This Record is already action upon are you crazy seriously no it's not oh I made it a mistake over here so it should be not I made a reverse condition receive I can go back to the recipe Explorer clear response send this time it should work scroll down 200 success record is being approved we don't believe on this commands we'll go back to the upload record to see where is that record see and you can see the record is approved with the Google commands so this is how we have created end-to-end scripted resist API for the approval of the record in the similar way you can create for your practice you can create for reject and you should follow this this way of thinking only when you when you create a scripted rest API don't make things over complicated okay on your set so this is all for today's tutorial hope you like this tutorial okay

View original source

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