+1 vote
12 views
by (163k points)

I'll start off by saying that I am in no way a Developer, and the code I am posting is from things I have found online and recycled for my use-case. It's working, but I know it's not structured effeciently.

My business case is to update 3 fields on an Account when a new Enhanced Note is linked to the Account. The 3 fields I need to update is a date/time field to capture when the Account was linked to the Note, the title of the Note, and the body content of the Note. Below is the code that I have so far which is correctly populating the data for the first 2 fields (date/time and title).

Apex Trigger:

trigger ContentDocumentLinkTrigger on ContentDocumentLink  (after insert) {

    if (trigger.isAfter && trigger.isInsert) {
        ContentDocumentLinkHandler.newNote(trigger.new);
    }

}

Apex Class:

Public class ContentDocumentLinkHandler {

  public static void newNote (list<ContentDocumentLink> noteCreated) {
    Set<Id> setRelatedId = new Set<Id>();

    for(ContentDocumentLink objLink : noteCreated){
      if(String.IsNotBlank(objLink.LinkedEntityId)){
        setRelatedId.add(objLink.LinkedEntityId);
      }
    }

    // Query the records only to get ContentDocumentLink which ContentDocument's file type is SNOTE.
    List<ContentDocumentLink> lstDocLink = [Select LinkedEntityId, ContentDocument.CreatedDate, ContentDocument.Title, ContentDocument.LatestPublishedVersion.TextPreview from ContentDocumentLink where ContentDocument.FileType = 'SNOTE' and LinkedEntityId in : setRelatedId and Id in: Trigger.New];
    
    setRelatedId.clear();
    
    //CREATEDDATE
    // Create a map to hold Related ID and Note created Date.
    Map<Id, DateTime> mapRecodToNoteCreatedDate = new Map<Id, DateTime> ();
    for(ContentDocumentLink objLink : lstDocLink){
      if(String.IsNotBlank(objLink.LinkedEntityId)){
        mapRecodToNoteCreatedDate.put(objLink.LinkedEntityId, objLink.ContentDocument.CreatedDate);
      }
    }

    // Update the related Records with Note created date and Note linked with relatd record date.
    List<SObject> lstObject = new List<SObject>();
    Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    for(Id recordId : mapRecodToNoteCreatedDate.keySet()){
      String objectType = String.valueOf(recordId.getsobjecttype());
      SObject objSobject = schemaMap.get(objectType) != null ? schemaMap.get(objectType).newSObject() : null;
      if(objSobject != null && objectType == 'Account'){ // Only for Account object
        objSobject.put('id', recordId);
        objSobject.put('Latest_Note_Created_Date__c', mapRecodToNoteCreatedDate.get(recordId));
        lstObject.add(objSobject);
      }
    }
    try{
      Database.update(lstObject, false);
    }
    catch(Exception ex){
      System.debug('Something happend wrong. Here are the details: ' + ex.getMessage());
    }
    
    //TITLE
    // Create a map to hold Related ID and Note Title.
    Map<Id, String> mapRecodToNoteTitle = new Map<Id, String> ();
    for(ContentDocumentLink objLink : lstDocLink){
      if(String.IsNotBlank(objLink.LinkedEntityId)){
        mapRecodToNoteTitle.put(objLink.LinkedEntityId, objLink.ContentDocument.Title);
      }
    }

    // Update the related Records with Note created date and Note linked with relatd record date.
    List<SObject> lstObject2 = new List<SObject>();
    Map<String, Schema.SObjectType> schemaMap2 = Schema.getGlobalDescribe();
    for(Id recordId : mapRecodToNoteTitle.keySet()){
      String objectType = String.valueOf(recordId.getsobjecttype());
      SObject objSobject = schemaMap2.get(objectType) != null ? schemaMap2.get(objectType).newSObject() : null;
      if(objSobject != null && objectType == 'Account'){ // Only for Account object
        objSobject.put('id', recordId);
        objSobject.put('Recent_Note_Title__c', mapRecodToNoteTitle.get(recordId));
        lstObject2.add(objSobject);
      }
    }
    try{
      Database.update(lstObject2, false);
    }
    catch(Exception ex){
      System.debug('Something happend wrong. Here are the details: ' + ex.getMessage());
    }

  }
}

Apex Test Class:

@IsTest
Public class ContentDocumentLinkHandlerTest {

    static testmethod void testmethod1(){
        Test.startTest();

        Account accA = new Account(Name='Demo');
        insert accA;

        ContentNote note=new ContentNote();
        note.Title='Test File';
        Blob noteBlob=Blob.valueOf('Unit Test ContentNote Body');
        note.Content=noteBlob;
        note.SharingPrivacy='N';
        insert note;

        ContentVersion content=new ContentVersion();
        content.Title='Test File';
        content.PathOnClient='Test File.snote';
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body');
        content.VersionData=bodyBlob;
        content.Origin = 'H';
        content.SharingPrivacy='N';
        content.ContentDocumentId=note.Id;
        insert content;

        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=accA.Id;
        contentlink.ContentDocumentId=[select ContentDocumentId from ContentVersion where Id =: content.Id].ContentDocumentId;
        contentlink.ShareType = 'V';
        contentlink.Visibility = 'AllUsers';
        insert contentlink;

        Test.stopTest();

    }
}

I have been trying to get the Note's content added in so that it would update a "Recent_Note_Content__c" rich text Account field, but I can't get a handle on it because of the Note field's data type not being string. Any thoughts on how I might be able to pull that field in to successfully update the Account with the Note's data?

Also, please let me know if there are any issues you see with the code as it is now. Again, I'* certainly not a developer and know there's probably a much cleaner way to write this out.

I appreciate any and all help!

1 Answer

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
You can donate any amount for Orphans village using following QR Code.
...