In my experience with working on Salesforce, one thing I have come across far more often is the requirement of being able to show/hide quick actions dynamically based on some conditions. When you try finding solutions for it on internet, the most popular solutions one would get are one of the following:
- Have different layouts based on your requirements and play around with the profile assignments — This one comes close to addressing it, but conditions other than profile(may be for specific users or people with specific roles) would not be possible to handle. So, this solution is off the list.
- Show the buttons anyways and display a message after the user clicks on them — Fair, but as everyone is working hard towards reducing user clicks these days, this solution seems far from ideal. Wait, that’s not it. What about the standard buttons? How will you display a message? This doesn’t help either.
And few more which hardly even come close to addressing the requirement. I too was of the same opinion till a couple of months ago until I stumbled across one possibility. When I tried, it worked wonders. Totally in line with what was expected.
With this solution, you can show/ hide any or all quick actions based on any condition or custom apex logic without any constraints.
Read along to see how this can be achieved.
Create a lightning component(not LWC) which makes a call to the apex controller to find out the list of buttons that are to be shown for a particular scenario on doInit. Let’s say the visibilities of buttons will be stored in the ‘buttonStates’ attribute.
Here comes the simple yet interesting part of showing/ hiding the quick actions. The simplicity of this solution made me wonder how come not many were aware of this workaround. You just need to iterate over the ‘buttonStates’ attribute and set the button visibility like below:
Drag and drop this component on the page you want to Show / Hide the buttons in using the lightning app builder. Since the component does not have any UI, it goes about its business silently without coming to anyone’s notice.
How good is the solution?
What we are essentially doing here is manipulating the styles of the quick action buttons by accessing them from our hidden component. This is exactly what the internet community wanted to avoid and hence they came up with the concept of Shadow DOM where none other than the component itself can alter its style or state.
So, till the time lightning components exist, this solution will hold good. Since LWCs function strictly around the Shadow DOM concepts, the above solution would not work with them.
Enjoy while you can!!:)
Although this solution does not deserve a blog post considering its simplicity, the lack of awareness among the developers in the Salesforce community drove me to come up with one. I struggled hard to fill in the contents for this post;)