Thursday, December 15, 2016

Visualforce - Beyond Basics Series 23 - Cascade Action Functions

When Action Functions used ?

The action functions are invoked normally from visualforce page after the page has been loaded. This could be from page init action, button action or any call directly from Custom Javascript.


What cascading action function means ?

If you have a action function associated with Button & you want to take some more action once its completed. And you could continue doing so for any number of times.

What is the benefit of doing so ?

I have seen developers cramming so many lines of code inside the action function, mostly for Button actions. Because, as the requirements keeps growing, developer keeps adding so many logic inside same action function. Instead, you can have many smaller action functions & call one after another depending on conditions.


Where is the code ?

For illustration, i have a button which will invoke a action function. While its invoking action, it also specify what is the next action will  be. So that, after the call, the next action function gets called.

public class testcls_15122016 {
public string message {get; set;}
public string nextActionName {get; set;}
public void actionForButton() {
message = 'Default Message';
}
public void messageForEvenOrOdd() {
if (Math.mod(System.Today().Day(), 2) == 0)
message = 'Good Day. Today\' day is Even';
else
message = 'Good Day. Today\' day is Odd';
}
}
view raw gistfile1.txt hosted with ❤ by GitHub
<apex:page controller="testcls_15122016">
<apex:form>
<!-- Button -->
<apex:commandButton value="Find Todays Day is ODD or EVEN" action="{!actionForButton}" reRender="ActionCall">
<apex:param name="nextAction" value="messageForEvenOrOdd" assignTo="{!nextActionName}"/>
</apex:commandButton>
<!-- Action Function (which will be called after button action) -->
<apex:outputPanel id="ActionCall">
<apex:outputPanel layout="none" rendered="{!nextActionName == 'messageForEvenOrOdd'}">
<apex:actionFunction name="findDayEvenOrOdd_JS" action="{!messageForEvenOrOdd}"
rerender="UserMessage"/>
<script>
findDayEvenOrOdd_JS();
</script>
</apex:outputPanel>
</apex:outputPanel>
</apex:form>
<!-- User Message -->
<apex:outputText value="{!message}" id="UserMessage"/>
</apex:page>
view raw gistfile1.txt hosted with ❤ by GitHub

1 comment:

Thanks for reading my blog !