Académique Documents
Professionnel Documents
Culture Documents
Scenarios
Compare old values with new values
Scenario?
When ever a account is updated ensure that account number is not
changed
Solution?
What to use? : apex, Visualforce or apex triggers?
Scenario?
Ex:- When user creates an account with annual revenue less than 2000 or Account
Source is blank then show error
BV
Solution?
Use either workflow, process builder or apex trigger
Solution with apex trigger
trigger AccountValidationTrigger on Account (before insert, before update) {
for(Account acc: Trigger.New)
{
if(acc.AccountSource == NULL)
{
acc.AccountSource.addError('Account Source is blank');
}
else if(acc.Annualrevenue <2000)
{
acc.AnnualRevenue.addError('Annual Revenue less than $2000 ');
}
}
}
Apex Trigger Bulkification
What is Trigger bulkification?
Simply it means writing code to handle many records at a time
instead of processing one record at a time
conRec.email = 'testemail@kmail.com';
update conRec;
}
Solution
global Class recusrssionPreventController{
Global static boolean flag = true;
Public recusrssionPreventController(){
}
}
conRec.email = 'testemail@kmail.com';
update conRec;
}
}
Revist some common Visualforce
Tags
To display Visualforce Page Like this
<apex:page standardController="account" recordSetVar="account">
<apex:form >
<apex:pageBlock >
Account details are below
</apex:pageBlock>
<apex:PageBlock >
<apex:PageBlockSection columns="3" >
<apex:PageBlockSectionItem >
<apex:dataTable value="{!account}" var="acc" >
<apex:column value="{!acc.name}" headerValue="Name"/>
<apex:column value="{!acc.accountnumber}" headerValue="Number"/>
</apex:dataTable>
</apex:PageBlockSectionItem>
<apex:PageBlockSectionItem > <apex:commandButton value="Second column"
action="New"/></apex:PageBlockSectionItem>
<apex:PageBlockSectionItem > <apex:commandLink value="ThirdLink"/></apex:PageBlockSectionItem>
</apex:PageBlockSection>
</apex:PageBlock>
</apex:form>
</apex:page>
Visualforce and apex combinations
Getting global and current page parameters
<apex:page controller="passparamController">
<!-- Pass parameters from visualforce page to controller -->
<apex:form >
<apex:pageblock >
Input Here <apex:inputText value="{!myinput}"/>
<apex:commandButton value="Submit" reRender="outputID" action="{!MyMethode}"/>
</apex:pageblock>
<apex:pageblock >
<b>Output here = </b><apex:outputText value="{!myoutput}" id="outputID">
</apex:outputText>
</apex:pageblock>
</apex:form>
</apex:page>
Relationship Query
Scneario?
Whenever you want to show child records under parent
or parent records in child
Example please?
If you want to show contact records under account
Relationship Query Implementation
public with sharing class contrller {
Public id Current_Acc_Id;
Scenario?
If you want to display different objects or datatypes in a
single list ex: lets display 5 account records in table with
checkbox
Implementation
<apex:page standardController="Account"
extensions="WrapperDemoClass">
<apex:form >
<apex:pageblock >
<apex:pageblockTable value="{!wrapperObj}" var="Rec">
<apex:column value="{!Rec.accObj.name}"/>
<apex:column >
<apex:inputcheckbox value="{!Rec.checkBox}"/>
</apex:column>
</apex:pageblockTable>
</apex:pageblock>
</apex:form>
</apex:page>
Wrapper class
public with sharing class WrapperDemoClass {
Public List<WrapperClassEx> WrapperList{get;set;}
public WrapperDemoClass(ApexPages.StandardController controller) {
Example:-
If you want to display some block when command button is clicked
Solution:-
Rendered attribute
Public with sharing class hideblock{
Public Boolean ShowpageBlockFlag{get;set;}
Public Account accRec{get;set;}
Public hideblock(){
accRec = [select name,id,accountnumber from account limit 1];
ShowpageBlockFlag = false;
}
<apex:page controller="hideblock">
<!-- Render and Rerender 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>
Refresh a component
Scenario:-
Based on a button click we might have to change the
value in a component
Public with sharing class RefreshComp {
Public string OutPutString{get;set;}
<apex:page controller="RefreshComp">
<!-- 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>
Schema class
Scenario:-
Often there are situations where you might have get
objects and Fields data and most often it is used in
dynamic soql query construction
Example
If you want display all the objects data in a picklist
How do we access Schema Object
-------- Function to get all the objects data
--------------------
Schema.getGlobalDescribe().keyset();
---------------- What visualforce component displays
the picklist ---------
<Apex:selectlist> and
<apex:selectOption>
Display all the objects data in a
picklist
public class AllObjectsinOrg {
Public string ObjectSelected{get;set;}
Public Map<String, Schema.SObjectType> AllObjmap;
Public AllObjectsinOrg(){
AllObjmap = New Map<String, Schema.SObjectType>();
AllObjmap = Schema.getGlobalDescribe();
System.debug('******All object Names :'+ AllObjmap.keyset());
}
Public List<selectoption> getObjList(){
List<selectoption> objList = new List<selectoption>();
for(string s:AllObjmap.keyset()){
objList.add(new selectoption(s,s));
}
return objList;
}
}
Visualforce
<apex:page controller="AllObjectsinOrg">
<apex:form >
<apex:pageBlock id="pgblck">
<apex:outputlabel value="Object Name" for="ObjPickList"/>
<apex:selectList value="{!ObjectSelected}" multiselect="false"
id="ObjPickList" size="1">
<apex:selectOptions value="{!ObjList}"/>
</apex:selectList>
</apex:pageBlock>
</apex:form>
</apex:page>
Get all objects and fields using schema
public class AllObjectsandFields {
Public string ObjectSelected{get;set;}
Public string fldselected{get;set;}
Public Map<String, Schema.SObjectType> AllObjmap;
Public Map <String, Schema.SObjectField> fieldMap;
Public boolean rendflag{get;set;}
Public AllObjectsandFields (){
AllObjmap = New Map<String, Schema.SObjectType>();
AllObjmap = Schema.getGlobalDescribe();
System.debug('******All object Names :'+ AllObjmap.keyset());
}
Public List<selectoption> getObjList(){
List<selectoption> objList = new List<selectoption>();
for(string s:AllObjmap.keyset()){
objList.add(new selectoption(s,s));
}
return objList;
}
Public List<selectoption> getFieldList(){
List<selectoption> fldList = new List<selectoption>();
if(!fieldMap.isEmpty()){
for(string s:fieldMap.keyset()){
fldList.add(new selectoption(s,s));
}
if(fldList.size()== 0)
fldList.add(new selectoption('--None--','--None--'));
}
return fldList;
}
Public void fieldsofObject(){
fieldMap = New Map <String, Schema.SObjectField>();
fieldMap = AllObjmap.get(objectSelected).getDescribe().fields.getMap();
System.debug('###### all fields of object## :' + fieldMap.keyset());
Visualforce Page
<apex:page controller="AllObjectsandFields">
<apex:form >
<apex:pageBlock id="pgblck">
<apex:outputlabel value="Object Name" for="ObjPickList"/>
<apex:selectList value="{!ObjectSelected}" multiselect="false" id="ObjPickList" size="1">
<apex:selectOptions value="{!ObjList}"/>
<apex:actionSupport event="onchange" action="{!fieldsofObject}" rerender="pgblck" />
</apex:selectList><br/><br/>
<apex:outputlabel value="Field Name" for="fldPickList" rendered="{!rendflag}"/>
<apex:selectList value="{!fldselected}" multiselect="false" id="fldPickList" size="1" rendered="{!rendflag}">
<apex:selectOptions value="{!FieldList}"/>
</apex:selectList>
</apex:pageBlock>
</apex:form>
</apex:page>
Javascript and Visualforce pages
Visualforce tags that supports javascript are
<action:status>
<action:function>
<action:support>
<action:pollar>
<apex:page controller="actionstatusDemoController">
<apex:form >
<apex:pageblock id="pgblck">
<apex:actionStatus id="actstatus" >
<apex:facet name="start" >
<img src="/img/loading32.gif" />
</apex:facet>
</apex:actionStatus>
<apex:commandButton value="Submit" status="actstatus" action="{!demomethod}" reRender="pgblck"/>
</apex:pageblock>
</apex:form>
</apex:page>
Call controller method from
javascript?
<apex:actionfunction> will call the controller method
from a javascript
Public class AFController {
Public string MyString_From_Methode{get;set;}
public AFController(ApexPages.StandardController controller) {
}
public string ActionFunMethode(){
MyString_From_Methode = 'Method called from js using Action function';
return null;
}
}
<apex:page controller="ASController">
<apex:form >
<apex:pageBlock >
Click Inside this block <apex:inputtext >
<apex:actionSupport event="onclick" action="{!DemoMethod}" rerender="pgblck"/>
</apex:inputtext>
</apex:pageBlock>
<apex:pageblock id="pgblck">
<apex:outputText value="{!outValueSecond }"/>
</apex:pageblock>
</apex:form>
</apex:page>
Timer in Visualforce Page
Action poller acts as a timer in visuaflorce page. It is
used to send an AJAX request to the server depending
on the time interval (time intervalhas to be specified or
else it defaults to 60 seconds).
Implementation
Public with sharing class actionpollerDemoController {
Public Integer seconds{get;set;}
Public actionpollerDemoController(){
seconds = 0;
}
<apex:page controller="actionpollerDemoController">
<apex:form >
<apex:pageBlock id="pgplck">
<apex:actionPoller action="{!CounterMethod}" reRender="pgplck" interval="5"/>
{!seconds } seconds since the action poller was called !!
</apex:pageBlock>
</apex:form>
</apex:page>
Javascript remoting important
concept
What is a javascript remoting?
JavaScript remoting is a tool that front-end developers can
use to make an AJAX request from a Visualforce page
directly to an Apex controller.
@RemoteAction
global static Account getAccount(String accountName)
{
account = [select id, name, phone, type, numberofemployees from Account where name = :accountName ];
return account;
}
}