i am working on a trigger associated with 2 custom objects. when the type__c field (picklist) on role history is updated to a value, the rep_type__c field should automatically take this same value. however, when there is an active role (no end date set) and the user attempts to add another role OF THE SAME TYPE an error should display saying to end date the active role first. this should NOT display if the second role being added is of a different type. below is what i have so far:
trigger RoleHistoryBeforeTrigger on Role_History__c (before insert, before update) {
Map<Id, String> repIds = new Map<Id, String>();
if(Trigger.isInsert) {
for(Role_History__c roles :Trigger.New) {
repIds.put(roles.Rep__c, roles.Type__c);
}
List<Role_History__c> activeRoles = [SELECT Id, Rep__c, Type__c FROM Role_History__c WHERE End_Date__c = NULL AND Rep__c IN :repIds.KeySet()];
Set<Id> setActiveRoleIds = new Set<Id>();
for(Role_History__c roles :activeRoles) {
setActiveRoleIds.add(roles.Rep__c);
}
for(Role_History__c roles :Trigger.New) {
if(setActiveRoleIds.contains(roles.Rep__c) && (roles.Type__c == repIds.get(roles.rep__c)))
roles.addError('Please set an End Date on current role before adding a new role.');
}
}
right now the errors fires regardless of if the type is the same or not. what am i missing to get this to function properly? i feel like it has to do with the final if statement but don't know what exactly.
thanks