Skip to content

Instantly share code, notes, and snippets.

@oporter1
Last active August 2, 2016 21:58
Show Gist options
  • Save oporter1/28dd2740b795661768ba8111b334c99d to your computer and use it in GitHub Desktop.
Save oporter1/28dd2740b795661768ba8111b334c99d to your computer and use it in GitHub Desktop.
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>
@apexlarson
Copy link

apexlarson commented Jul 28, 2016

Instead, use an inner class:

public class MyController
{
    public List<SObject> records { get; private set; }
    public MyController(...)
    {
        List<DateSorter> wrappers = new List<DateSorter>();
        for (Task record : [/*query*/]) ...
        for (Note record : [/*query*/]) ...
        records = new List<SObject>();
        for (DateSorter wrapper : wrappers) records.add(wrapper.record);
    }
    public class DateSorter implements Comparable
    {
        //...
    }
}

@apexlarson
Copy link

Rename MyController to DateSorter. Then in your noteandtask_controller change wrappers.add(new noteandtask_controller(...)) to wrappers.add(new DateSorter(...)).

@apexlarson
Copy link

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

@oporter1
Copy link
Author

oporter1 commented Aug 2, 2016

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