On a visualforce page I want to display notes from an opportunity. To accomplish this I need a VF-page and an extension class (apex).
Part of VF-page that displays the notes:
<apex:outputPanel>
<apex:pageBlock title="Notes" mode="maindetail">
<apex:pageBlockTable value="{!MainOppNotes}" var="n">
<apex:column headerValue="Action" width="5%">
<apex:commandLink action="{!viewNote}" id="viewNote"
value="View" styleClass="actionLink">
<apex:param name="noteId" assignTo="{!noteId}" value="{!n.Id}" />
</apex:commandLink>
</apex:column>
<apex:column headerValue="Name" width="65%">{!n.Title}</apex:column>
<apex:column headerValue="Last Modified" width="30%">
<apex:outputtext value="{!n.LastModifiedByName}" />,
<apex:outputtext value="{!n.LastModifiedDateStr}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:outputPanel>
The getMainOppNotes method from the extension class:
// Get notes from the main opportunity.
public List<RetObject> getMainOppNotes() {
List<RetObject> rtObjs = new List<RetObject>();
RetObject rt = new RetObject();
List<Note> mainOppNotes =
[select Id, Title, LastModifiedDate, LastModifiedById
from Note where ParentId = :mainOppId];
for (Note n : mainOppNotes) {
rt = new RetObject();
rt.Id = n.Id;
rt.Title = n.Title;
rt.LastModifiedDate = n.LastModifiedDate;
rt.LastModifiedByName = userMap.get(n.LastModifiedById);
rtObjs.add(rt);
}
rt = null;
return rtObjs;
}
In the method above there is first a SOQL-query to get all notes for a specific opportunity. I want to return Id, Title, LastModifiedDate and LastModifiedBy name for the Note. Since Note has a field LastModifiedById we can only get the id which is not very readable.
A solution to this is to create an wrapper class to the extension class which can be used as an internal object that can hold all the values I wan’t to return to the VF-page. In this case it is called RetObject.
For each Note an instance of RetObject is created and put in a list of the RetObject type. The method is set to return a list of this type and that is what is done when the looping has finished.
The RetObject type contains 4 fields. One is the LastModifiedByName. All fields except this field get their values from a Note. The LastModifiedByName field gets its value from the user map. The usermap consists of a map of type String and Id and contains all active users in the org. This way we can do a quick lookup using the LastModifiedById from the Note.
The user map is created in the constructor and is defined as a private class attribute:
// Create user map
List<User> users = [select id,name from User where isActive = true];
userMap = new Map<ID, String>();
for (User u : users) {
userMap.put(u.id, u.name);
}
The wrapper class looks like this:
// Wrapper class. Used to return a set of information to
// display to the user on the VF-page
public class RetObject {
public RetObject() {}
public ID Id {get; set;}
public String Name {get; set;}
public String Title{get; set; }
public Datetime LastModifieddate {get; set; }
public String LastModifiedDateStr {get {
return LastModifieddate.format('yyyy-MM-dd hh:mm');
} set; }
public String LastModifiedByName {get; set;}
}
Tagged as:
apex,
extension,
visualforce,
wrapper class