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
Sherwin BetontaSherwin Betonta 

Create a test class to check record access of a specific profiles

I'm to new to apex. 
I have a class that would allow Users from a public group (marketing group) to access records created by another user say "System Admin".

I wanted to create a test class that would create a record using the sys admin profile and check if the marketing group can have edit access to the record.

Here is my code: 
public class OpportunitySharing {
    //sharing record after creating
   public void shareAfterInsert(List<Opportunity> oppoList){
       OpportunityShare oppShare = new OpportunityShare();

       List<Group> MarketingGroup = [SELECT Id, Name FROM Group WHERE Name = 'Marketing Group'];

       for(Opportunity opp: oppoList) {
           for(Group mg : MarketingGroup) {
               
               oppShare.OpportunityId = opp.Id;
               oppShare.UserOrGroupId = mg.Id;
               oppShare.OpportunityAccessLevel = 'Edit';
               Database.SaveResult dbsr = Database.insert(oppShare, false);
           }
       }
   }
}

 
Best Answer chosen by Sherwin Betonta
Agustin BAgustin B
Hi Sherwin in your test class try something like this:
Group test = new Group();
test.Name = 'Marketing Group';
insert test;


List<Opportunity> oppList = //Create the opportunity you want to test.
insert oppList;

Test.StartTest();
OpportunitySharing oppSharing = new OpportunitySharing();
oppSharing.shareAfterInsert(oppList);
Test.StopTest();

If it helps please mark as correct, it may help others.

All Answers

Agustin BAgustin B
Hi Sherwin in your test class try something like this:
Group test = new Group();
test.Name = 'Marketing Group';
insert test;


List<Opportunity> oppList = //Create the opportunity you want to test.
insert oppList;

Test.StartTest();
OpportunitySharing oppSharing = new OpportunitySharing();
oppSharing.shareAfterInsert(oppList);
Test.StopTest();

If it helps please mark as correct, it may help others.
This was selected as the best answer
Agustin BAgustin B
Hi Sherwin, if it solved your issue please mark as best so this question can be solved and help others asking the same.
 
Sherwin BetontaSherwin Betonta
Thank you.