You need to sign in to do that
Don't have an account?
Ryan 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');
}
}
}
}
}
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');
}
}
}
}
}
Can u please Insert oppylist and try once it will cover and what is the code coverage now.