logo

NJP

Do THIS on #ServiceNow UI Actions to save your job!

Import · Jun 03, 2022 · video

hey everyone welcome back to another servicenow toolbox my name is robert fedorick and it is so good to have you here in this episode we are going to talk about ui actions that have confirmation messages we all know ui actions can be run both client side and server side but there might be situations where what the ui action does is so sensitive that we may want to ask the user to confirm this is what they actually want to do and in that case we need a ui action that runs both server side and client side confirmation is going to happen on the client side execution of the server side script is obviously going to be server side the use case we're going to explore is really important for those of you who are involved in spm the product formerly called itbm the product the world recognizes as project portfolio management so here we are on a project this project has tons and tons and tons of project tasks on it dozens to hundreds and what organizations typically do is save these kinds of projects as templates i might have a collection of project templates and they will all build out basic project plans for me which instantiates dozens to hundreds sometimes even thousands of project tasks but project templates can [Music] be selected incorrectly and what happens if you're in the situation where you just click the wrong template and you've got hundreds of records to deal with now there's an argument that you could delete each of those records manually but that is going to be thousands of clicks yeah but robert can't you just delete the project you want me to go all the way through the demand process get that all taken care of then create a project then delete that project then go back to my demand managers and say oh we deleted the project you just create another one so that i might accidentally run the wrong temp so no we don't want to delete the project yeah but so in this servicenow toolbox we are going to be creating a ui action that will allow me to clear out all the project tasks under certain circumstances of a project and then start over again on the same project okay here we are on a ui action that i'm calling wrong template sweeper i've chosen form button form context menu and form link as the ways to execute this ui action also i've added this form style field to the ui action form this is out of the box you may not know you can have it but if i use the form style as destructive it will color that button in red and since i'm deleting records as a consequence of this button i want to make it look dangerous so we've given this ui action the name wrong template sweeper it's also critically important that we give it an action name you can think of this as what does the server-side code think this ui action is called it's also super critical that we mark this as client callable because that's how we're going to earn the confirmation from the user who's looking at the screen now if you make a ui action client callable you must have an on click function so we see the on click field here and it has an on click where i've named the function confirm template sweep that means it's going to go looking in the code part of our ui action for that function so here's a function i wrote just to demonstrate client-side ui actions the function is called confirm template sweep just like we said in the on click we are declaring a variable called answer and that is going to take the confirmation of this will delete all project tasks on this project are you sure that's what you want and that's going to return a true or false so we're going to run this if statement and so if answered that's just a way of saying if answer is true okay then it's going to alert if this was completed we would have deleted all the child tasks otherwise it's going to alert wow we almost need a whole bunch of records and we're not doing anything with the child tasks yet so let's just save this now we're on the details tab of our project console or our project form standing out like a sore thumb is our big red ui action called wrong template sweeper let's go ahead and click that now it's asking us this will delete all the project tasks on this project are you sure that's what you want if i say cancel wow we almost need a bunch of records let's try that again and use ok and now it's taking the true path this was completed we would have deleted all the child tasks so now we know how a client callable ui action works okay now let's make this ui action do something on the server side as a consequence of the client side here we are in our script we've already seen what that confirm will do it makes a true or false path for us based off of what the user clicked on the screen and in the true path we've put this gsft submit function this function has three parameters we're not going to talk in depth about the first two i'm going to link in the description below a really good community article that describes that and the third parameter is the really important one and it should look familiar it says wrong template sweeper where have we seen that before it's what we named our ui action in the action name essentially what this is doing is it's calling the ui action again is just using that action name to identify the ui action but it's doing it in such a way that ignores the on click command so we get down to the true path if answer is true then gsf submit null gform get element and wrong template sweeper is the name of the action we've got to take then it's going to run the code of the ui action the first line of code is if type of window equals undefined and what this does is just make sure that there's no other client side component of this running and if that's the case it's going to start running this code server side so cut we're going to add some work notes you'll notice there's code tags in there to make the work notes look pretty if you haven't seen my toolbox video on that should be coming up in the top right it will also be in the description below then we're going to add an info message then we're going to update the current record and just so that we don't go back to wherever we were before we're going to put an action.set redirect url to the current record that makes sure that when this whole thing is done doing what it's done it updates the record and it brings us back to this record critically important so let's save this and we're back on the project form let's click that big red wrong template sweeper button if i hit cancel it's going down the false path wow we almost need a bunch of records if i take the true path by confirming okay we can now see that it's added that gs info message right don't worry we didn't delete any records yet and if we go to the work notes section we'll also see that the work note got put in with big bold text so now we know with that gsft submit function we can do stuff on client side and server side in the same ui action now let's wrap this all together and make a complete solution here i am back on the ui action and let's paste in our main final code so just like before we had our function to confirm the template sweep that's going to run on click because it's a client callable ui action it's going to ask us to confirm this is going to delete all the project tasks on the projects are you sure this is what you want if we take the true path it's going to use the gsft submit function that we just finished talking about and it's going to call wrong template sweeper which is this ui action that's going to run the main code we're already making sure that the type of window is undefined so we're not running any other client functions on this ui action then we are going to declare a variable called task deleter task deleter is going to call a function called delete project tasks now the reason i could just put that code right here but i like to break stuff up into functions so that basically the code does one good thing at a time so it's going to call delete project tasks and it's going to feed it the current society current being the project that we click the ui action on once we're in that function it's going to receive our current sys id as top project we're going to declare a counter variable we're going to create a variable called prjtasks that's going to be a glide record for the project tasks table and we're going to add a query where the project tasks have a parent of top parent where we get top parent from we get it from the sys id that was fed from when we declared the variable task dealer okay then we're going to set counter to the project tasks.getroc so we know how many of them that we're dealing with then if there is any records in that glide record to speak of it is going to delete multiple delete multiples a nice function it can delete everything within that glide record that we just got then we are going to return the counter so we know how many we deleted now just a side note on the way projects work projects have functions built in servicenow that automatically delete subtasks so i'm only deleting the very very top level project tasks i trust servicenow more than i trust myself so i know when i delete those top level project tasks the ones that aren't projects it will cascade downward effectively and do everything it needs to do so i'm only deleting that top layer it's going to take care of itself all right so task deleter goes and runs that function it uh it will have the counter so it knows how many it deleted now we're going to load that into our work notes again we're using the codes so that we make it nice and formatted then we're going to add an info message that says it successfully ran then we're going to update our project then we're going to set the redirect url let's save that then let's go to our project we're gonna go to our planning console just to verify that we do have a bunch of project tasks just waiting to be deleted then we're gonna go back to our planning console just to make sure that we know that there's a bunch of tasks here to be deleted so i have six top level tasks and each of those top-level tasks have child tasks underneath them so plenty of stuff to be deleted back on our details tab we have the big red wrong template sweeper let's go ahead and click that it asks us to confirm we know what cancel we'll do let's see what the big ok button will do this can take a while as it processes all the deletions then comes back with info messages and a screen refresh so we see that we successfully cleared all project tasks by the incorrect template you may now apply a new template let's check our work notes template sweeper was run and deleted six records we're double checking on the planning console and we see that it is absolutely free of project tasks so there you go folks in five minutes you've just learned how to create ui actions that do server-side stuff and also ask the user for confirmations you can get way more complex than this too you can actually ask them to provide specific data you can do all kinds of form validations and don't ask them anything um sky's the limit the thing is you just have to understand how that gsft submit works and then structure your ui action accordingly hope you found something useful out of this and we will see you on the next one if you're a servicenow expert looking for better opportunities but maybe your resume or linkedin profile isn't doing you justice reach out to me via linkedin or the email pictured here as i offer both career coaching and recruitment services and if you're a servicenow customer or partner you heard that right robert fedoric now does servicenow recruiting with a 1 500 subscriber youtube channel and mailing list and thousands of linkedin followers let's make sure your open positions get first go with the prodigious pool of servicenow resources reach out via the emailed picture here

View original source

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