1

In a Rule, how can I prevent executing subsequent actions if the first action fails?

In my case, I want the order to be marked as Processing only if the order was successfully transmitted to the fulfilment center. I've tried returning FALSE from the action but this has no effect.

Example rule

2
  • Just a note, but triggering order fulfilment on order view seems like a bad idea. What if the order is viewed again by the user or an admin? It looks as though it would be sent for fulfilment again. Better to use something like order paid in full invoked. Jan 3, 2013 at 9:00
  • @DavidThomas : I think you have a valid point, however using any other Rules Event does not really change the challenge in this interesting question here. Feb 15, 2017 at 10:29

2 Answers 2

2

To provide conditional logic, you need the Rules Conditional module.

There's an open issue to document this further, and there's another meta issue which looks at integrating the conditional logic into the Rules module itself.

This module provides Rules plugins for conditionally altering execution flow directly inside a group of actions. Provided plugins include:

Conditional
    If/(Else) If
    Else
Switch
    Case
    Default case
While
2
  • Rules Conditional is still very ustable (i.e. can't save data in a conditional section). What would be an alternative?
    – Yuri
    May 6, 2016 at 23:38
  • @Yuri : the answer to your question is using Rules Components (check my answer for details). Feb 15, 2017 at 10:31
0

The answer to your question, may depend on what exactly you mean by the "if first action fails".

Have a look at the issue "After updating existing content" runs before content is saved, about an event which is a variation of your event here, and somehow explains that "after saving something" actually happens ... "before saving something" (which IMO is not a bug, simply how the Rules module works ...). That's probably not the reason why in your case the first Rules Action failed, but an even tougher scenario to get to work (in which even Conditional Rules cannot help you).

A possible compromise to avoid these kind of issues, is to transform what you're trying to do in your Subsequent Rules Action (for which you want to prevent its execution) in a Rules Component, so that after your First Rules Action you "schedule the execution" (using the Rules Scheduler submodule) of that Rules Component. For example after only a few seconds or minutes later on (if your cron job runs frequent enough, otherwise it'll be next time cron runs).

This approach will allow that, at the time the Rules Component is executed, you can try to add any relevant Rules Conditions in that Rules Component also. I.e to validate if the result of your First Rules Action is what you wanted/expected it to be. In certain scenarios (where you know something went wrong), you might even implement additional Rules logic to just delete the scheduled Rules Component so that it even doesn't actually get scheduled.

BTW, there are also scenarios where it is sufficient to just execute the Rules Component (containing the equivalent of your "subsequent Rules Action"), instead of using the Rules Scheduler to schedule its execution. In this case you can add similar Rules Conditions in that Rules Component also to validate the results of your First Rules Action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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