Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto-create Customer Contact from Inbound Email – Inbound Action Not Triggering

swethavelu
Tera Contributor

Hi Community,


I’m trying to auto-create a Customer Contact record when an inbound email is received. Here’s what I did:

  • Created an Inbound Email Action in Global scope.
  • Action Type: Record Action, Target Table: customer_contact.
  • Execution Order: 10, Weight: 100.
  • No conditions (to allow all emails).
  • Script checks if contact exists, then creates a new record with email, name, company (from subject), and phone (from body).
  • Tested the script in Script Background – it works and creates the contact.
  • Email account is configured and emails appear under System Logs → Emails → Received.
  • But the inbound action never fires. I noticed the received emails have Type = received, while my inbound action was set to Type = None.

Question:

  • Is the Type field causing this issue?
  • Are there any other settings required for inbound actions to trigger?
  • If inbound actions are unreliable, what’s the best alternative to achieve auto-contact creation?

Fyi, below is the script action.

(function runAction(current, event, email, logger, classifier) {

    gs.log('Inbound Email Action triggered for: ' + email.from);

    // Check if contact already exists
    var existing = new GlideRecord('customer_contact');
    existing.addQuery('email', email.from);
    existing.query();

    if (!existing.next()) {
        var contact = new GlideRecord('customer_contact');
        contact.initialize();

        // Extract name from display name or email prefix
        var nameParts = [];
        if (email.origFromName) {
            nameParts = email.origFromName.split(' ');
        } else {
            var emailPrefix = email.from.split('@')[0];
            nameParts = [emailPrefix];
        }

        contact.first_name = nameParts[0];
        contact.last_name = nameParts.length > 1 ? nameParts[1] : '';
        contact.email = email.from;

        // Parse company from subject
        var companyMatch = email.subject.match(/Company:\s*([A-Za-z0-9\s]+)/);
        if (companyMatch) {
            contact.company = companyMatch[1].trim();
        }

        // Parse phone from body
        var phoneMatch = email.body_text.match(/Phone:\s*([\+\d\-]+)/);
        if (phoneMatch) {
            contact.phone = phoneMatch[1].trim();
        }

        contact.insert();
        gs.log('New Customer Contact created for ' + email.from);
    } else {
        gs.log('Contact already exists for ' + email.from);
    }

})(current, event, email, logger, classifier);
 

Any guidance or best practices would be appreciated!

 

Thanks & regards,

swetha.

13 REPLIES 13

Ankur Bawiskar
Tera Patron
Tera Patron

@swethavelu 

is customer replying to email they previous received?

If yes then why would you create new contact since already contact received the email.

what's your actual business requirement?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

 

I have deleted the customer contacts that i have in PDI and trying to create new contact whenever servicenow receives inbound email.

@swethavelu 

I don't think this is a valid business requirement when you implement something in CSM space for your customers.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

 

Clarification of Requirement:

I have a fresh CSM Workspace installed via the plugin. After enabling inbound email in the email properties, I configured email accounts to receive incoming emails. When an external customer sends an email to ServiceNow to report an issue, the process should work as follows:

  1. Check if the customer contact exists in the CSM Customer Contact table by matching the email address.
  2. Since the customer is new, the contact will not exist. Therefore, I need to create a new contact for them.
  3. After creating the contact, I want to automatically create a new case for that customer.

Currently:

  • I have a Flow for auto case creation.
  • I have an Inbound Action (Type = “New”) for auto contact creation.

Issue: When ServiceNow receives an email, the processing order triggers the auto case creation flow first, creating the case. The inbound action for contact creation does not run afterward because its type is set to “New.” As a result, the contact is never created.

Goal: I need both requirements to be met:

  • Create the contact if it doesn’t exist.
  • Create the case for that contact.