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.

9 REPLIES 9

That you need to set in property first:

 

something similar need to check:

 

DrAtulGLNG_0-1763394516064.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Hi Swetha, New means we need to send a new mail to the instance address. Reply means sending reply to the existing mail. Please send a new mail and check

Hi @swethavelu ,

then have 3 inbound email actions created one for each type and have the same script in place and set the order to low and uncheck the stop processing for other items to process

ChaitanyaILCR_0-1763394304714.png

use below script just replace log with info just in case if you have created this is scoped app and also added try catch incase something goes wrong

 

and one more thing to check are you sure you want to create contact even if there is an existing user record(sys_class_name=sys_user)? if not have that check in place otherwise the system would create the contact for the users and when they send emails contact get attached as a user

 

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

        gs.info('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.info('New Customer Contact created for ' + email.from);
        } else {
            gs.info('Contact already exists for ' + email.from);
        }
    } catch (err) {
        gs.error('Inbound Email action error  ' + err);
    }
})(current, event, email, logger, classifier);

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @swethavelu 

So you want that whenever a customer sends an email, a case must be created from the inbound action. If yes, then the Type = New field is required.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

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