Showing posts with label Visualforce Page. Show all posts
Showing posts with label Visualforce Page. Show all posts

Friday, 1 July 2016

Salesforce Dev Utility Post #3 : Pagination in Visualforce Page, Using Visualforce Component

Working on large set of Data or Displaying more than 100 records in a single visualforce page, We need to do the Pagination,

Salesforce has a number of different pagination options that are available to you which include using a Visualforce StandardSetController, Query and QueryMore SOAP API Calls, and Offset clauses within SOQL queries. These options are great but sometimes when you are building a custom site or working with complex data models you are unable to use standard objects and need to use custom wrapper classes for your data.

Below is Generic component to implement pagination. Here you only have to pass the list of recodrs you want to show up using pagination also you have to pass the columns fields you desire to display.

Following is a visualforce component for pagination:
Here you have to pass the list of records(whichever object it may be) you want to display in table and the column fields to display.

Visualforce component

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<apex:component controller="ComponentController">
  <apex:attribute name="listo" description="List of Account" type="Account[]" required="false" assignTo="{!idList}"/>
  <apex:attribute name="listofield" description="Fields to display" type="string[]" required="false" assignTo="{!SobjFieldList}"/>     
     
    <apex:form >
       <apex:pageblock id="pg">
          <apex:pageBlockTable value="{!SObjectRecs}" var="rec">
             <apex:repeat value="{!FieldList}" var="fl">
                <apex:column value="{!rec[fl]}"/>
             </apex:repeat> 
          </apex:pageBlockTable>
       
          <apex:panelGrid columns="7">
             <apex:commandButton status="fetchStatus" reRender="pg" value="First" action="{!setRecords.first}" disabled="{!!setRecords.HasPrevious}" />
             <apex:commandButton status="fetchStatus" reRender="pg" value="Previous" action="{!setRecords.previous}" disabled="{!!setRecords.HasPrevious}" />
             <apex:commandButton status="fetchStatus" reRender="pg" value="Next" action="{!setRecords.next}" disabled="{!!setRecords.HasNext}" />
             <apex:commandButton status="fetchStatus" reRender="pg" value="Last" action="{!setRecords.last}" disabled="{!!setRecords.HasNext}"/>
             <apex:outputPanel style="color:green;">
                  <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
             </apex:outputPanel>
          </apex:panelGrid>
       </apex:pageblock>
    </apex:form>
  
</apex:component>

Controller For Visualforce component

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class ComponentController {

public List<sObject> idList{get;set;}
public List<String> SobjFieldList{get;set;}
public Integer pageSize = 10;  
  Public ApexPages.StandardSetController setRecords{
    get{
     if(setRecords == null){
        setRecords = new ApexPages.StandardSetController(getIdListRecrds());
        setRecords.setPageSize(pageSize);
     }  
      return setRecords;
    }set;
   }
    
   Public List<sObject> getSObjectRecs(){
        List<sObject> sObjList = New List<sObject>();
        for(sObject SObj :(List<sObject>)setRecords.getRecords())
            sObjList.add(SObj); 
        return  sObjList ;   
   }
   
   Public List<String> FieldList{
       get{
       List<String> FieldList = New List<string>();
       FieldList = getSobjtFieldList();
       return FieldList;
       }set;
   }
   
    public List<sObject> getIdListRecrds() {
       List<sObject> IdListRecrds =idList;
       return IdListRecrds;
    }
    
    public List<string> getSobjtFieldList() {
       List<String> FieldList = SobjFieldList;
       return FieldList ;
    }

}

Usage with Visual force Page :

1
2
3
<apex:page controller="PageController">
  <c:PaginationComponent listo="{!accountList}" listofield="{!accountFieldList}"/>
</apex:page>

Controller :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public Class PageController {
Public List<Account> accountList {get;set;}
Public List<String> accountFieldList{get;set;}

    public PageController (){
      accountList = New List<Contact>();
      accountFieldList = New List<string>();
      accountList = [select Name, AccountNumber, AccountSource, Phone, Type from Account];
      accountFieldList.add('Name');
      accountFieldList.add('AccountNumber');
      accountFieldList.add('AccountSource');
      accountFieldList.add('Phone');
      accountFieldList.add('Type');
    }
}

Please connect with me in case of any doubt.

                                         Happy Coding !!      

Thursday, 12 November 2015

Salesforce Dev Utility Post #2 : Best Practice -- Force.com Development Naming Conventions

If most people don't mind along with the senior developer then you will have very difficult time persuading them all. In this case I would recommend just keep going with the original convention set forth by the original developer and at least keep things consistent.

If other people are not okay with that as well then you can focus on the ROI of converting already existing project naming convention to language naming convention and present that to the management along with the senior developer. This will most certainly cause some friction.
In conclusion, I would say that language naming conventions are very important because new people coming into your project are more comfortable with the code-base requiring less explanation and causing less friction.

The reason being that language naming conventions are global and project naming conventions are local so people would need more time getting used to it.

As new developers are getting used to the project naming conventions they are bound to mix it up with language naming conventions if that's what they were used to. Code reviews could solve this issue but it is an unpleasant element of going with the project naming conventions nevertheless.

Hence, from that perspective language naming conventions are more important than project naming conventions.

Follow the CamelCase Java conventions, except for VF pages and components start with a lower case letter.

Triggers:
  • § <ObjectName>Trigger - The trigger itself. One per object.
  • §  <ObjectName>TriggerHandler - Class that handles all functionality of the trigger
  • §  <ObjectName>TriggerTest

Controllers:
  • §  <ClassName>Controller
  • §  <ClassName>ControllerExt
  • §  <ClassName>ControllerTest
  • §  <ClassName>ControllerExtTest

Classes:
  • §  <ClassName>
  • §  <ClassName>Test (These might be Util classes or Service classes or something else).

Visualforce pages and components:
  • §  <ControllerClassName>[optionalDescription] (without the suffix Controller). There might be multiple views so could also have an extra description suffix.


Object Names and custom Fields:
  • §  Upper_Case_With_Underscores

Variables/properties/methods in Apex:
  • §  camelCaseLikeJava - more easily differentiated from fields

Test methods in test classes
  • §  test<methodOrFunctionalityUnderTest><ShortTestCaseDesc> - For example, testSaveOpportunityRequiredFieldsMissing, testSaveOpportunityRequiredFieldsPresent, etc.

Working on something that would be used as an app or in some cases just a project? If yes, then do the following:

Prefix all custom objects, apex classes, Visualforce pages and components with an abbreviation so that they are easier to identify (e.g., easier for changesets). For example the WidgetFactory app would have the prefix wf on those. Additionally, when adding custom fields to a standard object they would also be prefixed to identify them as part of the app/package.


The main reason for the Object and Fields Names using Upper_Case_With_Underscores is that when you type in the name field or object with spaces it automatically adds the underscores. Although Apex is case insensitive, always refer to the Objects and Custom Fields in the code as Upper_Case_With_Underscores as well for consistency all around and consistency with what is generated by the SOQL schema browser and other tools. Object and Field Labels (which are generally ignored by code but visible to users) should keep spaces, not underscores.

"Happy Coding"


Wednesday, 18 February 2015

Jquery Colorbox Modal Window in Visualforce Page


Displaying a modal dialog with jQuery is easy. But what happens if you want to display that modal dialog within a visualforce page? Or what if you want to show the contents of a visualforce page inside the dialog? How can you deal with the cross domain issue if you want to, for example, close the popup once some logic is executed?

In this post I will explain how to display a modal dialog on a visual page by clicking a detail page button. In order to do this I will use “Jquery Colorbox”, a simple but powerful library that will provide different variety of Colorbox.


Visualforce Page Code Using Command Link :
  <apex:page >   
  <!-- Style Sheet For Colorbox -->    
  <apex:stylesheet value="{!URLFOR($Resource.Colorboxcss, 'colorbox.css')}"/>   
  <apex:pageBlock mode="maindetail">   
  <apex:form >   
  <!-- Here we are using class to call javascript or we can use diffrent javascript function  
 Note: href is referencing to another VF Page which needs to be open in Modal Window   
  -->    
  <a class="openModalBox" href="/apex/ModalPopup"> Open Modal Popup </a>   
 <!-- Calling Java Script Using Apex Command Button or Link -->  
 <apex:commandLink oncomplete="openColorBox()"  value=" Open Modal Popup 2"/>  
  </apex:form>   
  </apex:pageBlock>   
  <!-- Jquery Librarys -->    
  <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"/>   
  <apex:includeScript value="{!$Resource.Colorboxjs}"/>   
  <script>   
 <!-- Calling link Using Action -->   
 function openColorBox()  
 {  
 $.colorbox({iframe:true, href:'/apex/ModalPopup', innerWidth:340, innerHeight:290 , width:"90%", height:"80%", overlayClose : false ,escKey :false ,fixed :true });  
 }  
 <!-- Calling link Using Class Attribute -->   
  $(document).ready(function(){   
  $(".openModalBox").colorbox({iframe:true, innerWidth:640, innerHeight:390 , width:"90%", height:"80%", overlayClose : false ,escKey :false ,fixed :true });   
  });   
  </script>   
  </apex:page>   
Static Resource : Click Here to download (Extract Zip and Create Two  Static Resource One for Java Script and Other of CSS)

Using Colorbox Link to explore more functionality from colorbox : Jquery Colorbox

                                                                Happy Coding !!