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
Jos Vervoorn 2Jos Vervoorn 2 

List controller results by parent (case)

I'm working on a controller to list case related email.I'm struggling to get the SOQL query to limit results based on ParentID = case.id.

Without this everything works just fine.as a 'related' email list on the case page layout. What do I miss of what is wrong ?

The code of the controller:

public class CaseEmailListController {

    public CaseEmailListController(ApexPages.StandardController controller) {

    }

private ApexPages.StandardController stdCtrl {get; set;}
public List<Case> Cases {get; set;}

// Sorting on CreatedDate and ToAddress
private String sortOrder = 'CreatedDate';
public void sortByCreatedDate() {
    this.sortOrder = 'CreatedDate';
}  
public void sortByToAddress() {
    this.sortOrder = 'ToAddress';
}
    
public Case   ourCase {get; set;}
public List<EmailMessage> getCaseEmail() {

  List<EmailMessage> results = Database.query(
      'SELECT CreatedDate, FromAddress,HasAttachment,Id,ParentId,Subject,TextBody,ToAddress ' +
      'FROM EmailMessage ' + 'WHERE ParentID=:' + Case.ID +
      ' ORDER BY ' + sortOrder + ' ASC ' +
      'LIMIT 10'
    );

return results;
}
}
Best Answer chosen by Jos Vervoorn 2
Jos Vervoorn 2Jos Vervoorn 2
Thanks Raj,

After some coffee, soda and debugging I managed to find the key.

String CaseId = ApexPages.currentPage().getParameters().get('id');

Jos/

All Answers

Raj VakatiRaj Vakati

Hi Jos , 

Use the below code . 


String q = 'SELECT CreatedDate, FromAddress,HasAttachment,Id,ParentId,Subject,TextBody,ToAddress ' +
      'FROM EmailMessage '
      + 'WHERE ParentID=\'' +case.id+'\''+
      ' ORDER BY ' + 'FromAddress'  + ' ASC ' +
      'LIMIT 10' ;
System.debug('qqqqq'+q);
List<EmailMessage> mess = Database.query(q);
 

Thanks ,
Raj 
Jos Vervoorn 2Jos Vervoorn 2
Thanks Raj,

After some coffee, soda and debugging I managed to find the key.

String CaseId = ApexPages.currentPage().getParameters().get('id');

Jos/
This was selected as the best answer