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
Melissa BunchMelissa Bunch 

Share Files with Apex - Public Group

Hello!

I don't have any developer experience, so I'm struggling with a Trigger I'm trying to customize.

The use case is to share all Files added to Cases with a specific Public Group.
I found an Apex Trigger to share with all users, but I'm not sure how to edit it to only share with a specific set of users instead of everyone. 

Below is the code.
Does anyone have any insight?

Thank you!

trigger shareFilesWithCommunityUsers on ContentDocumentLink(before insert){

   Schema.DescribeSObjectResult r = Case.sObjectType.getDescribe();
    String keyPrefix = r.getKeyPrefix();

      for(ContentDocumentLink cdl:trigger.new){
        if((String.valueOf(cdl.LinkedEntityId)).startsWith(keyPrefix)){
          cdl.ShareType = 'I';
          cdl.Visibility = 'AllUsers';
          } 
       }
    }
Abdul KhatriAbdul Khatri
Hi Melissa,

I am not sure if you can share with Public Group, but I guess you can share with Chatter Group. I think the code would make more sense to be placed on the ContentVersion Trigger vs ContentDocumentLink Trigger as you need to insert a new record for that sharing. Here is the code for you but the group is Chatter Group which is what it supports.
 
trigger ContentVersionTrigger on ContentVersion (after insert) {

    Schema.DescribeSObjectResult r = Case.sObjectType.getDescribe();
    String keyPrefix = r.getKeyPrefix();    
    
    List<Id> idCDList = new List<Id>();
    for(ContentVersion cv : trigger.new){
        idCDList.add(cv.ContentDocumentId);
    }
    
    if(idCDList.isEmpty()) return;

    CollaborationGroup groupRec = [SELECT Id FROM CollaborationGroup WHERE Name = 'Test' LIMIT 1];
    List<ContentDocumentLink> cdlListToInsert = new List<ContentDocumentLink>();
    for(ContentDocumentLink cdl : [SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId = :idCDList])
    {
		if((String.valueOf(cdl.LinkedEntityId)).startsWith(keyPrefix)){

            ContentDocumentLink cdlToInsert = new ContentDocumentLink();
            cdlToInsert.ContentDocumentId = cdl.ContentDocumentId;
            cdlToInsert.ShareType = 'V';
            cdlToInsert.Visibility = 'AllUsers';
            cdlToInsert.LinkedEntityId = groupRec.Id;
            cdlListToInsert.add(cdlToInsert);
        }
    	    
    }
        
    Database.insert(cdlListToInsert);
    
}

 
Melissa BunchMelissa Bunch
Thank you, Abdul!

Unfortunately it throws an error when I try to share it with a community group (and I can't seem to add my site users to the internal group even when customers are allowed), but this did work great for internal chatter groups so I think this is still a great solution for anyone who may need that.

I have a case in with Salesforce to dive further into this, so I'll post an update if I find a solution. Otherwise, I'm not sure how possible this really is.

Thanks very much again!
Abdul KhatriAbdul Khatri
Hi Melissa,

Thanks will try to do further research.

Question, are you able to do that manually? Does Salesforce provide you that options through the UI?
Melissa BunchMelissa Bunch
So I thought you could share a file with site users, but I'm only see internal users or internal groups.
I may have been thinking of chatter posts - if you share a chatter post with a file attached to "all with access", that will share the file with site users.

But maybe a file uploaded directly internally can't be shared with a site or site user.
I'm down the rabbit hole of reading the document trail of who sees what and when :)
Abdul KhatriAbdul Khatri
Since you can share by user individually, the other way I can think of doing it programatically is this.
  • Create a public group of users you want to share the file with
  • We can use the same above code to read the users from that group and share to them by inserting record for that file inidividually.

Not sure I was able to explain it properly.
Abdul KhatriAbdul Khatri
Hi,

Let me know if you need help designing the solution.