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
brigitte carrabinbrigitte 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>

 
Alain CabonAlain Cabon
Hi,

<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).

User-added image

 
Raj VakatiRaj Vakati
Can you give me the code "MultiRecordComponent" component .. 
brigitte carrabinbrigitte carrabin
Thanks much Raj & Alain.
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;
    }


 
brigitte carrabinbrigitte carrabin
And here is the VF component:
<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>
Raj VakatiRaj Vakati
Go one think .. 

Create an attibute for sorting field and use it for sorting  .. 


In Component 
 
<apex:attribute name="sortField" description="Sotring field " type="String" required="false"

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 

 
<apex:page standardController="Services_Engagement__c">
   <apex:form >
      <c:MultiRecordComponent aParentRecId="{!Services_Engagement__c.Id}" asObjectType="Services_Engagement_Cost__c"  sortField="Name" 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>

 
brigitte carrabinbrigitte carrabin
Makes sense Raj. Let me give this a try. Thanks!