You need to sign in to do that
Don't have an account?
Muhammad Jawwad 16
Please help
How to write a test class for helper class(Content Document Link?
Please find my trigger in code sample and suggest me how to write a test class for helper class.
Trigger : trigger ContentDocument on ContentDocument (after insert) { if(Trigger.isAfter && Trigger.isInsert) { //1. SELECT THE OPPORTUNITY TEAM SET<ID> SetOfContentDocumentIDs = new SET<ID>(); for(ContentDocument cdoc: Trigger.New) { SetOfContentDocumentIDs.add(cdoc.Id); } //2. Sharing Files With Opportunity Team ContentDocumentHelper.shareOpportunityPrivateFileWithOppTeam(SetOfContentDocumentIDs); } } Helper Class: public class ContentDocumentHelper{ @future public static void shareOpportunityPrivateFileWithOppTeam(SET<ID> SetOfContentDocumentIDs) { System.Debug('AM: SetOfContentDocumentIDs :' + SetOfContentDocumentIDs); List<ContentDocumentLink> ListOfContentDocumentLink = new List<ContentDocumentLink>(); integer i = 0; while(i <=80 && ListOfContentDocumentLink.size()<=1 ) { ListOfContentDocumentLink = [ SELECT LinkedEntityId,ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId IN : SetOfContentDocumentIDs]; i++; } //2.Get OpportunityTeamMember SET<ID> SetOfOpportunityIDs = new SET<ID>(); for(ContentDocumentLink cdl : ListOfContentDocumentLink ) { SetOfOpportunityIDs.add(cdl.LinkedEntityId); } System.Debug('AM: SetOfOpportunityIDs : ' + SetOfOpportunityIDs ); List<OpportunityTeamMember> ListofOpportunityTeamMember = [SELECT UserId,OpportunityId FROM OpportunityTeamMember WHERE OpportunityId IN : SetOfOpportunityIDs]; System.Debug('AM: ListofOpportunityTeamMember :' + ListofOpportunityTeamMember ); MAP<ID,SET<ID>> MapOfOppIDSetUsersID = new MAP<ID,SET<ID>>(); for(OpportunityTeamMember oppteam :ListofOpportunityTeamMember ) { if(MapOfOppIDSetUsersID.get(oppteam.OpportunityId) == null) { SET<ID> SetofUserID = new SET<ID>(); SetofUserID.add(oppteam.UserId); MapOfOppIDSetUsersID.put(oppteam.OpportunityId,SetofUserID); } else { SET<ID> SetofUserID = MapOfOppIDSetUsersID.get(oppteam.OpportunityId); SetofUserID.add(oppteam.UserId); MapOfOppIDSetUsersID.put(oppteam.OpportunityId,SetofUserID); } } System.Debug('AM: MapOfOppIDSetUsersID: ' + MapOfOppIDSetUsersID); //3.Process the documents and share with users List<ContentDocumentLink > ListofUserShare = new List<ContentDocumentLink >(); for(ContentDocumentLink cdl: ListOfContentDocumentLink) { SET<ID> SetofUserIDs = new SET<ID>(); SetofUserIDs = MapOfOppIDSetUsersID.get(cdl.LinkedEntityId); if(SetofUserIDs!=null) { if(SetofUserIDs.size()>0) { for(ID userID: SetofUserIDs) { if(!SetOfOpportunityIDs.contains(userID)) { ListofUserShare.add( new ContentDocumentLink ( ContentDocumentId = cdl.ContentDocumentId , LinkedEntityId = userID, ShareType = 'C' ) ); } } } } } System.Debug('AM: ListofUserShare : ' + ListofUserShare); if(ListofUserShare.size()>0) { insert ListofUserShare; } } }
Test Class:
@isTest public class ContentDocumentTest { @istest public static void test() { Opportunity o = New Opportunity (); o.StageName ='Open'; o.CloseDate=System.today() + 5; o.Name='Test NL Opportunity'; insert o; List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument]; ContentDocumentLink contentlink=new ContentDocumentLink(); contentlink.LinkedEntityId=o.Id; contentlink.ShareType= 'v'; contentlink.LinkedEntityId = o.id; contentlink.ContentDocumentId=documents[0].Id; contentlink.Visibility = 'AllUsers'; insert contentlink; } }
Please help
Please find the test class and let me know if you have any queries
Thanks,
SfdcCouple
All Answers
Please find the test class and let me know if you have any queries
Thanks,
SfdcCouple