function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zach AckermanZach Ackerman 

List View VFPage

I have created a VF Page of an opportunity list view. One of the fields is Opportunity Name. When you click the name it opens the record in the VFPage. Can I modify the code to open a new tab when the opportunity is clicked? 

<apex:page sidebar="false">
 
   <apex:enhancedlist height="450" listid="00Be0000001eBaB" rowsperpage="100">;
    
</apex:enhancedlist></apex:page>
Best Answer chosen by Zach Ackerman
Saurabh BSaurabh B
Hi Zach,
You are on the right path but it looks like your queryString is breaking. Please see updated code below.

In example below, we are using Dynamic SOQL which works little differently than normal SOQL. In Dynamic SOQL, you have to pass all parameters in string format. Please take a look at Line 9 and 10 where we are converting SOQL in strings datatype. Also, look at the updated line 11 where we have updated the queryString.

Here is an article on Dynaimic SOQL for your future reference,
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm
 
public with sharing class Pagination_min {
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}

    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                size = 50;
               string OwnersId =  userinfo.getUserId() ;
               string Rectypeid =   '01290000000uI0O'  ;
                string queryString = 'Select ID,Name,Type,StageName,CloseDate from Opportunity WHERE OwnerId = :OwnersId AND IsClosed = False AND RecordTypeId = :Rectypeid';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
        }set;
    }
     
    Public List<Opportunity> getOpps(){
        List<Opportunity> oppList = new List<Opportunity>();
        for(Opportunity o : (List<Opportunity>)setCon.getRecords())
            oppList.add(o);
        return oppList;
    }
     
    public pageReference refresh() {
        setCon = null;
        getOpps();
        setCon.setPageNumber(1);
        return null; 
    }
}

Did it help you? Please mark this as Best Answer if it did!
 

All Answers

Saurabh BSaurabh B
Hi Zach,
You wont be able to do that using <apex:enchancedlist> however, you can do it using Custom List Controller. Please try code below,

Controller:
public class opportunityList2Con {
    // ApexPages.StandardSetController must be instantiated
    // for standard list controllers
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Name, CloseDate FROM Opportunity]));
            }
            return setCon;
        }
        set;
    }

    // Initialize setCon and return a list of records
    public List<Opportunity> getOpportunities() {
        return (List<Opportunity>) setCon.getRecords();
    }
}
VF page:
 
<apex:page controller="opportunityList2Con">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column headerValue="Opportunity Name">  
      <apex:outputlink value="/{!o.id}" target="__blank">{!o.Name}</apex:outputlink>  
     </apex:column>  
                   <apex:column value="{!o.CloseDate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Please mark this as Best Answer if it helps you!
 
Zach AckermanZach Ackerman
Very helpful. A few follow up questions. How could I make it so the page displays 50 rows and gives me the option to hit next to display the next 50 records?
Saurabh BSaurabh B
Hi Zach, I believe you are referring to Pagination in Salesforce. Please see code below I modified for your use case. This will fetch 50 Opportunity records at a time and will give you an option to hit Next, Pevious etc. You can modify the code for you needs.

Controller:
public with sharing class Pagination_min {
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}
    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                size = 50;
                string queryString = 'Select ID,Name,Type,StageName,CloseDate from Opportunity order by Name';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
        }set;
    }
     
    Public List<Opportunity> getOpps(){
        List<Opportunity> oppList = new List<Opportunity>();
        for(Opportunity o : (List<Opportunity>)setCon.getRecords())
            oppList.add(o);
        return oppList;
    }
     
    public pageReference refresh() {
        setCon = null;
        getOpps();
        setCon.setPageNumber(1);
        return null; 
    }
}

VF Page:
<apex:page controller="Pagination_min">
    <apex:form >
        <apex:pageBlock id="pb">
            <apex:pageBlockTable value="{!opps}" var="o">
            	<apex:column headerValue="Opportunity Name">  
    				  <apex:outputlink value="/{!o.id}" target="__blank">{!o.Name}</apex:outputlink>  
     			</apex:column>                  
                <apex:column value="{!o.Type}"/>
                <apex:column value="{!o.StageName}"/>
                <apex:column value="{!o.CloseDate}"/>
            </apex:pageBlockTable>
            <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>
                <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>
                <apex:outputPanel style="color:#4AA02C;font-weight:bold">
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                </apex:outputPanel>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please mark this as Best Answer so that it can help others!

Cheers!
Zach AckermanZach Ackerman
Thanks one last question. I had updated the select in the first verison to show where OwnerId = :userinfo.getUserId() AND IsClosed=False AND Recordtypeid='01240000000Uexl'. The isclosed where clause is still working, but when the other two are present the vf page doesn't work. I'm new to this so I don't understand the difference. How can i get the where clause to work on the  Pagination. 
Saurabh BSaurabh B
Hi Zach,
You are on the right path but it looks like your queryString is breaking. Please see updated code below.

In example below, we are using Dynamic SOQL which works little differently than normal SOQL. In Dynamic SOQL, you have to pass all parameters in string format. Please take a look at Line 9 and 10 where we are converting SOQL in strings datatype. Also, look at the updated line 11 where we have updated the queryString.

Here is an article on Dynaimic SOQL for your future reference,
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm
 
public with sharing class Pagination_min {
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}

    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                size = 50;
               string OwnersId =  userinfo.getUserId() ;
               string Rectypeid =   '01290000000uI0O'  ;
                string queryString = 'Select ID,Name,Type,StageName,CloseDate from Opportunity WHERE OwnerId = :OwnersId AND IsClosed = False AND RecordTypeId = :Rectypeid';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
        }set;
    }
     
    Public List<Opportunity> getOpps(){
        List<Opportunity> oppList = new List<Opportunity>();
        for(Opportunity o : (List<Opportunity>)setCon.getRecords())
            oppList.add(o);
        return oppList;
    }
     
    public pageReference refresh() {
        setCon = null;
        getOpps();
        setCon.setPageNumber(1);
        return null; 
    }
}

Did it help you? Please mark this as Best Answer if it did!
 
This was selected as the best answer
Zach AckermanZach Ackerman
Saurabh B, 

I have made a few changes to make the code work the way I want. The last thing I want to be able to to acheive is giving the user the ability to sort the column headers. Is that possible? 


Apex: 
 
public with sharing class Pagination_min {

    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}
    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                size = 500;
                string OwnersId =  userinfo.getUserId() ;
                string Rectypeid =   '01240000000Uexl';
                string Rectypeid2 = '01240000000cnJS';
                string queryString = 'Select Name, StageName,FED__c,Broker__c,Broker_Name__c,Core_Product__c,LeadSource,Number_Quoted__c,CreatedDate FROM Opportunity Where OwnerId = :OwnersId AND IsClosed = False AND (RecordTypeId = :Rectypeid OR RecordTypeId= :RecTypeId2) order by FED__c';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
        }set;
    }
     
    Public List<Opportunity> getOpps(){
        List<Opportunity> oppList = new List<Opportunity>();
        for(Opportunity o : (List<Opportunity>)setCon.getRecords())
            oppList.add(o);
        return oppList;
    }
     
    public pageReference refresh() {
        setCon = null;
        getOpps();
        setCon.setPageNumber(1);
        return null; 
    }
}

Visual Force
 
<apex:page controller="Pagination_min">
    <apex:form >
           <apex:commandButton value="New Opportunity" onClick="window.open('https://cs15.salesforce.com/006/e?retURL=%2F006%2Fo&RecordType=01240000000Uexl&ent=Opportunity');"/>
       <apex:pageBlock id="pb">
       
            <apex:pageBlockTable value="{!opps}" var="o">
                <apex:column headerValue="Opportunity Name">  
                      <apex:outputlink value="/{!o.id}" target="__blank">{!o.Name}</apex:outputlink>  
                </apex:column>                  
                <apex:column value="{!o.StageName}"/>
                <apex:column headerValue="Effective Date" value="{!o.FED__c}"/>
                <apex:column headerValue="Broker Name">
                <apex:outputlink value="/{!o.Broker__c}" target="__blank">{!o.Broker_Name__c}</apex:outputlink>  
                 </apex:column>    
                <apex:column value="{!o.Core_Product__c}"/>
                <apex:column value="{!o.LeadSource}"/>
                <apex:column value="{!o.Number_Quoted__c}"/>
                <apex:column value="{!o.CreatedDate}"/>
            </apex:pageBlockTable>
            <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/>
                <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,(setCon.pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>
                <apex:outputPanel style="color:#4AA02C;font-weight:bold">
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                </apex:outputPanel>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>