Flows | Use Hierarchy Custom Setting in a Lightning Flow

In this blog, I am going to show how to use Hierarchy Custom Setting in a Flow. We will see how to avoid Hard Coding values in a Flow which frequently change. Also, we will see how to maintain different values for a same parameter for different profiles/users using custom settings.


Main Takeaways from this Article

 1. How to use Hierarchy Custom Settings in Flow

 2. How to pass Record Details (not just IDs) from Record Page to a Flow (Summer'20)

What is Hierarchy Custom Setting?

Hierarchy Custom settings are a great way to avoid hardcoding of parameters/ attributes which often require changes and have different values for different users or profiles.

----------------------------------------------------------------------------------------------------

Tip: On a high level, a Custom Setting is just like a Custom Object with fields and data. The main difference is that they are mostly used to store data which is used across the org in different places like Flows, Apex, Workflows, Validations etc. and also if this data is dynamic in nature i.e changes often
----------------------------------------------------------------------------------------------------

For example, while working on Opportunities you want that a particular profile’s user can provide a discount of 10% while some other profile users can give 20%. But these discount values can vary from time to time and are not fixed (Remember the Festive/Holiday Sales). So, in order to avoid changing our Flow logic every time these values change, we should not hardcode such values inside our Flows. Instead, we can use Hierarchy custom setting to fetch these values and our flow will fetch the latest value at that point of time. Also, the value which Flow will fetch will be different for different user. These values can be user specific,Profile specific or org wide defaults.

----------------------------------------------------------------------------------------------------
New to Custom Settings? A bit more about Hierarchy Custom Settings. 

This is for you if custom setting concept is new to you. If you are familiar, you can skip this & jump directly to the sample use case section below.

Custom settings are essentially custom objects that are exposed in the applications cache and are accessible via their own API.

Hierarchy Custom settings allow you to personalise your application for different profiles and/or users. The interface has baked-in logic that drills down into the org, profile, and user level (based upon the current user) and returns the most specific or lowest value in the hierarchy.

Most unique feature about Hierarchy Custom setting is that we can have different values at Organisation level, profile level and user level.

In case you are not familiar about custom settings and want to understand more please refer these two great articles:


----------------------------------------------------------------------------------------------------

Why should we use Hierarchy Custom Setting in Flows?

Scalable Flow Design - We can anytime change values or add new values (profile specific and User specific) in custom settings and no change will be required in Flow if we get these values using custom settings. Based on profile/user, values defined in Custom Setting will be automatically fetched by the Flow.
This leads to better Flow Maintenance as we avoid hard coding stuff inside the Flow and make it dynamic.

Sample Use Case and Requirement

Let’s consider we have users of different Profiles that work on Opportunity.Consider the names of these Profile are Standard Sales Profile and Sales Manager Profile.

Users with Standard Sales Profile can give Maximum Discount of 15% known as Silver Discount Level.
Users with Sales Manager Profile can give Maximum Discount of 20% known as Gold Discount Level
All other Users/Profiles (Organisation Default) can give only 5% discount known as Basic Discount Level.

A Screen Flow should be added on Opportunity Record Page so that logged in user can see the discount value allowed. And also, on click of flow button, discount should be automatically applied on Opportunity Amount and it should be updated.

Point to note in requirement : These discount values for different profiles/user may change/vary from time to time. Also, new profiles or users can be added with their own specific discount values. 
For example, here's a scenario: Next week/month SalesManager can give only 15% and Sales User can give 5% discount. Also, we may add another profile that cannot give any discount.

This kind of requirement is the main reason we should not hardcode in Flows and use custom settings.
----------------------------------------------------------------------------------------------------
Useful Tip: Even if there is no profile wise or user wise value requirement for some attribute, we can still use these custom settings for scenarios where we need to use some org wide values (like some text, email or number) within Flows that may change in future. In that case just simply create an org default value only in custom settings.
-------------------------------------------------------------------------------------------

Please Note: In order to try and showcase a Specific Use Case, I may have overlooked some of the best practises related to Flow Development in this article. Please make sure to follow these in real world scenarios and projects. Check some really important ones below.


Solution and Design

First, we need to create a new Hierarchy Custom Setting in the org.

Create a new Hierarchy Custom Setting 

We will first create a new Hierarchy custom setting to store values of allowed discount values and discount levels for different profiles. A custom setting is almost like a custom object. It can also have custom fields.

This Custom Setting will have two Custom Fields named “Discount Allowed” and “Discount Level”. Values in both these fields will be set for the two profiles (Sales User and Sales Manager). Also, an org default value will also be set for these two fields which will work for all other profile users who do not belong to either of these two profiles.

Checkout the video to see how I created custom setting for this requirement.

Points and Values to Remember : We need to use below details in the Flow later so need to remember these.

Custom Setting Name : Opportunity Discount (API Name: DiscountOpp__c)
Field Names : Discount Allowed(Number) & Discount Level (Text)

So, by now we created a new Custom Setting.This custom setting has two records for two profiles and one default org wide value.

Value in Fields for Standard Sales Profile


Value in Fields for Sales Manager Profile


Default Org wide Value (For all users who do not belong to either of the Sales Profile)



How can we use Hierarchy Custom Settings in a Flow?

There are currently two ways to fetch/access the values of Hierarchy Custom Setting in a Flow.

1. Using a Formula Variable : After selecting a new Variable of type Formula, we need to go to the $Setup option from the Insert a resource input tab.This will show all the available custom settings in the org and we can select a Custom Setting name and a particular field for that custom Setting. We will create two formula variables. One each for two fields (Discount Allowed and Discount Level).

Check screens below:


Note: For each field within the Custom setting we will have a different option to select. You can watch the Flow video below to see full steps to create these formulas.

2. Using Get Record Element: We can also use Get Elements to fetch the values of all the records and fields in a Custom Setting.This is just like fetching any other object's data in the Flow. 

----------------------------------------------------------------------------------------------------
Tip: Custom setting can be treated as an Salesforce Object in this scenario and field values for this object can be used as you use any other object within Flow.
----------------------------------------------------------------------------------------------------


Note : I am using a Formula variable to get custom Setting Data in this example. 

Flow Design and Steps





This Flow will have below main Components:


Record Type Input Variable to get Opportunity Data : Create a new Variable of type Record and object Opportunity and mark it as available for input. This will be used to receive the current opportunity details from record page to Flow. 


----------------------------------------------------------------------------------------------------

Note: This is available from latest Release Summer'20. If you are using any older org, you need to create a text Variable and mark it as available for Input to pass Record Id from Record page to Flow and then use a Get Record Element in Flow to fetch the other fields from this Id.
----------------------------------------------------------------------------------------------------

Formula Variable : We need to create Formula Variables to fetch the Custom Setting values. Formula variable’s value will be automatically be equal to the value defined in custom setting for a particular profile or user. This value will depend on which user is running the Flow.

We will create two new Formula variables. One to store Discount level(Text) value and one for Discount Allowed (Number)

----------------------------------------------------------------------------------------------------

Note : Flow will automatically recognise the logged in user and user’s profile and based on the value defined for that profile in the custom setting, this new formula variable will hold the value. For example, if Sales Profile Manger is running this flow, the values in these two formula variable will be 20% and Gold Discount.
----------------------------------------------------------------------------------------------------



Screen Element: Create a Screen Element to Show these two formula values to the User in a text format on the Screen. In this element I am showing the two Formula Variables created in the above step using a Display Text Element. Also, when user will click finish, we will update the opportunity amount by applying the discount.



Assignment Element: Use an Assignment Element to calculate the final Amount after the discount valid for the logged in user  is applied and change the value of the Amount field of the input opportunity. 

For this we will create a new Formula type variable to calculate the Amount and assign that Formula to the Amount Field of Opportunity .


Formula Variable : For Assignment Element calculations as shown on right hand side in the above step.

Update Element: Use Update Element to Update the Opportunity. This doesn’t require any new variable as we already received the Opportunity in our input variable and we will just update that directly.

Checkout the Video for all the steps of how I created this Flow. 


----------------------------------------------------------------------------------------------------
Important : If you note in this video, when I debug the flow, I get the Allowed Discount values as 5% and discount level as Basic. These values are coming as per org wide default as my profile was neither Sales User Profile nor Sales Manager Profile.So by default, for my user default values were selected in the Flow.
----------------------------------------------------------------------------------------------------
Add Flow to Opportunity Record Page
Now, we just need to Add this Flow(after Activating) to the Opportunity Page. We can just edit the opportunity record page and select Flows component to add and place that on the Opportunity Page screen. 
----------------------------------------------------------------------------------------------------
Important : One thing to note is that while we add this Flow to an Opportunity page, we need to select the checkbox true to send the opportunity details to our input Flow variable which we created as a first step for our Flow.
----------------------------------------------------------------------------------------------------

Below is the Demo of the Flow Screen for Sales Profile User. For this user, Allowed discount is 15% and level is Silver. In this video, I am logged in as a Sales Profile user.





----------------------------------------------------------------------------------------------------
Tip: Once we fetch Custom Setting values in a Flow, we use it anywhere within the Flow for any kind of calculation or automation. This is a very powerful feature and help in better maintenance of the Flows. This can be used for many different use cases and requirements.
----------------------------------------------------------------------------------------------------



And we are Done!

Like this? Don't Miss out on my future Posts. Please Subscribe!


Enter your email address:


Delivered by FeedBurner
Please share #Sharing is Caring


Comments

  1. Please leave your feedback or queries here!

    ReplyDelete
  2. Hi - The videos don't work - would love to watch it for additional info! Thanks for the great walk-through of custom settings to flows!

    ReplyDelete
  3. Thank you - The videos are working now!

    ReplyDelete

Post a comment

Thanks for your Feedback!


Never Miss Latest Posts