logo

NJP

REST API Trigger 🤦‍♂️ Moment - Flow Designer Fun

Import · Jan 19, 2023 · video

[Music] hey everyone it's Justin thank you for watching and welcome to my house that's Justin's house in this video I want to talk to you about the rest API trigger that I most recently used in my integration between bitbucket and servicenow agile teams and everything worked but there was a big gotcha and it was my first time ever doing a rest API trigger in servicenow in flow designer I've actually personally never even done a scripted rest API so you can imagine I was a little lacking on the knowledge I was following the documentation and here's what tripped me up so when you set this up you build out a rest trigger and everything was pretty straightforward I picked a HTTP method which is going to be post I knew from looking at bitbucket that it was gonna be sending a post message this got automatically made for me and I knew I didn't need any query parameters which I could have specified down here or here in the path parameters and drop those pills into this piece right here I didn't have any of that complication and I thought all I needed to do once I picked post watch what happens when you pick something else if I do a get you know you get some changes there my body goes away so when I had post it automatically populated this with application slash Json and body now what I was expecting let's go look at bitbucket real quick if we look at bitbucket all I'm doing is sending a push from bitbucket to servicenow that's it that's the extent I didn't pick any of those other boxes and what I get the payload you can see right here pardon all my extra tabs in notepad plus plus but this is the entire payload a lot of stuff is coming in in that request or that post message from bitbucket to servicenow so my assumption was naive as I was is that this uh let's see how many lines is it 345 lines is what I was going to get in the response body and so I was like oh good I've got body there I can move on and I'm ashamed to admit I hit the done button and I moved on and I left everything like it was and it wasn't working it wasn't working it wasn't working it was calling this flow it was executing but I never got anything here in the request body this request body was always blank and I could not figure out why I assert first I Googled I didn't ask AI in this example and I just couldn't figure it out and then I watched a video thank You Chuck tomasi and served us now Dev team it was an old video but it was basically how to use this rest API trigger and my aha moment came when in that video they started building out that that body and adding things to it so you'll see and the one that actually shows in My Demo it's actually built out with all the different fields that are in that big payload that I just showed you in notepad plus plus now the big difference is I didn't grab everything so I did find that pretty neat I didn't need to pull in stuff that I didn't want so I pulled in the author raw but I didn't pull in my user information myself link my avatar HTML I didn't pull any of that stuff in all I did on this particular build out is added the things that I needed which was I needed the push object I needed the changes object array I needed the commits object array and then I needed the commit object the author object the message string the links object and then the HTML string within that so very very tiny subset of what was in that big payload and I was fortunate enough to have used the rest action before in flow designer where you can copy and paste a sample payload in here and generate the complex object output and and use that so I was familiar with that and I actually just happened to go do that because at first if you look at this first one here I have a step to go parse the bitbucket payload because I thought I had to do that work but it turns out this trigger when used properly and you know how to use it actually does that parsing for you so long as you populate this body information within the rest API trigger so the second gotcha that is probably again straightforward to a lot of people but I'm going to include it here because I don't know everything and I have to assume that not everybody else knows everything and so when we're looking at a Json object there's a couple of cheat codes you can use here number one is the indentation of this Json object So that's its reflection of nesting that's going on in the Json object and the use of curly braces so notice I have one big curly brace and then I I have a curly brace after push so this is the Json notation for letting me know that this big entire thing this first curly brace is there's an object coming and for the push I have another curly brace saying hey there's another object coming but this is a bracket not a curly brace and a bracket means that's not an object coming it's an array of objects coming an array meaning you can have this repeat one to n number of times like you would an array and that's how you know to set up these differently so if you look behind me here notice that some of these are objects and some of these are array.objects when I'm building out that body and of course the values that I actually want are strings so what's the takeaway for this what should you learn from this particular video number one is the rest API trigger very helpful you do not need to write a script scripted rest API for the particular use case I did or maybe not all of use cases but don't be a dummy like I was and just assume that servicenow is going to accept the entire response and build out a complex object for you without you pre-determining or letting the system know exactly what information it should be looking for in that payload and that's what I did here and that's what actually worked that's what made my bitbucket and servicenow agile teams integration work together play nice together however you want to put it I hope you found this video helpful if you did please like Please Subscribe or share with somebody who you think might be interested in Saving hours and hours and hours and hours of wasted time trying to figure out why their request body wasn't building its own complex object all by itself and until next time don't forget to always be learning foreign [Music] [Music]

View original source

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