You need to sign in to do that
Don't have an account?
Kevin 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;
}
}
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;
}
}
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
Visualforce code Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
My issue that I can't seem to pass the field name from VF to my Controller. It keeps coming up as Null.
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 Controller Code
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.