Enabling Failure Notfications in Custom Action
What are Failure Notifications?
From Jadu Central 1.2.0, it is possible to receive an email notification if an action fails to complete successfully.
In the Control Center, you can subscribe to receive failure notifications for one or more actions on a given form. You can choose to receive:
- Immediate notification - if a form submission fail on one or more actions you've subscribed to, you'll immediately receive a notification for this submission.
- Daily Digest - sent overnight, detailing all submissions from the previous day that failed to run action(s) you are subscribed to.
If you have developed custom form actions, you can update them to take advantage of failure notifications in a few simple steps.
Supporting Failure Notifications in a Custom Action
In order for a custom action to support failure notifications, make the following changes to your action type class:
-
Include
RuleActionHelper
as a trait at the top of the class.use RuleActionTypeHelper;
-
In the
execute
method, update any areas where an error is logged i.e.$log->setError
is called, adding the following line:$this->addActionFailureEntry($log, $this->action->getId());
-
Optionally, remove calls to
$log->setResponseTime()
- Jadu Central handles this automatically from Jadu Central 1.2.0 if you save the log via$this->writeLog()
.
Example: Enabling Action Failures for a custom action
We may want to enable failure notifications for our ReceiptingSystemActionType, to offer notifications when the API returns an error.
As a first step, we'll include the trait in the class like below:
namespace Jadu\Custom\CustomXFormsProBundle\Rules\Actions;
use Jadu\XFormsPro\Form\Rules\Actions\SimpleMappedActionType;
class ReceiptingSystemActionType extends SimpleMappedActionType
{
use RuleActionTypeHelper;
...
Next, we add the call to $this->addActionFailureEntry
where we want to report a failure in the execute
method. We can implement execute method in the following way:
/**
* Takes an array of mappings and carries out the integration.
*
* @param array $mappings
* @param UserForm $userForm
*
* @return string the result
*/
public function execute($mappings, $userForm)
{
$log = $this->getNewLog($userForm->getId());
$log->setRequest(
json_encode($mappings)
);
$log->setRequestTime(new \DateTime());
$result = $this->emailService->submitData(
$mappings[To],
$mappings['From'],
$mappings['Subject']
);
$log->setResult($result);
if ($result != 'ERR') {
$log->setResponse('Successfully sent email');
} else {
$log->setError('Unable to send an email');
$log->setResponse('Error');
$this->addActionFailureEntry($log, $this->action->getId());
}
$this->writeLog($log);
return $result;
}
Note the omission of $log->setResponseTime(new \DateTime());
which was present in the earlier example.