Scheduling Jobs From Rules
Back to IndexRules can be combined with Jobs to create advanced automated processes within Hantera based on various events. For example, you might want to send an email to the customer when the Order is confirmed. With a Rule that triggers on an Order state change, this becomes trivial.
In the following example, we assume that we already have a Reactor that uses Mailtrap to send e-mails called mailtrap
with method sendEmailFromTemplate
. You can find this exact example in the Cookbook
The Rule effect used to schedule jobs is scheduleJob
. Not all Rule Hooks support the scheduleJob
effect. Refer to the Rule Runtime Hooks Reference for more details.
param input: OnOrderCommandsparam templateUuid: textparam reactorId = 'mailtrap' // Default value 'mailtrap' but allow override
let sendOrderConfirmation = { effect = 'scheduleJob' reactorId = reactorId method = 'sendEmailTemplate' argument = { to = { email = input.order.invoiceAddress.email name = input.order.invoiceAddress.name } templateUuid = templateUuid templateVariables = { customer = { name = input.order.invoiceAddress.name address = { firstName = input.order.invoiceAddress.name street = input.order.invoiceAddress.addressLine1 city = input.order.invoiceAddress.city state = input.order.invoiceAddress.state zip = input.order.invoiceAddress.postalCode country = input.order.invoiceAddress.countryCode } } order = { number = input.order.orderNumber items = input.order.deliveries select d => d.orderLines flatten select l => { name = l.description quantity = l.quantity price = l.unitPrice } isRush = false total = input.order.orderTotal } } }}
from input match { before: { orderState: 'pending' } order: { orderState: 'confirmed' } } |> sendOrderConfirmation