Skip to main content
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);

}

}

}

}

 
3 respostas
  1. 22 de jun. de 2020, 13:04

    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.
0/9000