Build a custom Flow Action to do Aggregation in #ServiceNow Flow Designer
hey everyone welcome to another go with the flow my name is robert fedorick it is so good to have you here in this series i am building full flows and doing minimal video edits so you can get to the content faster in this episode we are going to be talking about building custom flow actions and i love building custom flow actions because it does two things it takes stuff that a developer would normally have to do and it makes it more accessible to people who aren't developers and it makes it reusable now if you think about the last episode which you'll be able to see right about here we created a flow in order to create automatic status reports now you can imagine wanting to put some kind of aggregated information into those status reports like for example the total cost of all the unmitigated risks that would involve summing a currency field and while a developer is perfectly able to build a flow variable and have a script run it what if i've got four or five developers all building the same kind of logic i'd really rather encapsulate that into one reusable object that doesn't need a developer to wield so in this video the the core case will be a action that will allow us to aggregate data in a similar way that glide aggregate does in a reusable fashion it doesn't need a developer to wield we're going to do all the hard development once and only once and package that into a flow action okay let's just review the setup we want a flow action that behaves a lot like a glide aggregate script we don't want developers developing a glide aggregate script every single time and we want this wielded by people who aren't developers so the idea is that this action will be fed a table a condition and a field to aggregate and it will return all the different aggregates that are available had i used that script so we go to our flow designer home and we go to actions tab and we click new action and we're going to call this the aggregator and because i have a lot of different versions of this i'm going to call this the showcase submit all right hopefully after this video you won't be as intimidated as i was in building flow actions it takes a little bit of getting to know the interface and then once you know the basics it comes pretty easily but a flow action needs a couple of essential things number one it needs input what is it going to be fed from your other parts of the flow in order to do the things that need doing and then it also needs output so this actually takes stuff in and do stuff with it and then output some stuff now what really threw me for a loop is that there's inputs and outputs at every stage of this game and you're going to see what i mean in a second but remember our setup we want to be able to feed this thing a table conditions and field and then something is going to aggregate that so let's just worry about the inputs first so the first input i'm going to create is a table input so let's just call this table we're going to give it the label table and then it will automatically create a name for us and what type is it going to be it is going to be a type called table name and we're going to make that mandatory because none of this will work if it doesn't know what table to do in aggregate against let's create another input we're going to call this conditions this is what i love about inputs on the actions is that it's basically any field type that's in the system and there's such a huge variety and one of those things is a condition field so all the condition builders you've seen on your list view and your business rules that's available to you as a variable type in the actions sweet so we're going to take conditions that's also going to be mandatory uh the trick with conditions though is that we want them to be dependent on the table that we select there's no sense putting conditions and trying to look for conditions on locations if the table that you picked is a task table so let's hit this little down arrow here to toggle the advanced options and we can make this dependent on another input and we're gonna make it dependent on the table input all right the third input we're gonna need is field and this is where um i really wish it could have been a graphical field selector but i just haven't found a way to do that in flow designer if you can find a way please put a comment down below or reach out i would love to have you on a video so you can show the world how you got that to work but we're just going to say field it names it field what type is that going to be it's going to be a string and it's going to require that whoever invokes this action knows the database name of the field that they are called and this must also be mandatory because we need it for the calculations to work now we have the inputs we're ready to go to the next step all right so the next step of flow actions is um steps and the way you invoke a step is you see this little plus sign between inputs and error evaluation we're just going to click a plus sign in there to add a new step and you will see that there is a lot of different possibilities in here highly recommend you just explore this and think about the possibilities of what these actions can be built to do you can also chain these together so a single action can do multiple steps again this is going to be a simple action we want a simple step with maximum control so we are going to pick the script step here we go with the script step i highly recommend that you change the name of this step to give somebody an idea of what it's actually doing so i'm going to just call this aggregate script don't need to worry about its required runtime environment because you can have it pass through mid servers all kinds of cool stuff but we're just going to do a simple instance now this action is asking me for inputs but didn't we already declare inputs back in the input section yes we did but you have to think about it this way every step of the flow action might need something different and so on step three of a flow action you might need an output from step one or two so it's not necessarily the same outputs that we defined at the start in this case it is but in your adventures maybe you'll find a case where you need an input that's the output of a previous step so what i'm going to do to make this super simple is just match the inputs here so i'm going to create an input i'm going to call this table and its value is going to be the table from our input variables i'm going to create another variable this one's going to be called conditions and you guessed it we are going to drag the conditions value from the input variables the same name i've had some trouble dragging and dropping these conditions so don't be afraid to use this data pill picker let me go to inputs conditions there we go i'm going to create a third variable it's going to be called field and you guessed it we use that data picker go to the inputs and pick the field input variable so now the inputs to the script step in our custom action are being populated by the inputs to the flow action all right now let's get down to the scripty bits and you just imagine in your world you are building this with your scripting knowledge but then everybody else after you doesn't have to have scripting knowledge to get this done this is a beautiful thing about it and let's take a look at the code so it wraps this step in a function and well it has two parameters inputs and outputs this inputs parameter is coming from the inputs of the step not the inputs of the flow that's why we went through the trouble of matching the inputs of the step with the inputs of the flow so this inputs is grabbing this object's data the first thing we're going to do is declare a couple variables here i'm just going to copy this from another script that i know is working so we're going to declare a variable of input table and that's going to pull from inputs object right which is coming up here and pull the table variable same with input conditions is going to be equal to input.conditions.tostring now if you've taken a close look at business rules before and using stuff with condition builder there you'll notice that when you write a business rule it saves the condition as an encoded query so we're coming in here and we're saving that conditions field to a string and that's just to make sure the servicenow knows hey just output the encoded query don't think that there's any hard carriage returns or anything in there and i i did have some trouble with it interpreting character turns so just it's a good idea to force this to a string we're also going to declare a field called input field and that's no surprise going to take the field variable from the inputs that we declared on this step okay so if you've ever built a glide aggregate before the next step is going to be pretty obvious if not i'm going to have a link in the description for how to do glide aggregates we're declaring a variable called a g is going to be a glide aggregate object a glide aggregate object requires a table where are we going to find a table from ah yes the input table variable that we just declared earlier so we're going to drop that in here the next thing we have to do is find out the conditions that we're going to conduct the aggregate on where we're going to find conditions of course we've gone all through all that trouble to get the conditions into this script via the inputs of our aggregate script step from the inputs from the flow action okay and i'm i pump that into a tostring so that i can then pump it into this add encoded query so that acts as my conditions we also want to set grouping false because we don't want to group these aggregates unless you want to do a ton of work meddling around with the outputs of this script then we put in all of the aggregates we're adding an aggregate for k for all the aggregate types count some average min max and uh standard deviations uh if you want more information on standard deviation i'm gonna have a video popping up right about here be sure to check that one out standard deviation is super super awesome for finding out how accurate your averages might be okay now we need to something to do something with all this stuff so let's just put this if statement in so this says if there is a next in this ag object now we want to start pumping out outputs so i'm just preemptively saying outputs.count outputs.sum outputs.average and then again if you know about glide aggregate you're just saying get the aggregate that i just added up here so we're going to get the aggregate of count via the input field aggregative sum for the input field etc etc now i have kind of skipped a step i've gone ahead and started defining outputs but i haven't got those outputs slotted so that's gonna be the next thing that we do okay the custom action has inputs and outputs we know that already but so too does every step and we've already seen in this step where we have the step has its own inputs that we've matched to the inputs for the entire flow action but the step also has outputs and this is really important if you're going to chain steps together because step one's output might set the stage for step two and it's input variables what we need to do is make sure that our steps outputs are clearly defined at the bottom of the step is this section called output variables and what i want to be able to do is use this action and just have the sum the count the min the max all that already predefined so i don't have to use say a a transform to get that data out of it so so what i'm going to do is just create a variable here we're going to call this one count now i could output this to a string i want to output it as a decimal though because who knows how this date is going to be used maybe someone's going to take the sum total of all the risks and multiply it by i don't know the tax so we want all this to output as numbers that can be used mathematically down the road so i'm going to make that count make it a decimal now i'm going to create another variable i'm going to do this for the whole set sum that's also going to be a decimal and average that's also going to be a decimal and so on and so forth so now we have an action that has inputs and it has a step that has inputs and that step has outputs so the next thing we have to do is define the outputs for the entire action so we go to the output section and let's start defining the stuff that this action will present us create an output and we're going to call this one count not surprisingly but it's a different count than the one we defined before because this is the outputs for the action not the step we're going to make this decimals as well you can see where this is going as i add another output for the sum and so on and so forth now i'm just going to hit exit edit mode and there i've defined the outputs now i've defined the outputs but i haven't said what to put in them so we are going to take all the stuff that we defined in our aggregate script step and start mapping them to our action outputs so let's go to our count our sum our average and so on and so forth you can really see now why you have inputs outputs and steps with their own inputs and outputs because what if we wanted to have several different steps and the final output was math done on the different steps involved so um that's why all of that stuff is separated and we're going to give it a test this is why i love flow designer this test capability just can't be beat i'm going to click the test button it asks us not surprisingly for the inputs that we asked it for right so let's start off with risk and let's put some conditions in there we want the estimated cost to not be empty and we want the state of the risk to be one of pending open or work in progress and the field that we're going to go off of is cost let's run the test view the results and we see in the output data that the average is 47.43 the count is 204 the max is this the min standard deviation etc we got different numbers that looks really promising but let's double check the data so here i am on a list of risks in the thoroughly awesome next experience and let's scroll down to the bottom where i've added some list controls and the average four seven four three max a hundred thousand some ninety six seven thousand let's just check that back up with our flow designer and we see the numbers are same so we must have been successful so now that i know that's successful i'm going to go ahead and publish it and we are going to give it a real crash test now remember last time we built the automatic project status generator and we're just going to go into that flow and we are going to use the action we just created so after it does all the things that it does we are going to add an action at the end let's call this aggregator there is our aggregator showcase and look at what it's asking us for a table let's go risk conditions let's go the estimated cost is not empty and state is one of pending opener work in progress and the field that we're going to aggregate is cost now that we're done with that let's add an action to log it okay and the message is going to be sum of estimated risk cost is and we are going to go to our action which is action number nine number nine in the aggregator oh look how nice that is count some average min max let's grab the sum drop that in look how easy that is for non devs to utilize and they don't have to worry about what in the hell a glide aggregate is you've just made this way easier for people to build flows with now but let's give it a test run the test show the results go to our log and we see the sum is 967637 which is the same thing that we saw in our test of the actual aggregator so we know that this action works and there you have it folks your first custom action doing a super super handy glide aggregate utility now you don't have to invoke your devs and you can give this action out to people who are building flows who aren't necessarily pro code developers super win for you all right one last thing folks i just want you to remember that i am just like you i spent 13 years in the servicenow ecosystem i've had many many many jobs in there i know what it's like to want to get to the next level both in terms of culture and pay and whatnot whatever situation you're in if you're trying to up your game and service now i do a little bit of recruiting on the side so reach out to me at the email address pictured here and we will try and find you your next job also if you are the kind of company that is looking for servicenow talent what better way to vet the talent than somebody who has been in the same trenches i am not going to send you a pile of resumes that may or may not make sense to you not going to waste your time i will only send you the resumes that i vet personally reach out to this email address right here and we will see you on the next episode
https://www.youtube.com/watch?v=Gm80JGzAUeA