1

I have created a visual force page in Account page that displays related opportunities by RecordType(5 Records at a time) for the given Account and when click on Go to List it directs to new VF page with all the related list by Opportunity record type. Now, I'm trying to implement Sorting Functionality in the new VF page that displays all the related list by Opportunity record type. I cannot implement using dynamic Soql. Is there a way to do it.

Here is my code Controller:

public with sharing class OppListView {
Public static set<String> record_types_for_orders = new set<String>{
           'Order1', 
           'Order2'};
Public static set<String> record_types_for_mOpportunities = new set<String>{
           'US Opportuntiy',
           'UState Opportunity'};  

private transient List<RecordType> recordTypeNames = new List<RecordType>();
private transient set<Id> orderRecordTypeIDs = new set<Id>();
private transient set<Id> oppRecordTypeIDs = new set<Id>();

private list<Opportunity> theOrders = new list<Opportunity>();
private list<Opportunity> theOpps = new list<Opportunity>();    

public Account acct{get;set;}
public string redirectUrl {public get;private set;}

List<aOLIPlus> scheduleArray;
List<aOLIPlus1> scheduleArray1;


public String selectedVal{get;set;}
 public String checkedVal{get {if(checkedVal ==null){checkedVal='DESC';}return checkedVal;}
    set;
}
public String sortField {get { if (sortField == null) {sortField = 'CloseDate'; } return sortField; }
set;
}

public OppListView(ApexPages.StandardController ctrl){
   acct = new Account();
   //get account Id from Url  
   acct.Id = System.currentPageReference().getParameters().get('id');

    recordTypeNames = [Select Id, Name, sObjectType From RecordType Where sObjectType = 'Opportunity']; 

    for(RecordType rt : recordTypeNames){  
        if(record_types_for_orders.contains(rt.Name)){
            orderRecordTypeIDs.add(rt.Id);
        } else if(record_types_for_Opportunities.contains(rt.Name)){
            oppRecordTypeIDs.add(rt.Id);
        }   
    }

   theOrders = [Select Name, StageName, Amount, CloseDate, Agency__c, RecordTypeId, RecordType.Name
                 From Opportunity Where AccountId = :acct.Id And RecordTypeId IN :orderRecordTypeIDs 
             Order By CloseDate DESC];


    theOpps = [Select Name, StageName, Amount, CloseDate, Agency__c, RecordTypeId, RecordType.Name
                 From Opportunity Where AccountId = :acct.Id And RecordTypeId IN :oppRecordTypeIDs
             Order By CloseDate DESC ];


    theMOpps = [Select Name, StageName, Amount, CloseDate, Agency__c, RecordTypeId, RecordType.Name
                 From Opportunity Where AccountId = :acct.Id And RecordTypeId IN :moppRecordTypeIDs
             Order By CloseDate DESC]; 

scheduleArray = new List<aOLIPLus>();
    for(Opportunity thisMaster : theOrders)        
        scheduleArray.add(new aOLIPlus(thisMaster, thisMaster.Name,thisMaster.StageName, thisMaster.Amount, thisMaster.CloseDate, thisMaster.Agency__c,thisMaster.RecordTypeId,thisMaster.RecordType.Name));        

    scheduleArray1 = new List<aOLIPLus1>();
    for(Opportunity thisMaster1 : theOpps)        
        scheduleArray1.add(new aOLIPlus1(thisMaster1, thisMaster1.Name,thisMaster1.StageName, thisMaster1.Amount, thisMaster1.CloseDate, thisMaster1.Agency__c,thisMaster1.RecordTypeId,thisMaster1.RecordType.Name));      

}
public List<aOLIPlus> getScheduleArray()
{    
    return scheduleArray;
}
public List<aOLIPlus1> getScheduleArray1()
{    
    return scheduleArray1;
}  

public PageReference gotoAcct()
{            
    String str = 'https://cs8.salesforce.com/'+ acct.id;
    PageReference newPage = new PageReference(str);
    newPage.setRedirect(false);
    return newPage;
}

//Using Seperate Wrapper classes get records from all categories
 public class aOLIPlus
{    
    public Opportunity iOLI {get; set;}
    public String iName{get;set;}
    public String iStageName {get; set;}
    public String iAmount {get;set;}
    public Date iCloseDate{get;set;}
    public String iAgency {get; set;}
    public String iRecordType {get; set;}
    public Boolean selected {get; set;}       

    public aOLIPlus(Opportunity aa,String aName, String aStageName, Decimal aAmount, Date aCloseDate,String aAgency,String aRecordTypeId,String aRecordType)
    {
      iOLI = aa;
      iName = aName;  
      iStageName = aStageName;
      iAmount = aAmount;
      iCloseDate = aCloseDate; 
      iAgency = aAgency;  
      iRecordType = aRecordType;
      selected = false;
    }    
}
public class aOLIPlus1
{    
    public Opportunity iOLI {get; set;}
    public String iName{get;set;}
    public String iStageName {get; set;}
    public String iAmount {get;set;}
    public Date iCloseDate{get;set;}
    public String iAgency {get; set;}
    public String iRecordType {get; set;}
    public Boolean selected {get; set;}       

    public aOLIPlus1(Opportunity aa, String aName, String aStageName, Decimal aAmount, Date aCloseDate,String aAgency,String aRecordTypeId,String aRecordType)
    {
      iOLI = aa;
      iName = aName;  
      iStageName = aStageName;
      iAmount = aAmount;
      iCloseDate = aCloseDate; 
      iAgency = aAgency;  
      iRecordType = aRecordType;
      selected = false;
    }    
}
public PageReference search(){
    //if(sortField =='')
    PageReference pageRef = new PageReference('/apex/OppSortView?id='+ acct.Id+'?sortField='+sortField+'?checkedVal='+checkedVal);
    //else if(sortfield == '')    
    pageRef.setRedirect(true);
    return pageRef;

}
}

Visual Force Page:

<apex:page standardController="Account" extensions="OppListView">
<apex:form >
<apex:sectionheader title="Orders for {!account.Name}"/> 
<apex:pageBlock >
     <div align='center'>
        <apex:commandButton action="{!gotoAcct}" value="Back to Account"/>
           </div>  
     <div align='right'>
           <apex:outputLabel value="Sort by" for="sortField">
            <apex:selectList value="{!sortField}" size="1">
                <!--apex:actionSupport event="onchange" action="{!search}"/!-->
                <apex:selectOption itemLabel="Close Date" itemValue="CloseDate"/>
                <apex:selectOption itemLabel="Opportunity Name" itemValue="OpportunityName"/>    
                <apex:selectOption itemLabel="Stage" itemValue="Stage"/>    
                <apex:selectOption itemLabel="Amount" itemValue="Amount"/> 
                <apex:selectOption itemLabel="Agency" itemValue="Agency"/> 
             </apex:selectList>
              </apex:outputLabel>
          <apex:commandButton action="{!search}" value="View"/>

         <apex:selectRadio value="{!checkedVal}">
            <!--apex:actionSupport event="onchange" action="{!search}"/!-->
                <apex:selectOption itemLabel="Ascending" itemValue="Ascending"/>
                <apex:selectOption itemLabel="Descending" itemValue="Descending"/>  
        </apex:selectRadio>
      </div> 
    <div style="overflow: scroll; height: 700px;" >  
     <apex:pageBlocktable value="{!scheduleArray}" var="e" columnsWidth="25%,15%,15%,15%30%">
     <apex:column >
    <apex:outputlink value="https://cs8.salesforce.com/{!e.iOLI.Id}" target="_top">{!e.iName}</apex:outputlink>
    <!-- apex:outputText value="{!e.iName}" /!-->
    </apex:column>
    <apex:column > <apex:outputText value="{!e.iStageName}" /></apex:column>
    <apex:column ><apex:outputText value="{!e.iAmount}" /></apex:column>
    <apex:column ><!--apex:outputText value="{!e.iCloseDate}" /!-->
        <apex:outputText value="{0, date, M/d/y }"><apex:param value="{!e.iCloseDate}"/>
            </apex:outputText></apex:column>
    <apex:column ><apex:outputText value="{!e.iAgency}" /></apex:column>
    <apex:column ><apex:outputText value="{!e.iRecordType}" /></apex:column>

</apex:pageBlocktable>   
         </div>
</apex:pageBlock>
</apex:form>
</apex:page>
3
  • 1
    Instead of posting your ALL code here you better describe the problem you are facing in details and mark the problem code snipped. Commented Feb 11, 2015 at 16:06
  • In the Visual Force page When Clicked on Ascending the page should display the in ascending order. something like ... string soql = 'Select Name, StageName, Amount, CloseDate, Agency__c, RecordTypeId, RecordType.Name From Opportunity Where AccountId = :acct.Id And RecordTypeId IN :orderRecordTypeIDs Order By ' if(CheckedVal =='ASC') { if(sortField=='CloseDate'{soql+='CloseDate'; soql+=' ASC';} else if(sortField=='Amount'){ soql+='Amount'; soql+=' ASC'; }} theOrders = DataBase.query(soql); but i cannot bind the the dynamic soql
    – naga42
    Commented Feb 11, 2015 at 16:16
  • @naga42 Unsolicited advice: I woudn't hardcode https://cs8... because if you refresh a sandbox or push to prod and forget about this value, no bueno... Why not something like this String baseURL = URL.getSalesforceBaseUrl().toExternalForm();
    – Grant
    Commented Oct 5, 2016 at 12:35

1 Answer 1

-1

Have a look at a blog done by Jeff Douglas:

http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/

@naga42 is right in terms of building the SOQL query with the additional search parameters. You do not appear to be utilizing any of the sort parameters that are passed into your controller. The attached post should give you the framework to accomplish what you are after.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .