Examples of event rules for system functions
Introduction
The document describes examples of event rule configuration for event processing automations for the system. Details of the general event configuration are described in the 'howto' documentation in the documentation portal.
Email notification and assigning a person to a service event
When a service event is changed and the person assigned to this event has changed, this new person receives an email with content informing him about his assignment to this event.
-
Title: Assigned service event
-
Description: Sends an email for a newly assigned person.
Definitions
-
Rule type: change (CHANGE)
-
Object: service event (mod.AssetApi.model.service.ServiceEvent)
-
Effector: email notification (Email Notification)
Email notification effect
Subject
: Service event number # {{obj.eventNumber}} has been assigned
Content
: You have been assigned a service event for the product: {{obj.product.name}}
List of email addresses: left blank because the email address is taken from the context.
That will substitute the event number and product name in the templates. Comparison of UUID assigned before and now. Test positive only if the UUID has changed and if the assigned one is non-empty. Comparison != supports both undefined and null.
Script expressions
check
The function returns true
if the UUID of the user assigned to the service event is different from the previous one and is not empty. This means that the assigned user has changed. Here we use access to the current version of the service event object obj
and to the previous version oldObj
.
(function(){
var assignedUuid=_.get(ctx,"obj.assigned.uuid");
var oldAssignedUuid=_.get(ctx,"oldObj.assigned.uuid");
return (assignedUuid!=null) && (assignedUuid != oldAssignedUuid);
})();
prepare
Getting the email assigned and checking that it is not empty and that the "@" character occurs at character 2 or further (indexOf returns -1 if the character does not occur or the character index). This is not a fully correct validation, but it is a basic check. If the mail is OK, it is added to the mails
array of the data
object (which will later be rebuilt into an effector data object), and that’s where the script will stop. If it is not, an exception will be thrown (a message formatted using the built-in template mechanism) that the user with the given login does not have an email set, so the message cannot be sent.
var assignedMail=_.get(ctx,"obj.assigned.employeeData.email");
if(
(assignedMail!=null) &&
(assignedMail.indexOf("@")>0)
) {
data.mails.push(assignedMail);
}
else {
logger.info("mail rejected: "+assignedMail);
throw templating.render("User {{obj.assigned.login}} has no email address set!",ctx);
}
Customer notification of a service event
-
Title: Notify the customer of a service event
-
Description: Send an email to the client when the event status changes to resolved
Definitions
-
Rule type: change (CHANGE)
-
Object: service event (mod.AssetApi.model.service.ServiceEvent)
-
Effector: email notification (Email Notification)
Email notification effect
Subject
: service event number #{{obj.eventNumber}} has been resolved
Message
: the service event for product {{obj.product.name}} has been resolved.
Script expressions
check
The check rule checks the public state of the event for the current and previous versions of the object. A positive check only occurs if the current state is RESOLVED (the != comparison supports both undefined and null objects.)
(function(){
var currentState =_.get(ctx,"obj.state.publicState");
var oldState = _.get(ctx,"oldObj.state.publicState");
return (currentState === "RESOLVED") && (currentState !== oldState);
})();
prepare
The preparation rule retrieves the client’s email and checks whether it is not empty and whether the '@' character is present on the second or subsequent character (indexOf returns -1 if the character does not exist or returns the index on which the character occurs). That is not a full check, but it is sufficient for basic verification. If the email is OK, it is added to the list of emails to be sent and then the script will exit. If not, an exception will be thrown with the internal formatting of the message due to no email address defined for the client, so the message cannot be sent.
var customerMail=_.get(ctx,"obj.eventManager.customer.email");
if(
(customerMail!=null) &&
(customerMail.indexOf("@")>0)
) {
data.mails.push(customerMail);
}
else {
logger.info("mail rejected: " + customerMail);
throw templating.render("Customer {{obj.eventManager.customer.name}} has no email address set!",ctx);
}
Aggregator not working
-
Title: Aggregator not working
-
Description: Checks the status of the connection to the aggregator
Definitions
-
Rule type: scheduler (SCHEDULER)
-
Object: mobile device (mod.AssetApi.model.mobile.Mobile)
-
Effector: email notification (Email Notification)
-
Use
You need to activate a task that periodically checks communication with the aggregators. The appropriate scheduler must be enabled. |
Email notification effect
Title
: Aggregator "{{obj.name}}" is not working
Message
: Aggregator "{{obj.name}}" is not responding. Last communication date: {{lastCommunicationDate}}
It is worth noting the lastCommunicationDate
value, which by default is not present in the context, but is instead inserted into it by the check rule.
Script expressions
prepare
blank
check
(function() {
if(ctx.detailedEventType !== "aggregator_failure") {
return false;
}
// Gets the date (as epoch ms) and converts to the string YYYY-MM-DD; HH:mm:ss
var lastComm = _.get(ctx, "obj.lastCommunicationDate");
var lastCommStr = "never";
if(lastComm) {
var date = new Date(lastComm);
lastCommStr = date.getFullYear(); //YYYY
lastCommStr += "-";
lastCommStr += ("00" + (date.getMonth()+1)).substr(-2,2); //MM
lastCommStr += "-";
lastCommStr += ("00" + date.getDate()).substr(-2,2); //DD
lastCommStr +="; "
lastCommStr += ("00" + date.getHours()).substr(-2,2); //HH
lastCommStr +=":"
lastCommStr += ("00" + date.getMinutes()).substr(-2,2); //mm
lastCommStr +=":"
lastCommStr += ("00" + date.getSeconds()).substr(-2,2); //ss
}
ctx.lastCommunicationDate = lastCommStr
return true
})()
Create a complaint
-
Title: Creation of a complaint
-
Description: Sends an email to the person who created the orders associated with this complaint
Definitions
-
Rule type: change (CHANGE)
-
Object: complaint (mod.AssetApi.model.warehouse.Complaint)
-
Effector: email notification (Email Notification)
Email notification effect
Subject
: complaint with {{obj.number}} has been created
Message
: Complaint with a number {{obj.number}} has been created
Script expressions
check
(function(){
var currentState =_.get(ctx,"obj.complaintState");
var oldState = _.get(ctx,"oldObj.complaintState");
return ((currentState === "CREATED") || currentState === "READY") && (currentState !== oldState);
})();
prepare
With this script, the claim number will be replaced in the templates. The check rule processes and compares the current and previous claim state. Only a positive test will be when the claim state has changed and the current state is CREATED. The preparer rule takes the email from the person who created the related order (by delivery) and checks that it is not empty and the "@" character appears and the second or next character (indexOf returns -1 if the character is not present in the string). This is not a fully valid check, just a basic one. If the email is OK, it is added to the list of email addresses in the data object (which will be embedded in the effector object).
Then the script will exit. If not, an exception will be thrown and message formatted according to the internal template mechanism with information which user login does not have a defined email address.
Available complaint states: CREATED, READY, SENT, IN_PROGRESS, VALID, INVALID, CLOSED;
var createdByMail =_.get(ctx, "obj.delivery.orderDocument.createdBy.employeeData.email");
if(
(createdByMail != null) &&
(createdByMail.indexOf("@")>0)
) {
data.mails.push(createdByMail);
}
else {
logger.info("mail rejected: " + createdByMail);
throw templating.render("User \"{{obj.delivery.orderDocument.createdBy.login}}\" does not have assigned email address!",ctx);
}
Delivery status change
-
Title: Checked Delivery
-
Description: Sends an email to the person who created the orders associated with this delivery
Definitions
-
Rule type: change (CHANGE)
-
Object: delivery (mod.AssetApi.model.warehouse.Delivery)
-
Effector: email notification (Email Notification)
Email notification effect
Subject
: delivery with the number {{obj.number}} has been checked on site
Message
: delivery with a number {{obj.number}} has been checked at the site
Script expressions
check
The check rule processes and compares the current and previous claim state. Only a positive test will be if the delivery status has changed and the current status is DELIVERED or REMARKS.
(function(){
var currentState =_.get(ctx,"obj.deliveryState");
var oldState = _.get(ctx,"oldObj.deliveryState");
return ((currentState === "DELIVERED") || (currentState === "REMARKS")) && (currentState !== oldState);
})();
prepare
The preparer rule takes the email from the person who created the associated order (by delivery) and checks that it is not empty and the "@" character appears and the second or next character (indexOf returns -1 if the character is not present in the string). That is not a fully valid check, just a basic one. If the email is OK, it is added to the list of email addresses in the data object (which will be embedded in the effector object). The script will then terminate. If not, an exception will be thrown and a message formatted according to the internal templating mechanism stating which user login does not have a defined email address.
Available delivery states: LOADING_ON_SITE, IN_TRANSIT, DELIVERED, REMARKS;
var createdByMail =_.get(ctx, "obj.orderDocument.createdBy.employeeData.email");
if(
(createdByMail!=null) &&
(createdByMail.indexOf("@")>0)
) {
data.mails.push(createdByMail);
}
else {
logger.info("mail rejected: "+createdByMail);
throw templating.render("User \"{{obj.orderDocument.createdBy.login}}\" does not have assigned email address!",ctx);
}
Order status change
-
Title: Order accepted
-
Description: Sends an email to the person who created the order if the order is accepted by the responsible person
Definitions
-
Rule type: change (CHANGE)
-
Object: order (mod.AssetApi.model.warehouse.Order)
-
Effector: email notification (Email Notification)
Email notification effect
Subject
: Order with number {{obj.number}} has been created
Message
: Order with a number {{obj.number}} has been created
Script expressions
check
The check rule processes and compares the current and previous claim state. Only a positive test will be when the order status has changed and the current status is ACCEPTED.
(function(){
var currentState =_.get(ctx,"obj.orderState");
var oldState = _.get(ctx,"oldObj.orderState");
return (currentState === "ACCEPTED") && (currentState !== oldState);
})();
prepare
The preparer rule takes the email from the person who created the associated order (by delivery) and checks that it is not empty and the "@" character appears and the second or next character (indexOf returns -1 if the character is not present in the string). That is not a fully valid check, just a basic one. If the email is OK, it is added to the list of email addresses in the data object (which will be embedded in the effector object). The script will then terminate. If not, an exception will be thrown and a message formatted according to the internal templating mechanism stating which user login does not have a defined email address.
Available order states: CREATED, READY, ACCEPTED, ORDERED, CONFIRMED, IN_TRANSIT, DELIVERED, CLOSED, REJECTED;
var createdByMail =_.get(ctx, "obj.createdBy.employeeData.email");
if(
(createdByMail!=null) &&
(createdByMail.indexOf("@")>0)
) {
data.mails.push(createdByMail);
}
else {
logger.info("mail rejected: "+createdByMail);
throw templating.render("User \"{{obj.createdBy.login}}\" does not have assigned email address!",ctx);
}
Notification that a task/work order has been created and assigned to a person
Definitions
-
Rule type: create (CREATE)
-
Object: work order (mod.AssetApi.model.workorder.WorkOrder)
-
Effector: email notification (Email Notification)
Email notification effect
Subject
: A new {{obj.name}} task has been created.
Script expressions
prepare
In this case, we send an email to the person who is assigned to the event. We do not limit sending to specific tasks - all tasks are sent.
var assignedMail=_.get(ctx,"obj.assignedUser.employeeData.email");
if(
(assignedMail!=null) &&
(assignedMail.indexOf("@")>0)
) {
data.mails.push(assignedMail);
}
else {
logger.info("mail rejected: "+assignedMail);
throw templating.render("Użytkownik \"{{obj.assignedUser.login}}\" nie ma ustawionego adresu e-mail!",ctx);
}
check
(function(){
return true;
})();
Notification of assignment to a task/work order
In this case, we send the data to the assigned person. We do this when the record is updated. We do this only when the newly assigned person is different from the person who was previously assigned (before saving changes to the record). Persons are identifier by UUID.
Definitions
-
Rule type: update (UPDATE)
-
Object: work order (mod.AssetApi.model.workorder.WorkOrder)
-
Effector: email notification (Email Notification)
Email notification effect
Title
: You have been assigned to the task {{obj.name}}
Script expressions
check
(function(){
var assignedUuid=_.get(ctx,"obj.assignedUser.uuid");
var oldAssignedUuid=_.get(ctx,"oldObj.assignedUser.uuid");
return (assignedUuid!=null) && (assignedUuid !== oldAssignedUuid);
})();
prepare
var assignedMail=_.get(ctx,"obj.assignedUser.employeeData.email");
if(
(assignedMail!=null) &&
(assignedMail.indexOf("@")>0)
) {
data.mails.push(assignedMail);
}
else {
logger.info("mail rejected: "+assignedMail);
throw templating.render("Użytkownik \"{{obj.assignedUser.login}}\" nie ma ustawionego adresu e-mail!",ctx);
}
Notification that an ISO registry has been created and assigned to a person
New corrective action → to assigned (if different from submitter). The system will check new records - in this case, corrective actions and send an email to the person who is assigned. The system will NOT send an email if the reporting person is the same as the assigned person. See the check script to verify this person.
Definitions
-
Operation: CREATE
-
Object type: mod.AssetApi.model.special.energyenvironment.CorrectionAction
-
Effector: email notification (Email Notification)
Email notification effect
Title
: New Corrective Action with protocol number {{obj.protocolNumber}}
Email content
: Protocol number {{obj.protocolNumber}}. Submitting login: {{obj.entryUser.login}}
Script expressions
check
(function(){
var responsibleUserUuid=_.get(ctx,"obj.responsibleUser.uuid");
var entryUserUuid=_.get(ctx,"obj.entryUser.uuid");
return (responsibleUserUuid != null) && (entryUserUuid != null) && (responsibleUserUuid !== entryUserUuid);
})();
prepare
var assignedMail=_.get(ctx,"obj.responsibleUser.employeeData.email");
if(
(assignedMail!=null) &&
(assignedMail.indexOf("@")>0)
) {
data.mails.push(assignedMail);
}
else {
logger.info("mail rejected: "+assignedMail);
throw templating.render("Użytkownik \"{{obj.responsibleUser.login}}\" nie ma ustawionego adresu e-mail!",ctx);
}
Notification of ISO register reassignment
Corrective Action Update → send email to assignee if they have changed. The machine will send email if the record is updated AND if the assignee has changed.
Definitions
-
Operation: UPDATE (Update)
-
Object type: mod.AssetApi.model.special.energyenvironment.CorrectionAction
-
Effector: email notification (Email Notification)
Email notification effect
Email subject
: You have been assigned a corrective action with protocol number {{obj.protocolNumber}}
Email
: You have been assigned a corrective action. Protocol number {{obj.protocolNumber}}. Submitting login: {{obj.entryUser.login}}
check
(function(){
var responsibleNewUserUuid=_.get(ctx,"obj.responsibleUser.uuid");
var responsibleOldUserUuid=_.get(ctx,"oldObj.responsibleUser.uuid");
return (responsibleNewUserUuid != null) && (responsibleOldUserUuid != null) && (responsibleNewUserUuid !== responsibleOldUserUuid);
})();
prepare
var assignedMail=_.get(ctx,"obj.responsibleUser.employeeData.email");
if(
(assignedMail!=null) &&
(assignedMail.indexOf("@")>0)
) {
data.mails.push(assignedMail);
}
else {
logger.info("mail rejected: "+assignedMail);
throw templating.render("Użytkownik \"{{obj.responsibleUser.login}}\" nie ma ustawionego adresu e-mail!",ctx);
}
Exceeding the MIN/MAX value for the parameter
Defined for the selected parameter setting in the system, i.e. we define a rule for the "Temperature" parameter in the "Temperature sensor no. 1" resource. Using the rule, we can generate a notification when the parameter value changes and exceeds the MIN/MAX value.
Definitions
-
Operation: UPDATE (Update)
-
Object Type: Parameter Settings
-
Effector: email notification (Email Notification)
Email notification effect
Subject email
: Temperature parameter value exceeded maximum value. Parameter: {{obj.parameter.name}}
Email message
: Temperature parameter value exceeded maximum value. Parameter: {{obj.parameter.name}}. Current value {{obj.value}}.
check
(function(){
var value =_.get(ctx,"obj.value");
val floatValue = parseFloat(value.replace(",", "."));
return floatValue > 10.50;
})();
prepare
<empty>
This Howto is based on system version 1.22.0.0 (07.2023) and presents features that may not be available on your system. Ask AMAGE to provide this functionality. |
Due to ongoing development of the system, some screens or configuration files may look slightly different, but will still retain the full functionality described here. This does not affect the core functionality described in this document. |