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
Ryan Mason 15Ryan Mason 15 

Test Class Coverage for Oppy Team Member Access

I wrote a trigger, trigger handler, and a class to check permissions of a user before they are able to create,edit, or delete an oppy team member. I am new to Apex and am struggling with the test code coverage. Here is my code(ignore the ugly error messages those are going to change). Any help would be much appreciated.

public class OppyTeamMemberCheckPerms {
    
    Public void CheckPerms(List<OpportunityTeamMember> newRecords, List<OpportunityTeamMember> oldRecords, System.TriggerOperation triggerEvent){
        Id OppyRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Rocket Pro Loan').getRecordTypeId();
        Boolean hasCustomPerm = FeatureManagement.checkPermission('Opportunity_Team_Member_Access');
        string triggerType = string.valueOf(triggerEvent);
        List<Id> OppyList = new List<Id>();
        List<Id> InvalidOppyList = new List<Id>();    
        
        if (triggerType == 'BEFORE_DELETE'){
            for (OpportunityTeamMember tm : oldRecords){
                OppyList.add(tm.OpportunityId);
            }
            for(Opportunity o : [SELECT recordtypeid FROM Opportunity WHERE id IN:OppyList]){
                if(o.RecordTypeId == OppyRecordTypeId){
                    InvalidOppyList.add(o.Id);
                }
            }
            for (OpportunityTeamMember tm : oldRecords){
                if(InvalidOppyList.contains(tm.OpportunityId) && hasCustomPerm == false){
                    tm.adderror('Cant delete team member2');
                }
            }
        }
        if (triggerType == 'BEFORE_UPDATE'){
            for (OpportunityTeamMember tm : newRecords){
                OppyList.add(tm.OpportunityId);
            }
            for(Opportunity o : [SELECT recordtypeid FROM Opportunity WHERE id IN:OppyList]){
                if(o.RecordTypeId == OppyRecordTypeId){
                    InvalidOppyList.add(o.Id);
                }
            }
            for (OpportunityTeamMember tm : newRecords){
                if(InvalidOppyList.contains(tm.OpportunityId) && hasCustomPerm == false){
                    tm.adderror('Cant update team member2');
                }
            }
        }
        if (triggerType == 'BEFORE_INSERT'){
            for (OpportunityTeamMember tm : newRecords){
                OppyList.add(tm.OpportunityId);
            }
            for(Opportunity o : [SELECT recordtypeid FROM Opportunity WHERE id IN:OppyList]){
                if(o.RecordTypeId == OppyRecordTypeId){
                    InvalidOppyList.add(o.Id);
                }
            }
            for (OpportunityTeamMember tm : newRecords){
                if(InvalidOppyList.contains(tm.OpportunityId) && hasCustomPerm == false){
                    tm.adderror('Cant insert team member2');
                }
            }
        }
    }
}
aparna kantuaparna kantu
Hi Ryan Mason,
Can u please Insert oppylist and try once it will cover and what is the code coverage now.