-
-
Save oporter1/28dd2740b795661768ba8111b334c99d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class noteandtaskcontroller | |
{ | |
public List<SObject> record1 {get; private set;} | |
public final Datetime value; | |
public String actId; | |
public List<SObject> sortedRecords {get;set;} | |
//taking in parameter 'controller' to pull account Id for query | |
public noteandtaskcontroller(ApexPages.StandardController controller) | |
{ | |
actId = controller.getId(); | |
//creating a new list called wrappers | |
List<myController> wrappers = new List<myController>(); | |
//querying tasks | |
record1 = [Select CreatedById, Subject, Description, CreatedDate, Date_Activity_Logged__c | |
From Task | |
WHERE Status = 'Completed' AND (AccountId = :actId OR WhatId = :actId OR WhoId = :actId)]; | |
//looping through record1 aka tasks and then creating a new list called myController and adding the task's date activity logged. And then adding this list to the wrappers list. | |
for (SObject rec : record1) { | |
wrappers.add(new myController(rec, 'Date_Activity_Logged__c')); | |
} | |
//querying notes | |
record1 = [Select CreatedById, Title, Body, CreatedDate From Note WHERE ParentId =: actId]; | |
//looping through record1, now notes, and then creating a new list called myController and adding the note's CreatedDate. And then adding this list to the wrappers list. | |
for (SObject rec2 : record1) { | |
wrappers.add(new myController(rec2, 'CreatedDate')); | |
} | |
//sorting | |
wrappers.sort(); | |
sortedRecords = new List<SObject>(); | |
//looping through list 'wrapper' and adding to sortedRecords the wrapper's record----???? | |
for (myController wrapper : wrappers) sortedRecords.add(wrapper.record); | |
system.debug('this is the list: ' + wrappers); | |
} | |
public class myController implements Comparable | |
{ | |
private SObject record; | |
public Id id; | |
private Datetime value; | |
//creating new controller that takes in two params (record and field) | |
myController(SObject recordobj, String field) | |
{ | |
//pulling the record ie task or note being passed in- and pulling out the id, CreatedDate or Date_Activity_Logged__c, and the record name | |
this.record = recordobj; | |
this.id = recordobj.Id; | |
this.value = (Datetime)recordobj.get(field); | |
} | |
public Integer compareTo(Object instance) | |
{ | |
//comparing the param to the next spot in the list (the list always changes so we are comparing to an instance) | |
myController that = (myController)instance; | |
//if value is equal we return 0, if greated then we return 1, if less then we return -1, this automatically builds the list in order | |
if (this.value == that.value) return 0; | |
return (this.value < that.value) ? 1 : -1; | |
} | |
} | |
} | |
<--------------VF PG----------------> | |
<apex:page standardController="Account" extensions="noteandtask_controller"> | |
<apex:pageBlock > | |
<apex:pageBlockTable value="{!sortedRecords}" var="record"> | |
<apex:column headerValue="Title/Subject"> | |
<apex:variable var="field" value="{!IF(BEGINS(record.Id, '002'), 'Title', 'Subject')}" /> | |
<apex:outputField value="{!record[field]}" /> | |
</apex:column> | |
<apex:column headerValue="Body/Comment"> | |
<apex:variable var="field" value="{!IF(BEGINS(record.Id, '002'), 'Body', 'Description')}" /> | |
<apex:outputField value="{!record[field]}" /> | |
</apex:column> | |
<apex:column headerValue="Completed Date"> | |
<apex:variable var="field" value="{!IF(BEGINS(record.Id, '002'), 'CreatedDate', 'Date_Activity_Logged__c')}" /> | |
<apex:outputField value="{!record[field]}" /> | |
</apex:column> | |
<!--<apex:column headerValue="Created Date"> | |
<apex:outputField value="{!record['CreatedDate']}" /> | |
</apex:column>--> | |
<apex:column headerValue="Created By"> | |
<apex:outputField value="{!record['CreatedById']}" /> | |
</apex:column> | |
</apex:pageBlockTable> | |
</apex:pageBlock> | |
</apex:page> |
Change sortedRecords
to be a List<SObject>
property. Then after your sort call do:
sortedRecords = new List<SObject>();
for (DateSorter wrapper : wrappers) sortedRecords.add(wrapper.record);
Adrian, I am going back through the code to make sure I completely understand it. Can you explain a little further on:
wrappers.add(new myController(rec, 'Date_Activity_Logged__c'))
As of now how I am understanding it as: the for loop uses rec to look through the records1 list and then pull out any line item that has Date_Activity_Logged__c. I am unclear what the new myController does. And then I save these results into the list 'wrappers'.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rename
MyController
toDateSorter
. Then in yournoteandtask_controller
changewrappers.add(new noteandtask_controller(...))
towrappers.add(new DateSorter(...))
.