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);
}
}
}
}
Hi Sherwin in your test class try something like this:
If it helps please mark as correct, it may help others.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();