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
Jive JIFJive JIF 

Querying all notes related to a Contact

Hi,

I'm trying to query all notes related to a contact, displaying these notes with title and textpreview and also order them by date. I also need this to be pageable.

I'm using SOQL. Now, my  solution at first was that I select all item from the ContentDocumentLink table which has the contactId I'm working on. With this I could get all ContentDocumentId for this contact (obviously these are not all Notes). The next step would be to query the ContentVersion table where I can use the ContentDocumentIds from the previous step and filter out items where the FileType are not "SNOTE".

However this solution is not really good for what I want to achieve as I still lacks the paging feature. I could use limit/offset with the ContentDocumentLink table but there not every item are Note.

I also try to worl with the ContentNote table directly but I still would need to get it to pair them to the contact so I'm kind of stucked.

Is there a simpler way to achieve this?

Thanks for the answers!
Best Answer chosen by Jive JIF
Khan AnasKhan Anas (Salesforce Developers) 
Hi Jive,

You can get the ContentNote details using relationship fields by querying on ContentDocumentLink object and filtering it with LinkedEntityId
Set<Id> contactIds= new Set<Id>(); 
contactIds.add('0030H000059CBKAQA4'); //include all the contact ids for which you need to retrieve content note
List<ContentDocumentLink> conLinks = [select Id, LinkedEntityId,ContentDocumentId, ContentDocument.LatestPublishedVersion.Title,ContentDocument.LatestPublishedVersion.VersionData from ContentDocumentLink where LinkedEntityId IN :contactIds];
system.debug(conLinks); //this will give only fields from ContentDocumentLink
system.debug(conLinks[0].LinkedEntityId); //this will give LinkedEntityId, in your case it will be Contact's ID
system.debug(conLinks[0].ContentDocument.LatestPublishedVersion.Title); //to access ContentNote's title
system.debug(conLinks[0].ContentDocument.LatestPublishedVersion.VersionData); //to access ContentNote's VersionData

Please refer to the below links which might help you further:
https://salesforce.stackexchange.com/questions/263808/how-to-fetch-contentnote-records-as-per-the-contact-record

https://salesforce.stackexchange.com/questions/138727/query-all-content-notes-of-an-opportunity

I hope it helps you.

Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Jive,

Greetings to you!

You can use the below SOQL to get all the notes related to Contact:
SELECT Id, Parent.Id, Parent.Type 
               FROM Note 
               WHERE Parent.Type='Contact'

Please refer to the below links which might help you further with the above requirement.

https://medium.com/@krissparks/soql-query-for-notes-on-an-object-a94221f61083

https://salesforce.stackexchange.com/questions/194743/soql-query-to-get-all-the-note-of-a-account

https://salesforce.stackexchange.com/questions/248812/what-is-the-right-way-to-query-all-notes-attachments

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi,

* You can use the below SOQL to get all the notes related to Contact:

  SELECT Id, Parent.Type FROM Note WHERE Parent.Type='Contact'

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Jive JIFJive JIF
Hi all,

Thanks for the answers, however I want to query ContentNote-s, not Notes objects, so in my case the provided answers unfortunately won't working. Do you happen to have a solution for that type of objects? Thank you again and have a nice day!
Deepali KulshresthaDeepali Kulshrestha
Hi Jive,


Greetings to you!

I have gone through your query.

Please try below query:

List<Note> Notes_List  = [select Id,Parent.Id,Parent.Type from Note where Parent.Type='Contact'];

Please visit the link for detailed explanation:

1. https://medium.com/@krissparks/soql-query-for-notes-on-an-object-a94221f61083

2. https://salesforce.stackexchange.com/questions/248812/what-is-the-right-way-to-query-all-notes-attachments
     
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Khan AnasKhan Anas (Salesforce Developers) 
Hi Jive,

You can get the ContentNote details using relationship fields by querying on ContentDocumentLink object and filtering it with LinkedEntityId
Set<Id> contactIds= new Set<Id>(); 
contactIds.add('0030H000059CBKAQA4'); //include all the contact ids for which you need to retrieve content note
List<ContentDocumentLink> conLinks = [select Id, LinkedEntityId,ContentDocumentId, ContentDocument.LatestPublishedVersion.Title,ContentDocument.LatestPublishedVersion.VersionData from ContentDocumentLink where LinkedEntityId IN :contactIds];
system.debug(conLinks); //this will give only fields from ContentDocumentLink
system.debug(conLinks[0].LinkedEntityId); //this will give LinkedEntityId, in your case it will be Contact's ID
system.debug(conLinks[0].ContentDocument.LatestPublishedVersion.Title); //to access ContentNote's title
system.debug(conLinks[0].ContentDocument.LatestPublishedVersion.VersionData); //to access ContentNote's VersionData

Please refer to the below links which might help you further:
https://salesforce.stackexchange.com/questions/263808/how-to-fetch-contentnote-records-as-per-the-contact-record

https://salesforce.stackexchange.com/questions/138727/query-all-content-notes-of-an-opportunity

I hope it helps you.

Regards,
Khan Anas
This was selected as the best answer
Jive JIFJive JIF
Hello Khan Anas,
That did the trick thank you very much!
Regards,
Istvan Reiter