Monday, June 1, 2015

Difference between ActionFuction And ActionSupport

Difference between ActionFuction And ActionSupport

Visualforce provides three great components to call controller methods they are ActionPoller, ActionFuction & ActionSupport. Among these three components ActionFuction & ActionSupport are very similar and in most of cases can be used interchangeably. Following is example of same.

VF1– Action Function
=================================
<apex:page controller="exampleCon">           
    <p><apex:outputText value="Clicked? {!state}" id="showstate" /></p>              
    <apex:outputPanel onclick="methodOneInJavascript()" styleClass="btn"> Click Me </apex:outputPanel>     
                        <apex:form >
                                                <apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
                                                                        <apex:param name="firstParam" assignTo="{!state}" value="" />
                                                </apex:actionFunction>
                      </apex:form>
</apex:page>

VF2 – Action Support
=================================
<apex:page controller="exampleCon">
<apex:form>           
    <p><apex:outputText value="Clicked? {!state}" id="showstate" /></p>               
    <apex:outputPanel styleClass="btn"> Click Me        
        <apex:actionSupport event="onclick" action="{!method2}" rerender="showstate">
            <apex:param name="firstParam" value="Yes" />
         </apex:actionSupport>
    </apex:outputPanel>   
</apex:form>  
</apex:page>

Controller
=================================
public class exampleCon {
private String state = 'no';         
    public void setState(String n) {
        state = n;
    }
public String getState() {
        return state;
    }            
public PageReference methodOne() {
        return null;
    }
public PageReference method2() {
        state = ApexPages.CurrentPage().getParameters().get('firstParam');
        return null;
    }
}

As you can see I can use both Action fiction and Action support to archive the same functionality.  So the question is they truly interchange? Well the answer is No!

When should I use Action Function and when to use Action support?
When you want to add ACTION to any (single) apex component then you should use Action support
When there multiple components which needs to call controller action then you should use Action Function.

Why?
The reason lies in the background implementation. When you define Action Support in your VF it generates AJAX code with function name that call controller method. If you use this in loop (repeat) this defines multiple JS function with same name which throws error in JS and your page do not work properly.

In Case of Action Function it is defined at one place and can be called from many places. So if you have loop (repeat) in your code then calling code can be repeated in loop and that is why Action Function should be used with Repeat.



Salesforce Static Code Analysis using codescan.villagechief.com

Salesforce Static Code Analysis .



Like many other sophisticated programming language there is Static Code Analysis tool available by codescan.villagechief.com. This code review tool can give us lots of benefits. I have listed some of them bellow.


• Warning on some governor limits  - for ex SOQL in loops

• Coding standards: naming conventions, fields at top of file, etc

• Redundant code


• Code simplification suggestions

• Clean code, less mistakes less bugs


So, now the question is hot to set up Static Code Analysis tool? 

There are 2 ways you can use a) Eclips IDE  2) Set up SonarQube Server

Setting SonarQube Server is little tricky and it need licence from codescan.villagechief.com so for now lets focus on how to set up Eclips IDE

Steps to install CodeScan Apex PMD Eclipse Plugin:
1.In Eclipse go to Help -> Install new Software-> use http://codescan.villagechief.com/eclipse/ link to install plugin


2) Select the plugin and click next next...

3) After successful installation of plugin, Go to project Explorer -> Right click on your sales-force project -> ApexPDM -> run ApexPDM


4) To view Codescan result, goto ApexPDM view 
5) You can run ApexPDM on specific class also


You can scan your code online.Goto http://codescan.villagechief.com/report/index Paste your class for code analysis

5) 

Salesforce oAuth Username Password Flow is security risk?


When user login from the UI, SFDC checks if its first successful login attempt from that machine. If it is true then it asks for addition security code which is sent to your email address. The only exception to this is made when that machine’s IP is in trusted IP range.

Similar additional check is implemented for SOAP API login call. It requires User name, password & security token to get access to any data.

If you consider above security measures then Salesforce oAuth Username Password flow seems vulnerable because you do not need any additional information to call Username Password flow.

I mean you defiantly need client_id & client_secret but anyone can create connected app in developer org and they can bypass the security if they just have user name & password.


Am I missing something?