Monday, 10 February 2014

How to remove required field validation in a Visualforce page?


To remove required field validation in a Visualforce page, set immediate attribute in <apex:commandButton> to true.

Example:

            <apex:commandButton value="Cancel" action="{!cancl}" immediate="true"/>

here immediate="true" makes the action to be executed first before executing any validation rule.

Monday, 3 February 2014

Difference between Action Function, Action Support and Action Poller.



actionfunction used to call the server side method using JavaScript
actionsupport used to call the server based on the client side event i.e. like onclick ,onchange..etc
actionpoller used to call the server side method in a regular interval of time,,.....

Explanation:-
----------------
actionFunction : provides support for invoking controller action methods directly from JavaScript code using an AJAXrequest

Used when we need to perform similar action on varioud events. Een though you can use it in place of actionSupport as well where only event is related to only one control.

Example :
actionFunction : provides support for invoking controller action methods directly from JavaScript code using an AJAXrequest

Example :
<!-- Page: -->
<apex:page controller="exampleCon">
<apex:form>
<!-- Define the JavaScript function sayHello-->
<apex:actionFunction name="sayHello" action="{!sayHello}" rerender="out"
status="myStatus"/>
</apex:form>
<apex:outputPanel id="out">
<apex:outputText value="Hello "/>
<apex:actionStatus startText="requesting..." id="myStatus">
<apex:facet name="stop">{!username}</apex:facet>
</apex:actionStatus>
</apex:outputPanel>
<!-- Call the sayHello JavaScript function using a script element-->
<script>window.setTimeout(sayHello,2000)</script>
<p><apex:outputText value="Clicked? {!state}" id="showstate" /></p>
<!-- Add the onclick event listener to a panel. When clicked, the panel triggers
the methodOneInJavascript actionFunction with a param -->
<apex:outputPanel onclick="methodOneInJavascript('Yes!')" 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>

/*** Controller ***/
public class exampleCon {
String uname;
public String getUsername() {
return uname;
}
public PageReference sayHello() {
uname = UserInfo.getName();
return null;
}
public void setState(String n) {
state = n;
}
public String getState() {
return state;
}
public PageReference methodOne() {
return null;
}
private String state = 'no';
}


.ActionSupport : A component that adds AJAX support to another component, allowing the component to be refreshed asynchronously by theserver when a particular event occurs, such as a button click or mouseover.

Used when we want to perform an action on a perticular eventof any control like onchange of any text box or picklist.

Example : 

<!-- Page: -->
<apex:page controller="exampleCon">
<apex:form>
<apex:outputpanel id="counter">
<apex:outputText value="Click Me!: {!count}"/>
<apex:actionSupport event="onclick"
action="{!incrementCounter}"
rerender="counter" status="counterStatus"/>
</apex:outputpanel>
<apex:actionStatus id="counterStatus"
startText=" (incrementing...)"
stopText=" (done)"/>
</apex:form>
</apex:page>


/*** Controller: ***/
public class exampleCon {
Integer count = 0;
public PageReference incrementCounter() {
count++;
return null;
}
public Integer getCount() {
return count;
}
}
 ActionPolor : A timer that sends an AJAX update request to the server according to a time interval that you specify. The update request canthen result in a full or partial page update. You should avoid using this component with enhanced lists.

Used when we want to perform an action on server again and again for a particular time interval. 

Example : 
<!-- Page -->
<apex:page controller="exampleCon">
<apex:form>
<apex:outputText value="Watch this counter: {!count}" id="counter"/>
<apex:actionPoller action="{!incrementCounter}" rerender="counter" interval="15"/>
</apex:form>
</apex:page>
/*** Controller: ***/
public class exampleCon {
Integer count = 0;
public PageReference incrementCounter() {
count++;
return null;
}

Tuesday, 28 January 2014

Display Date/Time Based On Lookup User.





VF Page:

<apex:page standardcontroller="Contact" extensions="RelatedController" showHeader="false">
 <apex:form >
  <apex:actionRegion >
   <apex:pageBlock id="accinfo">      
    <apex:pageBlockSection title="Account Information">
  
     <apex:inputField value="{!contact.AccountId}" id="abc">
      <apex:actionSupport event="onchange" action="{!currentDatePopup}" rerender="accinfo,abc" /> 
     </apex:inputField>
 <apex:outputText value="{!currentDatetime }" label="Current Date/time" />

   <apex:inputField value="{!contact.Look__c}" id="abc1">
    <apex:actionSupport event="onchange" action="{!currentDatePopup1}" rerender="accinfo,abc1" /> 
  </apex:inputField>       
 <apex:outputText value="{!currentDatetime1 }" label="Current "/> 

   </apex:pageBlockSection>        
  </apex:pageBlock>
  </apex:actionRegion>      
 </apex:form>
</apex:page>


Apex Class:

public with sharing class RelatedController
{
 private ApexPages.StandardController stdCtrl;
 public Contact con;
  public datetime currentDatetime{get;set;}
  public datetime currentDatetime1{get;set;}

  public void currentDatePopup()
{
        currentDatetime = System.now();            
}
  public void currentDatePopup1()
{
        currentDatetime1 = System.now();            
}
 public RelatedController(ApexPages.StandardController std)
 {
  stdCtrl=std;
 }

 public void AccountPopulated()
 {
Contact cont=(Contact) stdCtrl.getRecord();
   try{
   if(cont.account!= null)
 
  cont.Account=[select AccountNumber,CurrentDate__c,Lookuptest__c,DateTime__c from Account where id=:cont.AccountId ];
 }
 catch(exception e){}
 }
}










Radar Chart using Visualforce and Apex in Salesforce

Sample Code:

Visualforce page:


<apex:page sidebar="false" Controller="Sample" showHeader="true" id="pg">
<apex:chart height="750" width="800" legend="true" data="{!data}">
    <apex:legend position="left"/>
    <apex:axis type="Radial" position="radial">
        <apex:chartLabel />
    </apex:axis>
    <apex:radarSeries xField="memName" yField="tenthPercent" tips="true" opacity="0.4"/>
    <apex:radarSeries xField="memName" yField="twelthPercent" tips="true" opacity="0.4"/>
    <apex:radarSeries xField="memName" yField="age" tips="true" markerType="cross" strokeWidth="2" strokeColor="#f33" opacity="0.4"/>
</apex:chart>
</apex:page>

Apex Controller:

public class Sample {   

    public List<RadarData> data {get;set;}
    public sample() {
    data = new List<RadarData>();
        List<Member__c> memList = new List<Member__c>();
        memList = [SELECT Name, Age__c, X10th__c, X12th__c FROM Member__c];
        for(Member__c mem : memList) {
            data.add(new RadarData(mem.Name, mem.X10th__c, mem.X12th__c, mem.Age__c));
        }
    }
    public class RadarData {
        String memName {get;set;}
        Decimal tenthPercent {get;set;}
        Decimal twelthPercent {get;set;}
        Decimal age {get;set;}
        
        public RadarData(String memName, Decimal tenthPercent, Decimal twelthPercent, Decimal age) {
            this.memName = memName;
            this.tenthPercent = tenthPercent;
            this.twelthPercent = twelthPercent;
            this.Age = age;
        }
    }
}

Output:

How To: Call Apex code from a Custom Button

How do I call Apex code from a Custom Button?




The steps are as follows to add either a Detail or a List View  button (as illustrated below) to Standard or Custom Object. It’s well worth going through the topics and general reference guides I’ve linked in more detail. I’ve given some examples of my own, but there are also plenty of them in the help topics I’ve linked to if you need more examples.


Screen Shot 2013-07-16 at 09.13.00Screen Shot 2013-07-16 at 09.12.46
Steps to Create a Custom Button that runs Apex Code
  1. Create a Apex Extension Controller class as shown in the examples below.
  2. Create a Visualforce page, using the ‘standardController‘ and ‘extensions‘ attributes on apex:page
  3. Create a Custom Button using the Visualforce page as its Content Source
  4. Add the Custom Button to the appropriate Layout of the object
  5. Use either the ‘action‘ attribute (see warning below) or apex:commandButton‘s on your page to invoke Apex logic.

Detail Page Custom Button Template
Example page and class using apex:commandButton to invoke the logic.
1
2
3
4
5
<apex:page standardController="Test__c" extensions="DetailButtonController">
    <apex:form >
        <apex:commandButton value="Do something" action="{!doSomething}"/>
    </apex:form>
</apex:page>
Apex controller code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public with sharing class DetailButtonController
{
    private ApexPages.StandardController standardController;
    public DetailButtonController(ApexPages.StandardController standardController)
    {
        this.standardController = standardController;
    }
    public PageReference doSomething()
    {
        // Apex code for handling record from a Detail page goes here
        Id recordId = standardController.getId();
        Test__c record = (Test__c) standardController.getRecord();
        return null;
    }
}
Or to have your Apex logic run as soon as the user presses the Custom Button use the action attribute.
1
2
<apex:page standardController="Test__c" extensions="DetailButtonController"
           action="{!doSomething}">
To add the Custom Button should look something like this…
Screen Shot 2013-07-16 at 07.43.22
List View Custom Button Template
Example page and class, using apex:commandButton to invoke the logic.
1
2
3
4
5
6
<apex:page standardController="Test__c" extensions="ListButtonController"
           recordSetVar="TestRecords">
    <apex:form >
        <apex:commandButton value="Do something" action="{!doSomething}"/>
    </apex:form>
</apex:page>
Apex controller code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public with sharing class ListButtonController
{
    private ApexPages.StandardSetController standardSetController;
    public ListButtonController(ApexPages.StandardSetController standardSetController)
    {
        this.standardSetController = standardSetController;
    }
    public PageReference doSomething()
    {
        // Apex code for handling records from a List View goes here
        List<Test__c> listViewRecords =
            (List<Test__c>) standardSetController.getRecords();
        List<Test__c> selectedListViewRecords =
            (List<Test__c>) standardSetController.getSelected();
        Boolean hasMore = standardSetController.getHasNext();
        return null;
    }
}
Or to have your Apex logic run as soon as the user presses the Custom Button use the action attribute.
1
2
<apex:page standardController="Test__c" extensions="ListButtonController"
           action="{!doSomething}" recordSetVar="TestRecords">
To add the Custom Button should look something like this…

Screen Shot 2013-07-16 at 07.43.58