You need to sign in to do that
Don't have an account?
brigitte carrabin
Hello VF/Apex Developers
I am a newbie and have what I think is a simple question for the gurus out there.
I have this VF code using a standard controller to display a list of records. I'd like to order the list from most recent entries to oldest (currently it lists them in the order they are created). I can also use the field Week_Ending_Date__c to sort if needed.
How would I accomplish this?
Thanks much for putting me on the right track!
<apex:page standardController="Services_Engagement__c">
<apex:form >
<c:MultiRecordComponent aParentRecId="{!Services_Engagement__c.Id}" asObjectType="Services_Engagement_Cost__c" aFieldList="Name,Week_Ending_Date__c,Labor_Hours__c,Labor_Rate__c,Labor_Cost__c"
aRelationField="Services_Engagement__c" aLabelOverrideFieldList=""
aLabelOverrideTextList="" aDefaultValueFieldList=""
aDefaultValueTextList="" aBlockTitle="Services Engagement Costs" aAllowAdd="true"
aAllowEdit="true" aAllowDelete="true" />
</apex:form>
</apex:page>
I have this VF code using a standard controller to display a list of records. I'd like to order the list from most recent entries to oldest (currently it lists them in the order they are created). I can also use the field Week_Ending_Date__c to sort if needed.
How would I accomplish this?
Thanks much for putting me on the right track!
<apex:page standardController="Services_Engagement__c">
<apex:form >
<c:MultiRecordComponent aParentRecId="{!Services_Engagement__c.Id}" asObjectType="Services_Engagement_Cost__c" aFieldList="Name,Week_Ending_Date__c,Labor_Hours__c,Labor_Rate__c,Labor_Cost__c"
aRelationField="Services_Engagement__c" aLabelOverrideFieldList=""
aLabelOverrideTextList="" aDefaultValueFieldList=""
aDefaultValueTextList="" aBlockTitle="Services Engagement Costs" aAllowAdd="true"
aAllowEdit="true" aAllowDelete="true" />
</apex:form>
</apex:page>
<c:MultiRecordComponent> is a custom component which can have its own controller.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_comp_cust_elements_controllers.htm
That is the most important part and you should post the code of this component because it is only in your org.
There are the apex classes, the visual force pages, the triggers and the page components (here: MultiRecordComponent).
Here are snippets of the MultiComponent class code.(It's too long for the post space so I extracted what heopfully are the relevant snippets where the list is being built.
I think I need to change the InitOrderByField = 'CreatedDate'; in the LoadObjectList class ??
public with sharing class MultiRecordComponentController {
public MultiRecordComponentController() {
DisableSave = true;
DisableCancel = true;
AddedRowCount = 0;
FieldList = new list<String>();
LabelOverrideFieldList = new list<String>();
LabelOverrideTextList = new list<String>();
DefaultValueFieldList = new list<String>();
DefaultValueTextList = new list<String>();
}
public String FieldListString {
get;
set {
if (null == value) return;
FieldList = String.valueOf(value).split(',');
}
}
* ColumnList - get/set methods. get initializes columns and list entries on first load
***/
public list<ColumnWrapper> ColumnList {
get {
if (ColumnWrapList == null) {
InitValues();
// load fields for table columns
ColumnWrapList = LoadColumnList(sObjectType, FieldList, LabelOverrideMap);
// load records in the table
ObjectList = LoadObjectList(ParentRecId, sObjectType, FieldList, RelationField, OrderByField);
}
return ColumnWrapList;
}
set;
}
* LoadObjectList - query the object, and load results into the object wrapper list
***/
public static list<ObjectWrapper> LoadObjectList(String InitRecId, String InitSObj, list<String> InitFieldList, String InitRelField, String InitOrderByField) {
list<ObjectWrapper> ObjWrapList = new list<ObjectWrapper>();
list<String> QueryFieldList = new list<String>();
set<String> QueryFieldSet = new set<String>();
// add id to field
QueryFieldList.addAll(InitFieldList);
QueryFieldSet.addAll(InitFieldList);
if (QueryFieldSet.contains('id')) {
QueryFieldList.add('id');
}
if (InitOrderByField == null || InitOrderByField.trim().length() == 0) {
InitOrderByField = 'CreatedDate';
}
String TmpQuery;
TmpQuery = 'Select ' + String.escapeSingleQuotes( String.join(QueryFieldList,', ') )+
' From ' + String.escapeSingleQuotes( InitSObj ) +
' Where ' + String.escapeSingleQuotes( InitRelField ) + '=\'' + String.escapeSingleQuotes( InitRecId ) + '\'' +
' Order by ' + String.escapeSingleQuotes( InitOrderByField ) +
' limit 1000';
system.debug('Query: ' + TmpQuery);
list<sObject> TmpObjectList = database.query(TmpQuery);
for (sObject o : TmpObjectList) {
ObjWrapList.add(new ObjectWrapper(o, false));
}
return ObjWrapList;
}
<apex:component controller="MultiRecordComponentController" allowDML="true">
<style>
.cmdLink {
font-size: 89%;
text-decoration: none;
float: left;
}
.cmdLink:hover {
text-decoration: underline;
}
</style>
<apex:attribute name="aParentRecId" description="Parent Record Id" type="String" required="true" assignTo="{!ParentRecId}" />
<apex:attribute name="aRelationField" description="Field that will be assigned the Parent's Record Id" type="String" required="true"
assignTo="{!RelationField}" />
<apex:attribute name="asObjectType" description="Type of child Object." type="String" required="true" assignTo="{!sObjectType}" />
<apex:attribute name="aFieldList" description="List of fields to display." type="string" required="true" assignTo="{!FieldListString}" />
<apex:attribute name="aAllowAdd" description="Ability to add new records." type="Boolean" required="false" assignTo="{!AllowAdd}" />
<apex:attribute name="aAllowEdit" description="Ability to edit records" type="Boolean" required="false" assignTo="{!AllowEdit}" />
<apex:attribute name="aAllowDelete" description="Ability to delete records" type="Boolean" required="false" assignTo="{!AllowDelete}" />
<apex:attribute name="aLabelOverrideFieldList" description="List of fields with overridden labels" type="String" required="false"
assignTo="{!LabelOverrideFieldListString}" />
<apex:attribute name="aLabelOverrideTextList" description="List of text that overrides the field labels" type="String" required="false"
assignTo="{!LabelOverrideTextListString}" />
<apex:attribute name="aDefaultValueFieldList" description="List of fields used to set default values on added records" type="String"
required="false" assignTo="{!DefaultValueFieldListString}" />
<apex:attribute name="aDefaultValueTextList" description="List of text used to set default values on added records" type="String"
required="false" assignTo="{!DefaultValueTextListString}" />
<apex:attribute name="aBlockTitle" description="Page block title text" type="String" required="false" />
<apex:actionFunction name="DoDeleteJS" action="{!DoDelete}" rerender="pbContainer,msgs" immediate="true">
<apex:param name="ActionId" assignto="{!ActionId}" value="" />
</apex:actionFunction>
<apex:actionFunction name="DoRemoveJS" action="{!DoRemove}" rerender="pbContainer,msgs" immediate="true">
<apex:param name="ActionRowNumber" assignto="{!ActionRowNumber}" value="" />
</apex:actionFunction>
<apex:pageMessages id="msgs" />
<apex:pageBlock id="pbContainer" title="{!aBlockTitle}">
<apex:pageBlockButtons location="top">
<apex:actionStatus id="ButtonStatus">
<apex:facet name="stop">
<apex:outputPanel >
<apex:commandButton rerender="pbContainer,msgs" status="ButtonStatus" value="Add" action="{!DoAdd}" immediate="true"
rendered="{!AllowAdd}" />
<apex:commandButton rerender="pbContainer,msgs" status="ButtonStatus" value="Save" action="{!DoSave}"
rendered="{!OR(AllowEdit,AllowAdd)}" disabled="{!DisableSave}" />
<apex:commandButton rerender="pbContainer,msgs" status="ButtonStatus" value="Cancel" action="{!DoCancel}" immediate="true"
rendered="{!OR(AllowEdit,AllowAdd)}" disabled="{!DisableCancel}" />
</apex:outputPanel>
</apex:facet>
<apex:facet name="start">
<apex:outputPanel >
<apex:commandButton value="Processing..." disabled="true" rendered="{!AllowAdd}" />
<apex:commandButton value="Processing..." disabled="true" rendered="{!OR(AllowEdit,AllowAdd)}" />
<apex:commandButton value="Processing..." disabled="true" rendered="{!OR(AllowEdit,AllowAdd)}" />
</apex:outputPanel>
</apex:facet>
</apex:actionStatus>
</apex:pageBlockButtons>
<apex:pageBlockTable id="pbTable" value="{!ObjectList}" var="ow">
<apex:column headerValue="Action" width="71px" rendered="{!OR(AllowEdit,AllowDelete,AllowAdd)}">
<apex:outputPanel rendered="{!ISBLANK(ow.obj.Id)}">
<apex:outputLink style="color: #015BA7;" styleClass="cmdLink" value="javascript:DoRemoveJS('{!JSENCODE(ow.AddedRowNumber)}');">Remove</apex:outputLink>
</apex:outputPanel>
<apex:outputPanel rendered="{!!ISBLANK(ow.obj.Id)}">
<apex:commandLink style="color: #015BA7;" styleClass="cmdLink" value="Edit" action="{!DoEdit}" immediate="true"
rendered="{!AllowEdit}" rerender="pbContainer,msgs">
<apex:param name="RecId" value="{!ow.obj.Id}" assignTo="{!ActionId}" />
</apex:commandLink>
<apex:outputPanel style="display:inline;float: left; margin: 0 2px 0 2px" rendered="{!AND(AllowEdit,AllowDelete)}"> | </apex:outputPanel>
<apex:outputLink style="color: #015BA7;" styleClass="cmdLink"
value="javascript:if (window.confirm('Are you sure?')) DoDeleteJS('{!JSENCODE(ow.obj.Id)}');" rendered="{!AllowDelete}">Del</apex:outputLink>
</apex:outputPanel>
</apex:column>
<apex:repeat value="{!ColumnList}" var="cf">
<apex:column width="200">
<apex:facet name="header">
<span>{!cf.FieldLabel}</span>
</apex:facet>
<apex:outputPanel rendered="{!OR(!ISBLANK(ow.obj['Id']), AND(ISBLANK(ow.obj['Id']),cf.IsObjField))}">
<apex:outputField value="{!ow.obj[cf.FieldName]}" rendered="{!!AND(ow.IsEditMode,cf.IsEditable)}" />
<apex:InputField value="{!ow.obj[cf.FieldName]}" rendered="{!AND(ow.IsEditMode,cf.IsEditable)}" />
</apex:outputPanel>
</apex:column>
</apex:repeat>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:component>
Create an attibute for sorting field and use it for sorting ..
In Component
In Apex Class ..
define the set and get
and in LoadObjectList method insted of agument , you can get from get and set or from the component invoking properties
Change page as below