0

I am using the SFDC trigger framework to force a particular field of the Order Object to be always the same. for example, if the the user enters on the field Status 'Active' I want to automatically change it to 'draft'. My code as follows:

Trigger handler:

public class OrderTriggerHandler extends TriggerHandler {
    public override void beforeUpdate() {
        for (Order o : (List<Order>) Trigger.new) {
            o.Status = 'dralf';
        }
    }

}

Trigger:

trigger OrderTrigger on Order (before insert) {
    new OrderTriggerHandler().run();
}

What am I doing wrong?

1
  • This, to me, seems like it would be more appropriate as a validation rule.
    – Derek F
    Jul 10 '18 at 12:45
3

You are not passing before update in trigger OrderTrigger on Order (before insert).

Also technically you should compare with old list as well that if old record contains status=='active' then only you change its value to 'draft'. In this it will always change it to draft.

If you want to run similar logic on insert then you need to create a separate method for beforeCreate as well according to this pattern.

3
  • if I want that Status is always 'draft' then like that is ok. Right? Jul 10 '18 at 12:05
  • Move this logic to a method and then call it from the other events as well. Jul 10 '18 at 12:07
  • @Cubancoffee No, this code would only run on insert and not on update, and your case is to run it on update. so you need to pass before update .
    – Mr.Frodo
    Jul 10 '18 at 12:07

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.