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
Rai SahitaRai Sahita 

How to insert a file into a library folder named 'Test' when the file is attached to a record by trigger?

Hello

I'm trying to save attachment file into a library folder named 'Test' when file is attached to a custom records and updating field of record same time.

Here is my trigger below.

Please anyone help me to add some code for inserting file into library.

trigger UploadfileintoTest on ContentDocumentLink (after insert, after update) {
    
    List<ContentDocumentLink> cdls = ( Trigger.new == null ? Trigger.old : Trigger.new );
    
    Set<ID> parentIds = new Set<ID>(); 
    
    for (ContentDocumentLink cdl : cdls) {
        parentIds.add( cdl.LinkedEntityId );
    }
    
    for (List<Person__c> prn : [SELECT Id, (SELECT Id FROM ContentDocumentLinks LIMIT 1) 
                                       FROM Person__c WHERE Id IN :parentIds]) {
                                           for (Person__c pr : prn) {
                                               pr.Skill_sheet_last_update_date__c = System.today();
                                           }
                                           UPDATE prn;   
                                       }
    List<ContentWorkspace> cw = [SELECT Id, RootContentFolderId FROM ContentWorkspace WHERE Name = 'Test' LIMIT 1];

    
}

 

SwethaSwetha (Salesforce Developers) 
HI Rai,
Does the below code help
trigger UploadfileintoTest on ContentDocumentLink (after insert, after update) {
    List<ContentDocumentLink> cdls = ( Trigger.new == null ? Trigger.old : Trigger.new );
    Set<ID> parentIds = new Set<ID>(); 
    
    for (ContentDocumentLink cdl : cdls) {
        parentIds.add( cdl.LinkedEntityId );
    }
    
    List<ContentWorkspace> cws = [SELECT Id, RootContentFolderId FROM ContentWorkspace WHERE Name = 'Test' LIMIT 1];
    Id rootFolderId = cws.isEmpty() ? null : cws[0].RootContentFolderId;

    List<ContentWorkspaceDoc> workspaceDocs = new List<ContentWorkspaceDoc>();
    List<ContentDocumentLink> cdlToUpdate = new List<ContentDocumentLink>();
    
    for(ContentDocumentLink cdl : cdls) {
        // Only process CDLs where a new ContentDocument was created
        if(cdl.ContentDocument.LatestPublishedVersionId == cdl.ContentDocumentId) {
            ContentWorkspaceDoc cwd = new ContentWorkspaceDoc();
            cwd.ContentWorkspaceId = cws[0].Id;
            cwd.ContentDocumentId = cdl.ContentDocumentId;
            workspaceDocs.add(cwd);
            
            // Update the parent record's custom field
            if(cdl.LinkedEntityId.getSObjectType() == Person__c.sObjectType) {
                Person__c p = new Person__c(Id=cdl.LinkedEntityId, Skill_sheet_last_update_date__c=System.today());
                cdlToUpdate.add(new ContentDocumentLink(Id=cdl.Id));
            }
        }
    }
    
    // Insert the ContentWorkspaceDocs
    if(!workspaceDocs.isEmpty()) {
        insert workspaceDocs;
    }
    
    // Update the ContentDocumentLinks
    if(!cdlToUpdate.isEmpty()) {
        update cdlToUpdate;
    }
}

If this information helps, please mark the answer as best. Thank you
Rai SahitaRai Sahita

Hello, @Swetha.

Thank you for your kind response :)

I tried above code, but above code is not working. 

Please if anybody has an idea to insert related file to specific folder, please let us know how to do.

Thank you.

SwethaSwetha (Salesforce Developers) 
@Rai Sahita,
By not working, do you see any error?Thanks