Update parent from child record with #ServiceNow Flow Designer
hey everyone welcome back to my channel it is so good to have you here in this go with the flow we are going to talk about updating a parent record based off of updates to child records and the specific use case i'm going to talk about here is demand task in demand management a demand can have multiple demand tasks associated with it but there's no functionality around the updates to the demand tasks so you could be having work notes being updated the demand tasks being reassigned the demand tasks transitioning through different states and the demand manager has zero visibility to this because there's nothing in the demand task that notifies the demand manager in any capacity so we're going to use flow designer to fix that i've gone ahead and created a flow called update parent demand from demand task and the first thing we're going to need to do is figure out the trigger i'm going to click add trigger i only want it to happen on updated records what updated records demand tasks demand task we also want this to run for every single update so i'm going to check to see every single time if significant things have changed so every update and i don't really have any conditions i could put a whole bunch of ore conditions to make it so that it triggers less but for this one we're just going to say every update and we'll let our if conditions handle it for us okay since i've already done some thinking on to the best way to build this what i want to do is create some variables to store some information for me the first thing i want to know is what the parent of this demand task is yes that's readily available to me by drilling into this trigger record demand task and scrolling all the way to parent but you know what scrolling down that list sucks i just want to go somewhere grab it and drop it wherever i need to so let's go to our flow variables and i'm going to add one this is going to be called parent demand and it's going to be a reference to the demand table what i also want to do is take all the significant updates from this demand task and i want to put it into a work note on the demand parent so i want to create another new flow variable and we are going to call this parent work note and it is going to be a string okay i've got my flow variables defined let's get into it so my intent here is to take all significant updates and push them into one work note for the parent demand and this is the trick because in our trigger record updated there is this changed fields object and it stores all of the fields that have changed the thing about it is there's going to be multiple of them so what we have to do is basically make a loop to iterate through all the field updates grab the ones we think are significant and do something in those cases so the first thing is let's loop through them we are going to do that with a for each flow logic so for each what for each all of these changed fields from our trigger record i could have just updated work notes i could have just updated the assigned to i could have adjusted the work notes the assigned to the state the priority the urgency blah blah blah blah blah all at once so this is going to make sure that we look at every single one of them now that we're inside of that loop now we can start putting if statements like did the assignment change did the work notes changed this allows you to be pretty exclusive what you think is a significant change in this example i'm only going to do assigned to and work notes let's start off with work notes so we're going to go to our flow logic and click if and this is going to be if work notes changes and we will know if work notes changes if this item in the change fields array is named work notes so we're going to grab our fd changed fields and the field name is work notes that's how we'll know that there's been a change to it what do we do when we know that work notes has changed let's dump that to the big work note variable that we're gonna push to the parent demand so we are going to go to flow logic and say set flow variables we are going to take the parent work note and this is tricky because we want to take the parent work note and basically say whatever the parent work note was before plus this update because again there could be multiple significant updates if the assigned to changes if the state changes if the work notes changes we want those to be all in a list in the work note we update the demand with so the way we're going to do that is we're going to enter this code section and we're going to let's see can we blow this up big so we're going to return whatever the parent work note field was plus something else so how do we know what the parent work note was so this is where we're gonna go fd underscore data this is basically a code way to interact with your data section here so fdata dot and if you just be patient it'll tell you the things you can dot walk down to so i'm going to go to flow variables dot i'm going to wait parent work note great so it's it is whatever it was plus equals which means add on to whatever it was work note update and we're going to add this slash n that makes it do a hard carriage return inside i'm going to say plus now we need to know what that current work note value is so we're going to say fd underscore data again dot something from that for each plus item plus current value and then we're going to add a couple more character turns okay so i'm in the middle of recording i realized i made a mistake but it's super valuable for you to see that mistake and its consequences so actually the first thing i wanted to do was take our flow variables and set whatever the parent demand is so let's go to our add actions flow logic and let's set flow variables and let's take that parent demand and set that equal to the triggering records demand task records parent okay the thing about it is i wanted that done beforehand so let's move that up to the top that's now flow action one really inconvenient because in the last couple minutes i showed you that we set flow variables and we did it via script but if we go into there that script hasn't changed and you know what this flow data dot underscore one that is referring to this number one and it's not there anymore it's actually that four each that we're referencing here in the script is actually number two so i gotta change this to two so if you're doing any scripting at all in your flow designer if ever you're changing the order of the things in the flow you got to be really cognizant about anything that you've done in the script that references a different flow step really super important i'm glad i made that mistake so that you can see how to fix it at any rate now we've gone and set parent demand to the demand task records parent and now we've gone through all the change fields and we've asked has the work notes changed if so set this parent work note variable to whatever the parent work note variable was plus that work notes uh text okay the next thing we're going to ask is if the assigned to has changed so we're going to create another flow logic if assigned to changes how did we do that before again we went to our for each and we need to know if the field name is assigned to now what happens if the assigned to changes we want to make an update to that parent work note so we're going to add a flow logic item we are going to set flow variables again we are going to set the parent work note field and now it should start making sense right because with each iteration if there's a significant field that changes we want to add to not replace the value of that parent work note variable so let's get back into our script and let's say return so we want to take whatever the parent work note variable was plus so we need fd underscore data dot flow variables dot parent work note and we want to make plus equal assigned to and then we'll just add in whatever the new assigned to value is what is that new assigned to well let's just go fd underscore data let's go to the for each loop which is where we're going to get this change thing we're going to go to the item within that workflow node plus a dot and this time we're going to use current display value because we know assigned to is a reference we don't want a cis id we want an actual name okay so we're looking for significant updates if those significant updates happen we are dumping stuff into the flow variables now what do we want to do we want to update the actual demand so let's go to add an action we are going to update a record which record are we going to update the parent demand we're going to drop that into the record automatically knows what table it's from now we're just going to add a field i'm going to update work notes and what we're going to upd update work notes with we're going to update the parent work note let's save our flow and let's go ahead and test it we're gonna run a test i'm just gonna pick this 1105 demand task now this is something you might not know when you're testing the flow that's running on a record update you can actually fudge the updates with this changed fields which i think is awesome so fd field changes details number one we are going to change the work notes field and we are going to put a current value of taxation is theft i mean it kind of is then i'm going to add another change fields the field this time is going to be assigned to we're going to set its current value to the society of a user that user just happens to be yours truly and let's run the test let's view the results okay let's dive deep here because we have a four each and we see one of two that means two things changes isn't it convenient that we changed exactly two fields now we'll see that the first if statement if work knows change not evaluated true but the second one didn't and why wouldn't the assigned to changes have evaluated to true if we change the assigned to well that's because it's a second item in that array so if i said this is two of two we'll see that the work notes changes didn't get recognized but the assigned to changes did this is why when we scripted our parent worknotes variable we had that thing that said whatever the parent variable once was plus this new update so no matter how many fields we change it just keeps on adding two instead of replacing we also see that the update record happened successfully the proof is in the pudding though let's actually go to that demand open the record and we see that the work note update is taxation is theft and the assigned to is robert fedor there's next plus sign in there probably just a syntax error in my point that's where i go back into my flow designer and see what i did wrong but there you have it folks whenever you have a task hierarchy where you have child tasks and a parent task and it's not obvious to the parent when the child has been updated you can just use flow in a very simple and effective way in probably less than 15 minutes you can be up and running with exquisite rolls up of information into that parent task hope you enjoyed this and i'll 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
https://www.youtube.com/watch?v=LK5XnFFhH-A