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
Terri HenryTerri Henry 

Test Class coverage for code tracking User Record Views

Hi All, 

I've amended a solution for tracking page views I found here:
https://success.salesforce.com/answers?id=9063A000000Dq6MQAS

I'm using it to track who has viewed an Opportunity record page - however I have no idea where to start with the test class - I'm currently working through a couple of trailhead modules, though any pointers would be greatly appreciated

Thanks

Terri 
 
public without sharing class lastViewedOpportunity{
    public Datetime cDT;
    public String LongDate;
    public String firstname;
    public String lastname;
    public String existinglastviewed;
    public String userid;
    private final Opportunity opp;
    
    public lastViewedOpportunity(ApexPages.StandardController stdController) {
        this.opp = (Opportunity)stdController.getRecord();
    }
    
    public String getLongDate() {
        cDT = System.now();
        //Formats the datetime value to locale
        LongDate = cDT.format('dd/MM/yyyy HH:mm');
        return LongDate;
    }
    
    
    public void updateField() {
        //Get the user info from the current user
        firstname = System.Userinfo.getFirstName();
        lastname = System.Userinfo.getLastName();
        userid = System.Userinfo.getUserId();
        //Get the Opportunity record to be updated
        Opportunity o = [select Last_Viewed_By__c, Track_Record__c from Opportunity where id = :opp.id limit 1];
        //get existing string of the record, so that new view can be appended to the end of this
        existinglastviewed = o.Last_Viewed_By__c;
        //check to see if the Opportunity has been flagged for tracking
        if(o.Track_Record__c){
            //Assign values to Last Viewed By field and update the record - only concatenate if there is a value there already
            if(existinglastviewed != null){
                o.Last_Viewed_By__c = (existinglastviewed + '; \n' + firstname + ' ' + lastname + ' ' + getLongDate());
            }
            else o.Last_Viewed_By__c = (firstname + ' ' + lastname + ' ' + getLongDate());
            update o;
        }    
    }
    
}

 
Best Answer chosen by Terri Henry
Steven NsubugaSteven Nsubuga
Sorry for that Terri, try this.
@isTest
public with sharing class lastViewedOpportunityTest {


    static testMethod void triggerTest() {
		
		Account testAccount = new Account(Name = 'testAccount');
		insert testAccount;
		
        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccount.Id,
			Track_Record__c = true,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating'
        );
        insert testOpportunity;
		
		ApexPages.StandardController sc = new ApexPages.StandardController(testOpportunity);
		lastViewedOpportunity lastViewedOpp = new lastViewedOpportunity(sc);
		
		System.assert(!String.isEmpty(lastViewedOpp.getLongDate()));
		lastViewedOpp.updateField();
	}	

}

 

All Answers

Steven NsubugaSteven Nsubuga
Hi Terri, 
something like this should work.
@isTest
public with sharing class lastViewedOpportunityTest {


    static testMethod void triggerTest() {
		
		Account testAccount = new Account(Name = 'testAccount');
		insert testAccount;
		
        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccount.Id,
			Track_Record__c = true,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating'
        );
        insert testOpportunity;
		
		ApexPages.StandardController sc = new ApexPages.StandardController(testOpportunity);
		lastViewedOpportunity lastViewedOpp = new lastViewedOpportunity(sc);
		
		System.assert(lastViewedOpp.getLongDate() != String.isEmpty());
		lastViewedOpp.updateField();
	}	

}

 
Terri HenryTerri Henry
Thank you for your help Steven!

Using the above I get the error:
Error: Compile Error: Method does not exist or incorrect signature: void isEmpty() from the type String at line 22 column 61

I tried replacing with isBlank(), same error - should I have anything in the parameters there ?
Steven NsubugaSteven Nsubuga
Sorry for that Terri, try this.
@isTest
public with sharing class lastViewedOpportunityTest {


    static testMethod void triggerTest() {
		
		Account testAccount = new Account(Name = 'testAccount');
		insert testAccount;
		
        Opportunity testOpportunity = new Opportunity(
            Name = 'Test Opportunity',
			AccountId = testAccount.Id,
			Track_Record__c = true,
            CloseDate = date.today().addDays(45),         
            StageName = 'Negotiating'
        );
        insert testOpportunity;
		
		ApexPages.StandardController sc = new ApexPages.StandardController(testOpportunity);
		lastViewedOpportunity lastViewedOpp = new lastViewedOpportunity(sc);
		
		System.assert(!String.isEmpty(lastViewedOpp.getLongDate()));
		lastViewedOpp.updateField();
	}	

}

 
This was selected as the best answer
Terri HenryTerri Henry
My hero! 94% code coverage Steven - thank you for your help!
Steven NsubugaSteven Nsubuga
My pleasure Terri!!