Skip to content

Instantly share code, notes, and snippets.

@mhamzas
Last active May 17, 2019 09:57
Show Gist options
  • Save mhamzas/d88a6f45ee137bd80b3fc6ecf4ed4a40 to your computer and use it in GitHub Desktop.
Save mhamzas/d88a6f45ee137bd80b3fc6ecf4ed4a40 to your computer and use it in GitHub Desktop.
Sharing Knowledge Files with Community Users for Knowledge
@IsTest
public class shareKnowledgeFiles_Test {
static testmethod void init(){
Knowledge__kav article = new Knowledge__kav();
article.Title='Test';
article.UrlName='Test';
insert article;
ContentVersion contentVersion = new ContentVersion(
Title = 'Penguins',
PathOnClient = 'Penguins.jpg',
VersionData = Blob.valueOf('Test Content'),
IsMajorVersion = true
);
insert contentVersion;
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
test.startTest();
//create ContentDocumentLink record
ContentDocumentLink cdl = New ContentDocumentLink();
cdl.LinkedEntityId = article.id;
cdl.ContentDocumentId = documents[0].Id;
cdl.shareType = 'V';
insert cdl;
test.stopTest();
System.assertEquals(1, documents.size());
}
}
trigger shareKnowledgeFilesWithCommunityUsers on ContentDocumentLink (before insert) {
Schema.DescribeSObjectResult r = Knowledge__kav.sObjectType.getDescribe();
String keyPrefix = r.getKeyPrefix();
for(ContentDocumentLink cdl:trigger.new){
if((String.valueOf(cdl.LinkedEntityId)).startsWith(keyPrefix)){
cdl.ShareType = 'I';
cdl.Visibility = 'AllUsers';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment