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
Kevin HorwitzKevin Horwitz 

Issue with Passing variable from VF to Order By in Controller

I'm using a datatable in VF and I can normally get it to populate via my controller.  However, I need a dynamic variable in my SOQL to have the end user sort by the choice of headers in the table and I apparently keep passing NULL values to the string.  I'm a rookie with Apex and a novice with VF when it comes to working with Controllers, so I'm trying to figure out how to pass the myString value from VF to the SOQL in my Controller (code listed below).  Ideally I'd like to send this info via a commandlink in the column headers within in the datatable, but I'm fairly open to suggestions.  Thoughts?

Controller:

public class dataTableOli {
public String myString {get;set;}
String myquery =       
            'SELECT id,Opportunity.Product_Name__c,Opportunity.Account.Name,Opportunity.Account.Agency_Names__c,Opportunity.Account.Customer_Number__c,Opportunity.Account.Agency_Customer_ID__c,unitprice,opportunityid,Opportunity.StageName,Opportunity.Name,Opportunity.id,Opportunity.Previous_Expire__c,Opportunity.Previous_Amount__c,Opportunity.Product_Tier__c,Opportunity.Add_Print_Subscription__c,Name ' +
            'FROM OpportunityLineItem ' +
            'WHERE Opportunity.Account.Agency_Names__c=\'001j000000R7b8s\' and CALENDAR_YEAR(Opportunity.Previous_Expire__c) =2017 and CALENDAR_MONTH(Opportunity.Previous_Expire__c) =12  and Opportunity.Previous_Amount__c > 0 '+
            'ORDER BY '+MyString+ ' ASC Limit 10';
            
List<OpportunityLineItem> oli;
    public List<OpportunityLineItem> getoli() {

if(oli==NULL) 
   oli=Database.query(myquery);

      return oli;
}
   }

 
jigarshahjigarshah
Kevin,

There ae multiple ways to accomplish this but considering the behaviour you mentioned i.e. setting the sort order in the Apex controller when a user clicks the <apex:commandLink> tag in the table header, using an <apex:param> tag from Visualforce would work best.

Refer the following link to understand, how to use <apex:param> in Visualforce pages to pass data values from the UI to the Apex Controller - https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_param.htm

Sample Apex and Visualforce code to assign the values to a controller variable using <apex:param> is as below.

Apex Controller Code
public class MySampleController{

	public String sortOrder{get; set;}
	
	public void sortRecords(){
		//Code to sort the records goes here
	}
	
}

Visualforce code
<apex:page controller="MySampleController">
    <apex:commandLink action="{!sortRecords}" value="Test Link" id="theCommandLink"/>
        <apex:param name="sort" assignTo="{!sortOrder}" value="DESC"/>
    </apex:commandLink>
</apex:page>
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
Kevin HorwitzKevin Horwitz
Thank you for the response, but I think I wasn't clear on what I was looking for, though we're close.  ASC or DESC isn't the issue, it's passing the column name to sort to the Controller.  In other words, if I have a column header of 'Name' (which is the name of the field), I'd like to be able to click on that header to resort by that field.  Immediately next to Name would be another field name in the column header.  Let's say it's Closedate.  I'd like to be able to click on that field to sort and so on for the remainder of the headers.  Note that I'm not looking for the ability to do multi-level sorts (e.g. Sort by Name first and then sort by Closedate), rather just looking to be able to click on one header in the header row to resort the entire table and clicking on another header will resort the entire table again.  Does that make sense?  

My issue that I can't seem to pass the field name from VF to my Controller.  It keeps coming up as Null.
jigarshahjigarshah
Kevin,

Please find the sample code below that will help you pass the name of the column based using the <apex:param> tag.

Visualforce Code
<apex:page controller="MySampleController">

    <apex:form id="theForm">
        <apex:commandLink action="{!sortRecords}" value="Name" id="_name">
            <apex:param name="sortCol" value="Name"/>
        </apex:commandLink><br/>
        
        <apex:commandLink action="{!sortRecords}" value="Close Date" id="_closedate">
            <apex:param name="sortCol" value="Close Date"/>
        </apex:commandLink>
        
    </apex:form>
      
</apex:page>

Apex Controller Code
public with sharing class MySampleController {    
  
    public MySamplecontroller(){}

    public PageReference sortRecords() {
        System.debug('****' + ApexPages.currentPage().getParameters().get('sortCol'));
        return null;
    }

}

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.