Trigger a sub-flow via a script | FlowAPI | Flow Designer | Use-case | ServiceNow
hello everyone in this video i'm going to consider a use case where i'm going to take a flow using the scripts so we have a couple of out-of-the-box available uh apis which can be used to trigger a sub flow via a script or to cancel a flow as well so the use case would be something like i have a field on my incident form which gets sent to a particular date and when that particular date is reached then i need to send out an email notification to the person to whom the ticket is assigned to so let's design a flow first and then we'll jump on to the script part to trigger the same so first of all i need to go to my flow designer here i am in the sub flows tab and i select new sub flow let's say i need to trigger this on the resolution date which is being set on the form let's select this is system user click on submit so since this is not a full-fledged flow but a sub flow i need to give pass certain inputs and outputs over here so considering the use case which i just said the first thing which i need is the email to which i need to send an email notification so let's say the variable name is also email and i'll mark this as mandatory and the second variable which i need is the date on which i need to send the notification so let's change the type to date time and i'll make this one as well mandatory now the next part is to wait for a certain duration of time and that duration time is the date in the future so i will select this flow logic and the duration type i would select is relative i need to wait zero hours after i due date which means immediately after my due date i need to send this email based on the organizational setup that you guys might be having you can also select a particular schedule i don't have one so i click on done for now and then i need to send out an email that can be done in three days first is if i don't have those fancy emails in my organization i can simply write the one liners over here saying that hey this incident is pending at your end please take certain action if they are out of the box or maybe your organizational specific email notifications that you have formatted which uses certain mail scripts in the backend so you can use the send notification option and select the notification from the lookup and the third option is trigger and event from this particular flow based on which a particular notification would be triggered so any which ways can be selected for now i'm going to select send email i will simply add a one liner and and into i need this email which is available in the inputs subject is let's say please take action and take it and here anything can be added resolution please take my cc action all right let's click on done and i think i'm done with my flow let's save and publish this one and by the way this is the new feature in rome release error handler if you're expecting that an error can occur in your flow you can simply add another logic over here so that it would be handled in the best possible manner and while this is getting saved let's jump on to our business tools to trigger this flow yeah this is published now let's click on new over here let's give it a name solution now this is advanced and i want to trigger this before update and now coming to my script part let's do it available first to store the context id and then i am going to use the script so we have out of the box available flow api which contains a function named start sub flow this accepts two parameters first parameters is first parameter is the name of my sub flow and the second is the inputs that we need to pass to the subfloor this is the input part which we have already configured so now coming to the name of my subflow i can simply copy it from here this is the name internal name actually so first the parameter over here is the name of the application and then the name of the software now my inputs is an object as you already seen while i was designing the flow there are two parameters that i need to pass first one is the email so i can easily fetch it from client or assign second is the resolution due date resolution due date is a custom field which i have created just for this loop demo so now i have passed the email and the due date and i have written the script so that the flow is triggered now we can also add a custom field to the form which stores the value of the flow stating that okay this is the particular flow and it is running for the incident record so that if we have to find it it becomes quite easy for us so this is the field resolution flow context and i can simply pass the context id since i'm using before update i don't need to use current dot update in this case okay so i think this is pretty much it let's save this record now next step is to test this out and i am going to navigate to incident let's create a new one and i will also open the emails from the system log in the new tab let's see caller is able to demonstrate and this is my resolution due date and let me save this incident record so by default it is being set to a date after three days so i have written my business soon for before update so let me update this to the same date and i'm going to change it toward time after let's say one minute and i'll save it again and after a minute there should be an email in my email log stating that whatever content we wrote in the flow designer for the email meanwhile let's go to the context and see if my flow has actually triggered or not so it looks like it has not okay so the possible reason for that is what are the conditions we gave this should not be empty and this should not be empty as well so let's assign this ticket to a user okay hr admin is working on this ticket now [Music] i have modified the time as well to be after five minutes and we check now whether my flow is executing or not um looks like there is an issue which is not us to start the flow so let me add some logs in my business rule and for this reason it is always recommended that we include our code within tie catch blocks so we'll do that now okay then goes my cat's block okay so if there's an error we'll come to know looks like i've already found it so this is not google but global human error so i'm going to save this and we trigger so this is the incident that i created [Music] if i save this record now i'm expecting that a flow should be triggered because now my script is all right yes so you can see that the resolution flow context field is updated with the flow and we can also check that particular flow in this list let me add a few columns over here which will let us know the origin of this particular flow whether it is related to an xyz table or record or not this is actually not true so we need to populate this source record and so stable as well while we are starting the flow via a script so let's go back so now here what i'm going to do is i'm going to glide through the flow context table and update the source record ideally i won't write this code in a business rule i would write this in a script include and trigger the script include function over here the name of my table is flow context get it and context id is actually the ssid so the field name is source underscore table and i can get the table from get table name and again the other field is source record and you can here i can pass the current 30. and i'll simply call the update function then and meanwhile you can also check whether it is still waiting or it is executed it's waiting and here also i can go to my subflow and click on the executions to check how many times it has been executed now since i made certain changes to the script let's go back to the incident and change the resolution date again so i'm going to make a 21 now i expect two sub flows to be triggered and both are in executing state see so this part is populated correctly now source table is incident and the record is this one but here's the problem when my source when my resolution date was changed or whether if i change or sign to as well either of them changes it triggers a new flow again and the existing one is also executing so in such cases we need to make sure whenever a new flow is started we are cancelling the existing flow if it exists okay so we'll see in a second how we can do that um so there are a couple of ways this can be done uh first is i can simply go to my business tool the existing one and i can add if else condition so that can be done somewhere over here before i call this function so let's see how we can make use of that so if g is nil and here i am going to pass so if it is not empty then first this piece of code whatever i'm going to write here will execute so if the flow is already complete i don't want to do anything let it stay complete but if it is executing whether it is waiting for something or maybe it is in progress then only i need to cancel the flow so [Music] i'll copy this piece of code again in usual cases i won't write this code in the business rule i would be using a script include but just for this demo to wrap up things quickly so [Music] you can simply pick up this and pass it over here and here i can check if the existing flow is in waiting state oh it is important so i can again simply use the out of the box flow api and this function called cancel which again accepts two parameters so first is the name of the flow and the second one is the any comment you need to pass for example triggered and here i'm going to pass the first parameter which is sorry not the name but this is id so or i could also pass this current dot resolution flow context so what else i need to do i can once the flow is cancelled i can simply is empty so let's save this and create a new incident again start pencil okay okay let's select and assign to save this incident nothing triggers on insert so i'm going to update the date to the same date and after a minute so resolution flow context is not empty there exists a flow let me refresh this list and yes this is the incident number 423 so matching so it is waiting for something and let's say if i change it to 30 instead of 29 my existing flow should be cancelled and new one should be the only one that is currently executing let's refresh the list see the existing one is cancelled and this one is in waiting state let's wait for a minute for the notification to be triggered while we are waiting so the assigned to user is glumu and if i go to the emails so i have an email triggered and i can also verify the content by clicking on preview email so flow works perfectly fine now let's verify one more thing so this is in complete set now and if i make any changes over here the previous one should not be cancelled let's save it and this is my flow context fish the list so there is a new one for this particular record but the existing one since it was already complete it is not cancelled so this is pretty much it and this is how we can trigger a sub flow via scripts or cancel it uh this is just one such use case but the multiple other use cases that can be followed and it's well everywhere thank you so much for watching and don't forget to subscribe
https://www.youtube.com/watch?v=Drl4_m5ROfM