Inbound email actions are most commonly used to process emails. For example, if an incoming email contains “Incident” in the subject line, the system creates an incident record.

Inbound email actions are similar to business rules in the usage of scripts and conditions that perform actions on the target table. Inbound email actions verify emails for defined conditions and marks that associate an email with a task. If the conditions are met, the inbound email action performs the pre-configured activities. There are two types of them:

  • Record Action: runs a specified script.
  • Reply Email: when triggered, sends a reply email.

Unlike Notification Rules, inbound email actions enable the user to interact with the records in the system.

For example, while processing an incident in the Information Needed state, the system inserts the body of this email to the Additional Comment field in the Activity Feed when the caller sends their response. To do so, an inbound email action is created. The condition for this action is the presence of a certain text in the subject and the same task number as in the incident record.

To process inbound email, set up the default email account. See the Email Accounts article to learn how to do it.

Inbound action chain


Inbound actions include three major groups of parameters:

  • An action to execute when an email is received. It is defined with a script.
  • A condition that an email should meet for the action to be executed.
  • Parameters that control the processing flow.

The third group of parameters is required to organize multiple inbound actions in a way that they check an incoming email to match the condition defined in each of them, in a certain order. The less is the value in the Order field of an inbound action, the earlier this inbound action checks if a received email matches its execution condition. As a result, each email goes through an ordered chain of condition checks, from the inbound action with the least Order value to the one with the biggest, with the following processing logic: 

  1. If the Active checkbox is disabled (its value is set to false), this inbound action is skipped. The inbound actions with the Active checkbox enabled (its value is set to true) are processed further.
  2. If an email matches no condition of any inbound action, it passes all the way down the processing chain, and no action will be executed.
  3. If the condition of an inbound action is matched, the execution of the action script of that inbound action starts. The further processing flow depends on the value of the Stop processing when complete checkbox of this inbound action:
    • If the checkbox is not selected, the email is checked to match the condition of the next Inbound Action down the chain, and the logic proceeds from Step 2 again.
    • If the checkbox is selected, the rest of the inbound actions down the chain are skipped, and the processing chain is terminated. 

As a result, one inbound email can trigger execution of none, one or several action scripts in a row, depending on the values of the Active, Order and Stop processing when complete fields in each inbound action defined in the system.


Configure an inbound email action


  1. Navigate to System Notification → Inbound Email Actions.
  2. Click New and fill in the fields.
  3. Click Save or Save and exit to apply the changes.

Role required: admin.


FieldMandatoryDescription
NameNSpecify an action name.
Action typeN

Select the action type. Available options:

  • Record action – when triggered, the Script runs.
  • Reply email – when triggered, an email is sent to the address that initiated the action. Its content is determined by the Reply email field.

The Reply email inbound action uses a default email account for sending emails, even when more than one email account is available.


Message typeN

Specify a message type required to run the action. Available choice options:

  • New – newly created emails, neither reply nor forward.
  • Reply – emails with a subject that begins one of the prefix defined in the simple.email.reply_subject_prefix property.

  • Forward – forward emails with a subject that begins with one of the prefixes defined in the simple.email.forward_subject_prefix property.

  • All – emails of all types (new, reply and forward emails).
DSNN

Select this checkbox to trigger the action in response to Delivery Status Notification emails.

InvitationN

Select this checkbox to trigger the action in response to Invitation emails.

DescriptionNAdd a detailed description of what this email action does.
Reply emailN

Enter the text of the reply letter. It will be sent to the original email address that initiated the action.

FromY

Refer to a user, who initiates a script execution, or sends a response email.

ScriptN

Enter a script that triggers the inbound email action. You can use all methods of server-side API classes here.

In the inbound action script, the following objects are available:

  • email is an instance of the SimpleRecord class and refers to a record from the Email (sys_email) table that the action will process. To get the values of the record fields, use the dot-notation for the email object. To do so, use properties with names corresponding to the names of record fields.
  • event is an instance of an entry in the Inbound Email Actions (sys_email_inbound_action) table. 
(function runAction(/*SimpleRecord*/ event, /*SimpleRecord*/ email) {
const newTask = new SimpleRecord('task');
newTask.subject = email.subject;
newTask.description = email.body_text;
if (email.blind_carbon_copy) {
    newTask.attention_required = true;
}
cosnt insertedTaskID = newTask.insert();



ActiveN

Select this checkbox to activate the action.

OrderNEnter a number to define the order of action processing. Actions are processed in ascending order.

Stop processing when complete

N

Select the checkbox to end the message processing after the current action is completed. All subsequent actions (with a higher Order) are ignored.

Use case


You need to configure an inbound action implementing the following logic:

  • When the system receives an email with the subject containing the keyword “access”, a new incident is created and assigned to the group responsible for security and access to the system and data.

Create an inbound action as shown below:

FieldValue
NameCreate incident (access issue)
Action typeRecord Action
Message typeNew
Activetrue
Script


(function runAction( /*SimpleRecord*/ event, /*SimpleRecord*/ email) {

    ss.importIncludeScript('EmailHelper');
    const helper = new EmailHelper();

    const letter = email.subject.match('aсcess'); // Keywords checking
    if (letter) {
        const incident = new SimpleRecord('itsm_incident');
        incident.subject = email.subject;
        /* If the sender's address is registered in the system,
        the Caller field will contain reference to an existing User record.
        If the address is unknown, the caller will be defined as Guest */
        incident.caller = helper.userIDbyEmail(helper.parseSender(email.from));

        incident.description = email.body_text;
        incident.state = '-2'; // Registered
        incident.contact_type = '20'; // Email
        incident.assignment_group = '162920380313692887'; // Security Group
        incident.impact = '2'; // Moderate
        incident.urgency = '2'; // Moderate
        const insertedIncidentId = incident.insert();

        if (insertedIncidentId != 0) {
            ss.debug(`Email ${email.sys_id} processing result: Incident with ID ${insertedIncidentId}`);

            const attach = new SimpleAttachment();
            attach.copy('sys_email', email.sys_id, 'itsm_incident', insertedIncidentId);
            return;
        }
        ss.error(`Inbound Action Failed!\nErrors:` + JSON.stringify(incident.getErrors(), null, 2));
    }
})(event, email);


Inbound action logging


Every inbound action triggering is logged in the Script Log. You can filter these logs using the criteria below:

  • Source IS Inbound Action
  • Essence Document CONTAINS 0229fa8a-bcbe-1f11.

The Essence Document field is responsible for email processed by inbound action. Enter the full address or a part of it, and use precision or imprecision condition operators.


Recommendations


The incoming mail parsing returns the address value for the From field of a record from the Email (sys_email) table in lower case. 

When searching for a user by email in the Script column, do not use the IS operator because the value of email can be entered with uppercase letters. For example, J.Doe@mail.com.

user.get('email', 'j.doe@mail.com');

Also, do not use the LIKE operator when searching for a user by email because the search will find a user with a similar email, for example, 'raj.doe@mail.com'.

user.get('email', 'like', 'j.doe@mail.com')

The following example shows the script with the userIDbyEmail() method that searches for a user by email value:

class MyEmailHelper extends EmailHelper {
    userIDbyEmail(email) {
        const user = new SimpleRecord('user');
        user.addQuery('email', 'startswith', email);
        user.addQuery('email', 'endswith', email);
        user.selectAttributes('sys_id');
        user.setLimit(1);
        user.query();
        if (user.next()) {
            return user.sys_id;
        }
        return this.searchGuest();
    }
}