Sunday, August 24, 2014

What is Action Function in Salesforce?

What is Action Function in Salesforce?


Its easy to call a controller method for most of the attributes using action="{!Yourmethode_Name}", but what if you were to call the controller method from java script?

One way to do this is by using Action Function. Expertise will definitely be able to use this in complex scenarios. This post is for those who haven't had hands on action function before and want to know how to use it.

Lets take a Example and work on it:


You have a checkbox and you are calling javascript function on click of this checkbox. And now once you are in js you wish to modify some variable or do something in your controller class.

Say you want to put some value for a variable in controller and then display it on your page. this will require calling your class method from js.


Example :

Visualforce Page:

<apex:page standardcontroller="Account" extensions="ActionFunctionController" tabStyle="Account">
    <apex:form >
       <apex:actionFunction name="actionFunName" action="{!ActionFunMethode}" reRender="outputtxtId"/>
       <apex:pageBlock > 
            <apex:outputLabel for="inptCheckBox" value="Check this box to call Controller method from js using ActionFunction" style="color:green;"></apex:outputLabel> 
            <apex:inputcheckbox onclick="javaScrpt()" id="inptCheckBox"/>
       </apex:pageBlock> 
      
      <apex:outputText value="{!MyString_From_Methode}" id="outputtxtId"></apex:outputText> 
    </apex:form> 
    
    <script>
      function javaScrpt(){
       actionFunName(); 
      }
    </script>
     

</apex:page>


Controller class:

Public class ActionFunctionController {
Public string MyString_From_Methode{get;set;}

  public ActionFunctionController(ApexPages.StandardController controller) {

    }

  public string ActionFunMethode(){
     MyString_From_Methode = 'Method called from js using Action function';
     return null;
    }

}



Output:
save image


save image








Friday, August 22, 2014

What is Difference between Database DML statements and simple DML statements?

What is Difference between Database DML statements and simple DML statements?


Let us see 'insert' dml as an example. When we execute the below two statements, how do they differ from each other?

1. Insert accList;
2. Database.insert(accList,false);


**accList is a list of account records.

First let's see simple insert- when we do simple insert on that list it will either insert all of the account records in the list or none will be inserted. That is, there will be either total success or total failure. You cannot have partial success, partial failure in case of simple insert. If any of the record in the list fails due to any validation or any other thing, then none of the records in that list will be inserted. Records will be inserted only if all of the records are good to get inserted(i.e pass all sorts of validations)

Database.insert on the other hand can give you partial success. To get partial success we will have to pass a parameter "opt_allOrNone" as 'false' separated by the list name. If this parameter is set to true then it functions like simple insert. So if the parameter is false, then all the records passing validations will be inserted and others will be restricted.


We can track the errors that are encountered, using saveresult object as shown in the below code snippet,

List<database.saveresult> 
 accResultList = database.insert(accList,false);
for(database.saveresult res:accResultList){
if(!res.issuccess()){
  database.error e = esr.getErrors()[0];
system.debug('error is *******'+e.getmessage());
}



Let us summarise on the points discussed above,

Simple Insert
1. Can insert either all the records or no records at all. Partial success is not possible.

Database.insert(listname,false)
1. Partial success is possible in this case.

Database.insert(listname,true)
1. Operates same like simple insert dml i.e no partial success possible.

Data Loader, if you have observed works on the principle of database methods with "opt_allOrNone" flag set as 'false', hence we get partial success even if any of the record fail.

all of the points discussed above for insert also apply for other DML: update,upsert,delete.









Thursday, August 21, 2014

What is the Difference Between Rendered, ReRender and RenderAs in visualforce?

What is the Difference Between Rendered, ReRender and RenderAs in visualforce?

Rendered :

A visualforce component in a VF page can be displayed or hidden by using rendered attribute. Rendered is bound to a Boolean variable in controller which can be switched between true and false making the vf component display or hide depending on Boolean value.

As an example:

Visualforce Page:

<apex:page controller="RenderedControllr">
 <!-- Rendered  Demo -->
 <apex:form >
   <apex:pageBlock >
    <apex:commandButton value="Show Bottom Page Block" action="{!ShowBlockMethod}"/>
   </apex:pageBlock>
    
   <apex:pageBlock rendered="{!ShowpageBlockFlag}">
    Account Name  :<apex:outputField value="{!accRec.name}"/>
<br/>
    Account Number :<apex:outputField value="{!accRec.accountnumber}"/>
   </apex:pageBlock>
 </apex:form>

</apex:page>

Controller : 

Public class RenderedControllr {
 Public Boolean ShowpageBlockFlag{get;set;}
 Public Account accRec{get;set;}

  Public RenderedControllr(){
   accRec = [select name,id,accountnumber from account limit 1];
   ShowpageBlockFlag = false;
  }
   
  Public void ShowBlockMethod(){
   ShowpageBlockFlag = true;
  }

}

Out put :

save image

Once you click on Show Bottom Page Block..
save image


Here lower page block is given rendered that is bound to a boolean variable "ShowpageBlockFlag". Initially (in constructor) this variable is set to false which hides the lower page block.

Once we click the command button, "ShowBlockMethod" method is called where this boolean is set to true and hence the lower page block gets displayed.



ReRender :
Rerender is used to refresh a particular section of the visualforce page. We have to just mention the id of the page section (in the Rerender attribute) that needs to be refreshed.

In the following example Clicking of the command button "Refresh Lower Page Block" refreshes the lower page block.


Visualforce Page:

<apex:page controller="ReRenderControllr">
 <!-- Render and Rerender Demo -->
     <apex:form >
           <apex:pageBlock >
                <apex:commandButton value="Refresh Lower Page Block" action="{!ShowBlockMethod}" rerender="pgblckID"/>
           </apex:pageBlock>
        
           <apex:pageBlock id="pgblckID">
               <b>  Output Text   : </b>   <apex:outputText value="{!OutPutString}"/>
           </apex:pageBlock>
     </apex:form>

</apex:page>


Controller Class: 

public class ReRenderControllr {

 Public string OutPutString{get;set;}
   
  Public ReRenderControllr(){
    OutPutString = 'Test value set in Constructor';
  }
   
  Public void ShowBlockMethod(){
   OutPutString = 'value set in method called by button click' ;
  }
  

}


Output:
save image



save image


Initially when the page is loaded the output string value is set in constructor as "Test value set in constructor".'

When the button is pressed method "ShowBlockMethod" is called where "OutPutString"  value is changed also lower page block is refreshed and hence the new value is displayed in the lower page block.

rerender="pgblckID" statement in command button indicates that the page block section with id ="pgblckID" should be refreshed when button is pressed. Only Lower page block is refreshed rest of the page remains as it is.

A single Rerender attribute could be used to refresh many sections of the page. For example: reRender= "pgblck1, pgbcl2"



RenderAs
This is used with page component and renders the page in the specified format. 

Following page will give output in pdf form /render as pdf.


Visualforce Page:


<apex:page standardController="Account" renderAs="pdf">
     <apex:pageBlock >
          <apex:outputField value="{!Account.name}"/>
          <apex:outputField value="{!Account.AccountNumber}"/>
     </apex:pageBlock>
</apex:page>









 
| ,