+ Start a Discussion
Brandon NelsonBrandon Nelson 

Pull ContentNotes using APEX/SOQL

I have a requirement to share a note with multiple accounts. My first issue I was trying to solve is being able to store the notes data in variables then create new records based off that data. I have a trigger on ContentDocumentLink that passes info off to a class. 

Problem is, if I save the note before it auto saves, everything works. If I let the record auto save, I dont get the body of the note. 
 
trigger trasnferNotes on ContentDocumentLink (after insert) {
    
    string relatedID;
    String documentID;
    
    //Get the current ID for the note
    if (Trigger.isAfter) {
        if (Trigger.isInsert) {
            for(ContentDocumentLink cdl : Trigger.New) {
                relatedID = cdl.LinkedEntityId;
                documentID = cdl.ContentDocumentId;
                transferNotesClass db = new transferNotesClass(relatedID, documentID);
            }
        }
    }
    
}
 
global class transferNotesClass {
    
    global String noteID;
    global String contentDocumentID;
    global String cvID;
    
    global transferNotesClass(String relatedID, String documentID) {
        
        try {
            
            noteID = relatedID;
            contentDocumentID = documentID;
            
            System.debug('Class Note ID: ' + noteID);
            
            
            //Getting the ID from ContentVersion
            List<ContentVersion> cv = New List<ContentVersion>([select id from ContentVersion 
                                                                where ContentDocumentId = :contentDocumentID]);
            for(ContentVersion cv1 : cv) {
                cvID = cv1.Id; 
            }
            
            //Getting the Content Note by passing the above ID to ContentNote
            List<ContentNote> contNote = New List<ContentNote>([select Content, ContentSize, CreatedById, CreatedDate, FileExtension, FileType, 
                                                                Id, IsDeleted, IsReadOnly, LastModifiedById, LastModifiedDate, LastViewedDate, 
                                                                LatestPublishedVersionId, OwnerId, SharingPrivacy, TextPreview, Title 
                                                                from ContentNote where LatestPublishedVersionId = :cvID]);
            
            for(ContentNote contNote1 : contNote) {
                Blob contentBlob = contnote1.Content;
                String contentString = contentBlob.toString();
                List<String> csvFileLines= contentString.split('\n');
                system.debug(csvFileLines);
                System.debug('Note Title: ' + contNote1.Title);
                //System.debug('Note Content: ' + csvFileLines);
            }
            
        }
        
        catch(DmlException e) {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
        
    }
    
}

 
satish090satish090
JEE Main and JEE Advanced are the two gateways that students need to cross in order to enter the elite IITs.Only the top 150000 rankers of JEE Main (https://www.admissionadvice.in/JEE-Main-Rank-Predictor-2019) are eligible to sit for JEE Advanced,
Christan G 4Christan G 4
- Not sure if this is helpful, but I noticed in your trigger statement that you only included "after insert". I think in this case, you should also include "after update" since when a note auto saves, I assume Salesforce is considering it as an update. Nothing will happen since the trigger is not specified to fire after the update occurred. 
- When you say share a note with multiple accounts, how do you know which accounts should the note be shared with and which ones should not? Is there a specific criteria mentioned?
- Quote: "I was trying to solve is being able to store the notes data in variables..."  - Assuming that there is a separate object being used to store these notes, why would it be necessary to separate its contents into other variables. I assume the best way would be to clone each note and then associate the new cloned notes to the multiple accounts that need it. I apologize if I misunderstood what you meant
 
VIPIN KUMAR 91VIPIN KUMAR 91
Admission Guidance is premium education web portal and 
trusted by a large number of students.aftr neet 2020 exam mcc going to conduct mbbs bds counselling ( https://cbseneet2019.co.in/mbbs-bds-counselling/ ) for the mbbs admissiopn in india .

 
Aayush K 7Aayush K 7
I see that you only used the "after insert" as the trigger. But I think you should also include "after update" in this example because I imagine Salesforce considers an auto-saved note to be an update. Because the trigger is not set to fire after the upgrade, nothing will happen.

Check out this post for better understanding: ExactlyHowLong (https://exactlyhowlong.com/how-long-to-learn-c-after-python-and-why/)