it correctly stops a new role from being added when the previous one doesn't have an end date set. however, it continues to throw the error even if the new role doesn't have an end date set. what am i missing?


  • 1
    your trigger is not stopping new role from being added when the previous one doesn't have end date set. It's preventing you the new record from being inserted because it doesn't have end date populated. You will need to query the existing records first, and compare to the "new" one being inserted. – Rodrigo Sep 19 '19 at 16:39

@Rodrigo is correct.

After you clarified your Question in the comments below. I will make some assumptions that you should have made in your question.

1.There is a field on the Role_History__c object called RepresentativeId__c which is a lookup field to the representative.
2. When a new Role_History__c records is inserted, the RepresentativeId__c field must be filled
3. If a Role_History__c record is inserted for a Representative and there is already an 'Active' Role_History__c record for that representative (i.e, End_Date__c = null) then the record being inserted should display an error.

What I would do is something like the following:

trigger RoleHistoryEndDateBeforeTrigger on Role_History__c (before insert) {
  List<Role_History__c> liActiveRoleHistory = [SELECT Id, RepresentativeId__c FROM Role_History__c WHERE End_Date__c = NULL];

  Set<Id> setOwnersWithActiveRH = new Set<Id>(); //if an owner has an active RH, put their Id in this Set.
  for(Role_History__c rh : liActiveRoleHistory){

  for (Role_History__c rh : Trigger.New){
     if(Trigger.isInsert && setOwnersWithActiveRH.contains(rh.RepresentativeID__c){   //if the Representative of the role history being inserted already has an active role history
        rh.addError('Must set an End Date on current role before adding new role'); //block insert

Please note: Nothing in your question explained how you want to handle an update of an existing record.

The above solution should handle the insertion of a new Role_History__c record.

Also, I would take a look at Trigger Best Practices before deploying this. If you need additional logic to handle the update of Role_History__c records I would write a seperate class ( ex: RoleHistoryValidation) and do something like the following:

trigger RoleHistoryEndDateBeforeTrigger on Role_History__c (before insert, before update) {
      RoleHistoryValidation(validateBeforeUpdate(Trigger.New, Trigger.OldMap));

I Hope this answers your question and helps you out.

  • i tried this and still get an error when trying to insert a new record that doesn't have an end date. i didn't mean to have it as a before update, as no errors should throw when the old record is edited to have an end date. it should just be on insertions. also, there will only ever have 1 (max) active record, so in the query should i have it be new Map<Id, Role_History__c>([SELECT Id FROM Role_History__c WHERE End_Date__c = null ORDER BY CreatedDate DESC LIMIT 1]); ? – testing Sep 19 '19 at 18:08
  • What is the error message you are getting? it could be coming from another source (ex: validation rule) Did you ensure that there are no other 'Active Role History' records? Go to the Dev Console Query Editor and run the query: Select Id FROM Role_History__c WHERE End_Date__c = null Do any row's pop up? – Z33dawg Sep 19 '19 at 18:21
  • its the error that's defined after rh.addError but eureka! after running that query i found 2 rows. i did some research and found there are 2 instances in my org where there's an active role record but they are associated with a different rep (which is why i never saw them before). so i would need this trigger to only analyze active roles on the associated rep (which is the master (custom) object to this child object), sorry i had no idea to even consider roles not associated with the rep... Rep__c is a master detail field on this role history object – testing Sep 19 '19 at 18:31
  • I believe you could get the current userId by -->Id currentUserId = UserInfo.getUserId(); Do that and adjust your query to [....AND OwnerId =: currentUserId]; Make sure you write unit tests to test thouroughly! – Z33dawg Sep 19 '19 at 18:36
  • You really need to update your Question to be more specific to the problem you are facing because these details you failed to mention require a different solution. Considering there is a lookup field on a role history to the Representative ( i called this RepresentativeId__c above). What I did was create a Set<Id> that contains the Ids of Representatives that have an active role history. For each record being inserted, if the Representative of that record is in the set then addError() otherwise he is not in the set and has no active RH records, so do nothing. – Z33dawg Sep 19 '19 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.