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
LoïcLoïc 

Display last email received in a VF page issue asc vs desc

Hello,

 

I'm trying to display the last email received linked to a case in a VF page in an html format. 

 

This code is working great to display the first email received : 

 

public with sharing class CaseHTMLEmailController {
    private final Case caseObj;

    public String firstHTMLEmail { get 
        {return getFirstHTMLEmail(); }
    }
    
    public CaseHTMLEmailController(ApexPages.StandardController stdController) {
        this.caseObj = (Case)stdController.getRecord();
    }

    public String getFirstHTMLEmail() {
        EmailMessage firstEmail = [Select HtmlBody From EmailMessage where ParentId=:caseObj.Id order by LastModifiedDate asc limit 1];
        if (firstEmail!=null) {
            return firstEmail.HtmlBody;
        }
        
        return '';
    }
}

 But if I change the "order by LastModifiedDate asc limit 1" by "desc", to display the last email received, it won't display any records (EmailMessage HtmlBody) :

 

public with sharing class CaseHTMLEmailController {
    private final Case caseObj;

    public String firstHTMLEmail { get 
        {return getFirstHTMLEmail(); }
    }
    
    public CaseHTMLEmailController(ApexPages.StandardController stdController) {
        this.caseObj = (Case)stdController.getRecord();
    }

    public String getFirstHTMLEmail() {
        EmailMessage firstEmail = [Select HtmlBody From EmailMessage where ParentId=:caseObj.Id order by LastModifiedDate desc limit 1];
        if (firstEmail!=null) {
            return firstEmail.HtmlBody;
        }
        
        return '';
    }
}

 Any suggestion would be great and helpful.

 

Thanking you in advance for your help.

Loïc.