Account Hierarchies in Salesforce are fantastic! They allow you easily keep track of various relationships between your Accounts; from grouping various branches of a store together, to logging various subsidiaries under one legal entity. However, when it comes to automations, they can be tricky navigate when you have more than one level of hierarchy and start moving into the realms of grandparent and great grandparent relationships.
Let’s take an example (all of the examples used in this article are hypothetical use cases); Creative Crayons sell their crayons to various supermarkets, each of which have their own Account and child Accounts underneath them (in this example we will only be looking a 1 level of hierarchy, but this solution is scalable to multiple levels). Creative Crayon’s Account Managers would like to easily be able to identify the highest-level of Account in an Account Hierarchy directly from any Account.
Their System Admin, Juan, has already created a Lookup to Account on the Account object called “Highest Level Parent Account”. However, Juan wants to ensure Creative Crayon’s Account Team are always doing what they best, supporting their accounts. They know that making the Accounts Team populate this field manually every time they create an Account would be time consuming and wants to automate this process in a way that is not only quick, but scalable to any level of Account Hierarchy. Don’t worry Juan, we’ve got you covered!
Create your Record Triggered Flow
We have a couple of options here, we can either run this automation off the creation of a new Account, or when the Parent Account field on an Account is updated. Both scenarios would be built out the same, however, the entry criteria would differ. Let’s go with when an Account is created in this scenario.
Create a flow on the Object we would like to store the Highest-Level Parent Account on. Juan will be using the Account object in this scenario and will run the Flow when an Account is created and the ParentID is not empty (Is Null = False). This ensures the flow will only run when an Account is created that has a Parent Account associated to it.
Create your Variables
There are a few variables Juan will need to create in order for them to run this process. Below is each one and what they will be used for:
- varParentAccountID (Default Value = Record.ParentID)
- Text Variable
- Used to store the ID of the Account’s Parent Account we will be checking
- Text Variable
- varFinalParentID
- Text Variable
- Used to store the ID of the Final Parent Account we will be associating with the Account
- Text Variable
- varParentAccountFound
- Boolean Variable (Default Value = False)
- Used in our flow decisions
- Boolean Variable (Default Value = False)
Get the Parent Account
Using a Get Records, we will be getting the Parent Account of the Account that triggered the Flow. By doing this we will be able to determine if this Account has a Parent Account. This will for the foundation of the flow.
Here, we are going to get the Parent Account of the Account that Triggered the Flow. As we are defaulting the varParentAccountID to Parent Account of the Account that triggered the flow, we can use this to get the Parent Account.
By using the variables we created before, we can now use a decision to progress through the correct next step in the flow.
Check if a Parent Account was found
We are going to use 3 decision paths based, all based upon the Variables we made earlier on in the flow. I have broken down each path below:
Yes – Parent Account Found
- Id from Get Parent Account Is Null = False
- When the Get Parent Account returns a record, this branch will trigger
No – Parent Account Never Found
- varParentAccountFound = False AND
- Id from Get Parent Account Is Null = True
- We will be assigning the True value to the varParentAccountFound Variable if we find a Parent Account, so this will only trigger if we have never found a Parent Account
No – Parent Account Found Previously But Not This Time
- varParentAccountFound = True AND
- Id from Get Parent Account Is Null = True
- We will be assigning the True value to the varParentAccountFound Variable if we find a Parent Account, so this will only trigger if we have previously found a Parent Account
Assign our Variables
Depending on the outcome of our Decision above, we need to assign some values to our Variables. This flow relies heavily on all of our Variables being correctly assigned, so this step is crucial! Broken down below are the values that need to be assigned to each variable for each outcome from our decision:
Yes – Parent Account Found
- varParentAccountFound = True
- Ensures we will hit the Parent Account Previously Found decision going forward.
- varParentAccountID = GetAccount.ParentID
- Ensures we are updating the varParentAccountID variable with the Parent ID of the Parent Account we just got.
- This will keep the flow running up the hierarchy and ensure we are able to always get the highest-level Parent Account.
- varFinalParentID = GetAccount.ID
- This is what we will use to populate our “Highest Level Parent Account” at the end of our Flow.
- It will always be set to the Account we found in the Get Parent Account.
After the Variables have been assigned, we are going to connect the Assignment back to the Get Parent Account and the process will start again, this time, getting the higher-level Parent Account. This process will continue until the Highest-Level Parent Account is reached.
No – Parent Account Never Found
- End Flow
- We never found a Parent Account, so there is no need to do an update and Populate the Highest-Level Parent Account field.
Update the Highest-Level Parent Account
As soon as we find a Parent Account, we will be changing the varParentAccountFound Variable to True, meaning that by combining this with the Get Records, we can keep going up Account Levels in the Account Hierarchy until we reach the top.
In the Assignments broken down above, we are always assigning the Parent Account we found to the varFinalParentID Variable. This is updating each time we Get a Parent Account meaning that by the time we reach the top of our Hierarchy, this Variable will have the ID of the Highest-Level Parent Account stored in it. Perfect. This is exactly what Juan needs to ensure they can update the “Highest Level Parent Account” field on the Account that was just made.
By using an Update Records, Juan is now able to Update the Account that triggered the flow with the Highest-Level Parent Account that was found!
And there we have it! Through the use of Variables and Assignments, Juan was able to create a process that they can now apply to any sector of Creative Crayon’s Salesforce and always ensure a record is associated not only to the Account it relates to, but also the top-level Account in an Account Hierarchy; perfect for Creative Crayons Account Structure!
Thanks for reading,
Joehenry Cummings
The Tether Team
#salesforce #salesforcecrm #tethertips #tether #flows #ids #hierarchy