Trigger to check if the custom object has the correct file shared to community user
public without sharing class Domain_Documents extends fflib_SObjectDomain { public Domain_Documents(List<Documents__c> records) { super(records); } public class Constructor implements fflib_SObjectDomain.IConstructable{ public fflib_SObjectDomain construct(List<SObject> sObjectList){ return new Domain_Documents(sObjectList); } } public override void onAfterUpdate(Map<Id,SObject> existingRecords){ List<ContentDocumentLink> contentDocumentLinksList = new List<ContentDocumentLink>(); for (Documents__c documents : (List<Documents__c>) Records){ if (documents.File_Id__c!=null){ Documents__c existingDocument = (Documents__c)existingRecords.get(documents.Id); if (existingDocument.File_Id__c==null){ List<ContentDocumentLink> contentDocumentLinkExists = [select ContentDocumentId, Id, IsDeleted, LinkedEntityId, ShareType, SystemModstamp, Visibility from ContentDocumentLink where ContentDocumentId=:documents.File_Id__c]; Id userToShareWith = [Select Client__r.User__c from Documents__c where Id=:documents.Id].Client__r.User__c; Boolean flagToAddToDocs = true; for (ContentDocumentLink contentDocumentLinkExist : contentDocumentLinkExists){ if (contentDocumentLinkExist.LinkedEntityId==userToShareWith){ flagToAddToDocs=false; } } if (flagToAddToDocs){ ContentDocumentLink cdl = new ContentDocumentLink(LinkedEntityId = userToShareWith , ContentDocumentId=documents.File_Id__c, shareType = 'C'); contentDocumentLinksList.add(cdl); } } } } if (!contentDocumentLinksList.isEmpty()){ insert contentDocumentLinksList; } } }
Test class to check that sharing is done correctly
@isTest(SeeAllData=true) static void testUpdate() { User communityUser = Global_Test.setupCommunityUserAndLogin(); Documents__c bpDocument = (Documents__c)SmartFactory.createSObject('Documents__c'); bpDocument.OwnerId = communityUser.Id; bpDocument.File_Name__c='Test'; bpDocument.Document_Status__c = 'New'; bpDocument.Client__c = communityUser.ContactId; insert bpDocument; String yourFiles = 'Some file content here'; ContentVersion conVer = new ContentVersion(); conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files conVer.PathOnClient = 'test.txt'; // The files name, extension is very important here which will help the file in preview. conVer.Title = 'Test file '; // Display name of the files conVer.VersionData = EncodingUtil.base64Decode(yourFiles); // converting your binary string to Blog conVer.NetworkId=[Select Id from Network where Status='Live'][0].Id; //current Live community, should just be 1 but could have many insert conVer; Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId; //query for contentDocumentId ContentDocumentLink cDe = new ContentDocumentLink(); cDe.ContentDocumentId = conDoc; cDe.LinkedEntityId = bpDocument.Id; // you can use objectId,GroupId etc cDe.ShareType = 'C'; cDe.Visibility = 'AllUsers'; insert cDe; bpDocument.File_Id__c = conDoc; update bpDocument; Test.startTest(); System.runAs(communityUser){ bpDocument.File_Name__c='Update Name of File'; update bpDocument; } Test.stopTest(); Documents__c queryBpDocuments = [Select Id, File_Name__c, Document_Type__c from Documents__c where Id=:bpDocument.Id][0]; System.assertEquals(queryBpDocuments.File_Name__c, 'Update Name of File'); }