Reply
Regular Contributor
Sennah
Posts: 109

Another "Last Viewed" solution

[ Edited ]

Edit:

If you have trouble viewing this post

try to zoom out in your Browser via "STRG -".

 

Hi community,

 

a couple of weeks ago I got inspired by this blog post about a "Last Viewed by" solution for Accounts.

 

I put a little thinking in it to avoid the "last edited by" == "last viewed by" problem that comes with that. It needed to be something which does not edit the Account itself.

 

So what I did is set up a new object "Account Last View" and store the last view information as well as a relationship to the parent account in that and let the visualforce element at the account use that child element to display and update the "last view" information.

 

The output is not styled, so beside a discussion about this I would love to learn how to style the output without using the whole pageBlockSection stuff around it. I spent quite a while on it and did not find a proper solution. Maybe the only one is to copy the whole style via Firebug.

 

However, this is how it looks like:

 

 

And here's a walk-through how to set it all up:

 

First, set up a new custom object, similar to this. The first custom field is needed to have the child object being deleted once the account gets deleted and the other one for the upsert operation which takes places in the controller.

 

 

Now create the controller:

 

 

public with sharing class AccountLastViewController {

// Declare necessary variables
private final Account account;
public String LastViewedBy { set; }
public String oldLastViewedBy;

public AccountLastViewController(ApexPages.StandardController controller) {
this.account = (Account)controller.getRecord();
}

// Returns the "Last Viewed By" Information for the Visualforce element
public String getLastViewedBy() {
return oldLastViewedBy;
}

// The method called when the Visualforce element is loaded
public void setLastView() {

// To display the correct last view by it is stored in "oldLastViewedBy" and then returned by "getLastViewedBy()"
// First select the Information from the database
Account_Last_View__c[] oldLastView = [SELECT a.Id, a.LastModifiedBy.Name, a.LastModifiedDate
FROM Account_Last_View__c a
WHERE a.For_Upsert__c = :this.account.Id];
// To workaround the "list has no rows for assignment" problem we use this little if
if (oldLastView.size() > 0 ) {
// And finally create the String
// The ".format()" makes sure that the date/time is displayed in the correct format and time zone for each user
oldLastViewedBy = oldLastView[0].LastModifiedBy.Name + ', ' + oldLastView[0].LastModifiedDate.format();
}

// Now build the new last view
// We need the master-detail relationship to the account to make sure that this child is deleted when the account gets deleted
// and the "For_Upsert" to have a field which can be used by the upsert action
Account_Last_View__c LastView = new Account_Last_View__c (Account__c = this.account.Id, For_Upsert__c = this.account.Id);

// And then do an upsert.
try {
upsert LastView For_Upsert__c;
} catch (DmlException e) {System.debug(e.getMessage());
}

}

}

 

 And the visualforce element, which you can put in the Account page layout with a height of 20.

 

 

<apex:page standardcontroller="Account" extensions="AccountLastViewController" action="{!setLastView}" showheader="false" sidebar="false" tabStyle="Account">
<apex:outputLabel for="output">Last Viewed by: &nbsp; </apex:outputLabel>
<apex:outputText value="{!LastViewedBy}" id="output"/>
</apex:page>

 

 Just for the sake of completeness, here's a test class which just does the job to get you the percentage but should not be taken as best practice :smileywink:

Please notice that it first gets a RecordType as that is what we use in our org. you may remove that for your Salesforce.

 

 

@isTest
private class TestAccountLastViewController {

static testMethod void AccountLastViewTest() {
// Insert one Account
RecordType RT = [Select r.Id, r.Name from RecordType r where r.Name = 'Customer Account'];
Account acc = new Account(name = 'TestAccount', RecordTypeId = RT.Id);
insert acc;

ApexPages.StandardController sc = new ApexPages.StandardController(acc);

AccountLastViewController controller = new AccountLastViewController(sc);

controller.setLastView();
controller.getLastViewedBy();

}
}

 

 Curious about some feedback,

 

Hannes

 

 

 

Message Edited by Sennah on 11-24-2009 08:14 AM