You need to sign in to do that
Don't have an account?

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';
}
}
}
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';
}
}
}
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.
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!
Thanks will try to do further research.
Question, are you able to do that manually? Does Salesforce provide you that options through the UI?
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 :)
Not sure I was able to explain it properly.
Let me know if you need help designing the solution.