• Ryan Mason 15
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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');
                }
            }
        }
    }
}