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
ManojKumar MuthuManojKumar Muthu 

Apex Code for to list view all notes related a Case.

Hi All ,

Hi,
I am trying to list all case created to particular account here the parameter is the CaseId, below the apex code,
below the code,

RestResource(urlMapping='/NotesListView/*')
global with sharing class NotesListView{

@HttpGet
    global static List<Note> getNoteById() {
       RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String Id= req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
          List<Note> result = [Select Id, Title, Body from Note where ParentId= :Id];
        return result;
    }
}

Where I Pass CaseId in ParentId.
Best Answer chosen by ManojKumar Muthu
PawanKumarPawanKumar
As per the new enhancement, the object is ContentDocumentLink ,ContentDocument,ContentVersion for notes query. Please try below to get the correct result.

---------------------
RestResource(urlMapping='/NotesListView/*')
global with sharing class NotesListView{

@HttpGet
    global static List<ContentDocumentLink> getNoteById() {
       RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String Id= req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        List<ContentDocumentLink> result = [SELECT ContentDocumentId,ContentDocument.Title FROM ContentDocumentLink WHERE LinkedEntityId=:Id];
        return result;
    }
}
--------------------------------

Please mark it best if it helps you. Thanks.

All Answers

PawanKumarPawanKumar
can you please run the below query in your developer console? and plz share the output.

Select Id, Title, Body from Note where ParentId= 'YourCaseID'
ManojKumar MuthuManojKumar Muthu
Below the Output,

User-added image
ManojKumar MuthuManojKumar Muthu
If I pass AccountId its working fine, but when I Pass Caseid It returns no value.
PawanKumarPawanKumar
As per the new enhancement, the object is ContentDocumentLink ,ContentDocument,ContentVersion for notes query. Please try below to get the correct result.

---------------------
RestResource(urlMapping='/NotesListView/*')
global with sharing class NotesListView{

@HttpGet
    global static List<ContentDocumentLink> getNoteById() {
       RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String Id= req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        List<ContentDocumentLink> result = [SELECT ContentDocumentId,ContentDocument.Title FROM ContentDocumentLink WHERE LinkedEntityId=:Id];
        return result;
    }
}
--------------------------------

Please mark it best if it helps you. Thanks.
This was selected as the best answer
ManojKumar MuthuManojKumar Muthu
If there a way to display the Body of the Note along with that, I tried but it's not working 


 
PawanKumarPawanKumar
Yes, you can get the body as well.

// query where CaseId='5003C000001cdzr'
SELECT ContentDocumentId,ContentDocument.Title FROM ContentDocumentLink WHERE LinkedEntityId='5003C000001cdzr'

Select Id,VersionData from ContentVersion where ContentDocumentId='ContentDocumentId'

VersionData(from the second query) will give you the body of the Note.

To get the Body as text string use below code.
String afterblob = EncodingUtil.base64Encode(VersionData);