Create a JSON payload with the JSON Builder step
[Music] hey everyone it's Justin thank you for watching and welcome to my house in this video I have another new Vancouver feature for you and it has to do with flow designers so in flow designer um if you're not familiar it's basically a low no code way to build workflows in service now but you can still do proc code if you want to in flow designer the new feature today is actually about building a Json payload with the Json Builder step which you can see here uh to my uh to my right or to your right which is I don't know it's in front of me um but we can see here create a Json payload to use in another step enter values or use data pills to produce a dynamic payload uh the step supports several data types including objects and arrays for nested stru structures so where I'm going to show this to you is I have a or I have a custom action in flow designer that puts an event on a Google Calendar which is right behind me you can see just above my head I have the otterly Allon podcast number 32 that's scheduled to go out today at 12:00 p.m. so when I get done producing a video um I do all my checklist and stuff in service now and when I publish it when I say it's published I'd like it to go ahead and create um an entry in Google Calendar for me and I'll just show you real quick what what that looks like what this custom action looks like so it's called create Google Calendar event it receives an input which you can see here for the content record itself the content record is a custom table I've made and um because I have this rest step that goes and sends that to Google Calendar I have to prepare the rest body for that to be sent now if we look here it's actually a pretty involved script about 21 lines um let's see if I can walk you through it really quickly here um I basically it's declare an array or an object I'm sorry for the request body and then I set the summary and description of the calendar event to the short description of my content record and I'm right now I'm using the CIS ID because I don't really have a description right now for my videos and then the start time and end time you can see it gets a little gnarly in here in the script because we've got to start doing some math in some formatting part of the math is um I'm in a different time zone so I got to subtract 7 hours and then I've got to add an hour for the end time because if you look just above my head I have the podcast from 12: to 1 but really I only store a date for 12:00 p.m. so I got to add an hour for the end time so it shows on the calendar correctly and then I got to format it the way Google wants it Google wants it in the iso standard where it's like 2023 108 T and then the time um and then we send the time zone separately which you can see me doing here where I finally assemble my my request body with the string for uh the start and end times and then the time zone I'm in the America New York time zone or EST or EDT I think we're daylight savings time right at the moment and then after all that's done I output it as the um request body in a Json string and then we use that in the rest step if you look here to send that to Google Calendar so there's the request body um there's an actual um API endpoint that we're calling and um I'll probably blur some of this out but basically that's what does it so let's test it real quick so you can see this working in action let's take our example otterly Alison number 32 which is up behind me I'm just going to search for 32 there it is and we're going to run the test and if it's successful which it should be cuz I tested this before I hit record it'll create another calendar appointment there which you saw it just come up so let's click on this one and there it is with the CIS ID um the other one I created manually so it doesn't have the sis ID so it worked it actually created it and it's on my calendar ER which is working like it should so now what we're going to do is we're going to use this new feature in Vancouver create a Json payload to get rid of my script so I don't have to have this big old gnarly script in order to do it so um let's just get right to it I'm going to make this bigger here I'm going to move my head over to the right and we're going to go ahead and add a step after the prepare rest body for Json Builder uh let's see if we can find it I went past it Json Builder right there so this is the new feature in Vancouver uh generated a Json payload uh so we'll select that step there and um so uh and I have seen this before so basically we're going to build out our Json object here and we're going to specify things like the type of data so it's going to either object a string and they're probably service now will probably introduce more stuff here eventually but I don't know I don't work for service now and then it'll let me preview the structure as I build it so um essentially what we need if we go back to this one I need a request body with summary description and then two other objects for start and end so let's go ahead and start building that so um we this one's going to be uh summary and then my summary value I'm going to pick from my inputs the content record and the short description so that's the same thing I was doing in my script but now I'm doing it in this Json Builder so I put it in there summary short description let's go ahead and add another field for the description um now these summary and description Fields right here this is what Google wants they want an object that has summary and description in here and for the description I don't have a description on my videos so let's do something a little different than what I was doing in my script let's pull in the number so we'll pull the number and then I'll put a colon so that'll be the record number and then I'll pull in um let's pull in the CIS ID again I guess so here we go sis ID so I'm going to have the number colon the space and the CIS ID and that's perfect now that I've got my summary and my description here I'm going to go ahead and start building out my date times let me move my head so I'm not in the way here and let's go ahead and add um the start and the start is going to be an object and that object is going to be uh date time as an attribute and we're also going to have time zone as an attribute time zone is going to be uh America is it back slash I can't remember is it back slash or slash let's look at what I did before uh up here it is that's a forward slash so good thing I checked that is a forward slash so do forward slash newcore York and then my date time we're going to actually choose from my content record I have a planned published date time field that's when I plan on that being published and then we're going to apply um a data transformation to it so I'm going to apply two data Transformations one is I'm going to add time um it's going to say an hour here at first but I'm going to go back and I'm going to change that hour to nothing because I don't want to add an hour the reason I'm doing this is because when I tested if I don't apply that add time to it it comes out in in Gren meantime um if I do apply the data transfer data transformation it applies it comes out in my time zone which is America New York which is the time zone I'm using so I don't know why that's in there but when you use date times and service now or any scripting or any application there's always some weirdness some wonkiness so I just I figured that out for this one um the next thing I'm going to do is I'm going to convert that string um to I'm not to a number sorry I'm going to convert the date to a string so we're going to do string to um date and time date to string I'm getting my words mixed up there so we can actually choose the iso date time that I was talking about earlier this particular one that service now does is it has this Z on the end of it and I don't want the Z now I guess I could use another data transformation however it's just as easy for me to put in a custom um one there and I'll just show you how easy it is I'm going to type it y y y and then Dash month month and then a dash Day Day and then I need my T in there Google wants that then hour uh should be a colon minute minute second seconds and we'll just copy that to the clipboard so I have it for my end date hit apply and now I've got that date formatted just like I need it if I scroll down here it's not going to preview that um but I at least see that it's structure the way I want I got the the start and the object and then I got the time zone and the date time in there so next up we just need to build the next one which is going to be the end so let's go ahead and call this end and it's going to be an object and then add the two properties there it's going to be time zone and uh date time for my two properties for my time zone we get to type that in again SL newor York and then we'll pick our plan publish time but we're going to want to add an hour to it cuz remember on my on my calendar here I want that to be an hour appointment I don't want it to be a just a a one a one minute appointment or something so let's do the plan publish date time now we'll actually do our add time time to it so date time add time we're going to keep that 1 hour I'm not going to take it away this time and then I'm going to do my date to string just like we did before custom paste in that format hit apply and now I should be done I should have a Json payload um right there and there was the number of things I had to configure so 1 2 3 4 5 6 7 eight things I had to configure I'm going to hit save um so I'm replacing this script that I told you I had before on my prep rest body we're not going to use this anymore I'm going to use this Json Builder step instead and the output of this Json Builder step is going to be used in my rest St so let's go ahead and get rid of the request body from what I was using before that was working so you saw it work and now let's go grab my Json Builder step and we're going to grab the output and put it right there in my rest request hit save and our big moment is going to come when we test this so let me go ahead we've got uh the calendar appointment right there behind behind me I'm just going to hit test we'll pick the same record otterly alom 32 I'm going to hit run and um it should pop up behind me if I've done everything correctly um hopefully in the right time zone and everything and it did not so something went wrong uh let's go ahead and click on this and see if we can fix it um I've got an unterminated quote looks like I I fat fingered my t quote there um I I put a a double quote instead of a single quote so let's go back in this is what happens when you um when you're doing stuff let's get rid of this and see where I messed that up uh data transformation and I probably messed it up in both date to string scroll down there there it is I've got a quote a double quote instead of a single quote so we'll fix that there and apply and I know I copy pasted it so we got to go fix the other one um let's see applied Transformations date to string scroll this way and um there it is there's that double quote convert it to a single quote hit apply and I should be good so I'm going to save and hopefully that's the only error sometimes I fat finger a lot of things or I just get I move so fast I you know forget steps but hopefully I've done it well this time and we see it pop up above my head when it completes yes there it is look at that it is right there uh it just created it so let's click on it remember the one I did have the content uh this one doesn't and colon in the CIS ID there it is the number of the record plus the CIS ID in there so that was created from this new Json Builder step let's go ahead and view the the execution just to make see how that came out so we'll go into steps here um there's still that prepare rest body that's still happening so it was repairing this one here where the description had just the S ID so that was the scripted way of doing it let's go down to the Json Builder step lots of stuff going on here but if you go down to Output we can see there's my output and there's the the record number with the description and the start and end times just like I wanted them formatted from 12: to 1: p.m. and um and then it does its integration and it post so that's the new Json Builder step I'm actually quite impressed with the ease of building this out um kind of replacing all this scripting here that someone would have to know JavaScript I'd have to know how to dot walk their way to different fields on record they'd have to know how to grab um create Glide datetime objects and then manipulate those objects by adding a subtracting time to them and then converting them into Strings and then replacing spaces with a T I was able to do all of that in this Json Builder step and probably a fraction of the time that it took me to write the script test it and get rid of all my bugs um because you never write the script perfectly the first time or at least I don't so I hope you found this video helpful if you did please like Please Subscribe or share it with somebody who you think might be interested in the new Json Builder step in service now's Vancouver release and until next time don't forget to always be [Music] learning
https://www.youtube.com/watch?v=Vhzx4Q8RIXM