logo

NJP

ServiceNow Secrets: Effortlessly Cancel ServiceNow Workflows Using the Workflow API - Learn How!

Import · Oct 31, 2023 · video

[Music] hi all and welcome back so in today's tutorial we'll be looking at one of the use case Okay where we want to create a UI action on the request item okay what with with the name called cancel request okay and what that particular U action will do it will cancel the workflow for that particular request and set the state for for the request item as Clos incomplete so basically this is one of the requirement a generic requirement which I've seen in lot of projects okay and currently I got it in my project also so I thought let's create video around the same so for that uh what you have to do uh let's let's let's first look into the catalog item which you want to choose okay for demo because we'll be creating this UI across all the request item then and if you want to omit it out from certain request you can you can add condition for it but we will not be doing so so let's select someu Adobe Acrobat okay let's let's s this one only and let's try to um let's just try it out we'll order now and what it will do it will create a request let's go to the request and under request we have the RM so I will open the RM out and on the RM hopefully okay so it has a catalog task also so we'll be seeing how we can close the catalog task also and uh if we do the show workflow flow attached to it okay which is at fulfillment stage now what we want to do we want to just play around this same so we have to create a a a tion on the same so how to create a reaction configure we'll go to VI action and using this particular tutorial we'll be seeing how we can cancel the workflow that's the main agenda for this particular U tutorial so we click new it's taking some time to get load because setting the values to the state is pretty straightforward so we don't have to do anything over there so we'll give it a name as cancel request canel request let's make it distractive because um it will be it will be deactivating the record right so we'll make it a form button uh it should be an sc request table active show on on update only and then we can write simply server script we don't need any Cent side script for the same right so how we can do that so you can do valuable workflow or to new workflow and what we are doing we are initiating the workflow API over here okay and in the workflow API we have a function called cancel so we will be simply using that particular function out workflow dot cancel so what it will do it will cancel the workflow now we have to specify right which particular workflow needs to be cancelled so whenever we pass a current object okay whenever we pass any object GL object it cancel the workflow whatever workflows are attached to this part that particular record so we'll be passing current over here because we want to remove it from the current recorder okay and then we can simply do current dot state now the state for incomplete is four okay so we'll be setting it as four and simply current do update so we are just what we are doing over here we are just cancelling the state out or canceling the workflow first and then we are setting the state to close in complete okay I will be showing how I came to know that it's for for closing cete okay and then you can simply put GS do in uh add info message let's add add error message okay and we can give the workflow has when cancel for the request item and then we can give current dot can display value okay or you can do current. number also anything is fine and you have to just add a so so that's it now let's try it out so we'll first save it out we'll go to history just to get get hold of the same request which we have raised and now we can see the button called cancel request over here but that means because we have set it as destructive and now you can see the task is also in open State okay let me show you that also so if we see the task is also in open state so so I not written okay why you'll come to know in a minute and how I Define the state as four so if I just use the new and if I do technical name so here open for Clos in complet it's four right so we have to set that value out so let's click on it I will just click on cancel request button now and let's see what it does so it has given the message the workflow has been cancelled for the request whatever uh request we have given and we'll just route back to the request where exactly that request was tool I'm using as a Neal command only and we'll go back to that particular request item and see if everything so the state is set to close incomplete okay let's look at to show work let look into the task first okay the task is also got set to close in complete okay so this is why I have not added any code fora because there's an out of the boxs business route what it does whenever parent request get cancelled or compl okay the child request will also get Clos inced it will follow the same state why because it doesn't make any sense right if the parent is closed then the and the child are open it doesn't make any sense that's the reason it's already an out of the works implementation and if we go to this workflow that's the important thing we don't want to make workflow worklow life right so you can see the workflow has been cancelled okay so you don't have any active C because what what happens a lot of times what Mists a lot of people do that they will set the state to certain value like close complete close incomplete close Skip and so on right but at the backend the workflow context is still on it you can see it in the active context and which is not a best practice so whenever you are closing any request out please make sure that you fls for the same so this is all for today's tutorial in today's tutorial we have seen U how we can cancel the workflow using script okay so we have a use case where we want to create a UI action or the request item okay which can which can cancel the request the state to close in complete and cancel the workflow for the same so I hope this is pretty clear to you this is pretty generic use case okay which I've seen in lot of projects okay I hope you like it out if you likeed it out please subscribe to my channel so that you will get notified of my upcoming videos till then stay safe stay happy and have a nice day thank you

View original source

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