0

Requirement is to create a trigger that would check - if Status__c field of Variant__c record before update is "meta" then throw error if updation or deletion is performed on record. But the rule is "error message is thrown only When used on Trigger.new in before insert and before update triggers, and on Trigger.old in before delete triggers" hence in my case on update instead of error "lock record", exception is being thrown but as expected "lock record" error is being thrown on deletion of record.Also i cannot go for trigger.new as i need to check field value of old record. Is there any workaround to get error on updation or insertion in this scenario via trigger ?The current code goes like

public void beforeInsert(){
         lockRecord(Trigger.Old);
    }   
     public void beforeUpdate() {
         lockRecord(Trigger.Old);
    }
     public void beforeDelete() {
         lockRecord(Trigger.Old);
    } 
     public static  void lockRecord(List<Variant__c> oldVariants){
            for(Variant__c originalVariant: oldVariants)
                {
                    if(originalVariant.Status__c == 'meta')
                    {     
                      originalVariant.addError('Record lock');
                    }
                                  
                }   
        }
1

You can still check the old values, but you need to put the errors on Trigger.new for update, and Trigger.old for delete:

for(Integer i = 0, s = Trigger.old.size(); i < s; i++) {
  if(Trigger.old[i].Status__c == 'meta') {
    (Trigger.isDelete?Trigger.old:Trigger.new)[i].Status__c.addError('MSID Lock');
  }
}
| improve this answer | |
  • But in the beforeDelete context trigger.new will be null – Manish Anand Jul 9 at 19:48
  • 1
    @ManishAnand That was kind of implied, but I edited to make it explicit. You don't need two separate methods to accomplish this task. – sfdcfox Jul 9 at 19:50
1

You can use different variants to prevent deletion and updation

public static  void lockRecordBeforeUpdate(Map<Id,Variant__c> MapoldVariants, List<Variant__c> ListnewVariants){
        for(Variant__c NewVariant: ListnewVariants)
            {
                if(MapoldVariants.get(NewVariant.Id).status__c == 'Meta')
                {     
                  NewVariant.addError('MSID lock');
                }                
            }   
    }

public static  void lockRecordBeforeDelete(Map<Id,Variant__c> MapoldVariants){
            for(Variant__c Oldvariant: MapoldVariants.values())
                {
                    if(Oldvariant.status__c == 'Meta')
                    {     
                      Oldvariant.addError('MSID lock');
                    }                
                }   
        }

New records in case of beforeupdate will throw error in proper format.

However in beforedelete old record will prevent record deletion and will throw an exception. You can extract the relevant error messages and display it in the proper formatting.

Also,you can combine both the above methods.

You can call above method like:-

public void beforeUpdate() {
         lockRecordbeforeUpdate(Trigger.OldMap,Trigger.new);
    }

public void beforeDelete() {
         lockRecordbeforeDeleteTrigger.OldMap);
    }
| improve this answer | |

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.