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.

How to store data from email into custom table fields

aishwaryajn
Tera Contributor

I have a requirement for one use case where am getting mail from different tool 
Email sample:

Name: abc

Email: abc@gmail.com

Phone no: 987654320

....etc

Need all these info to be stored in custom table sys_user1 table with fields mapped

 

I will create a inbound email action, but what script needs to be added to create and store data in that custom table and field mapped ?

 

1 REPLY 1

Maik Skoddow
Tera Patron
Tera Patron

Hi @aishwaryajn 

 

You are on the right track by using an Inbound Email Action in ServiceNow. To store the extracted information into your custom table (sys_user1), you need to write script that:

  1. Parses the email body to extract each field's value.
  2. Maps those values to the appropriate fields in sys_user1.
  3. Inserts new record.

Below is sample script you can use in the Inbound Email Action (in the "Actions" tab, inside the "Script" field):

 

// Get the email body as plain text
var body = email.body_text;

// Helper function to extract fields
function extractValue(pattern, body) {
    var regex = new RegExp(pattern + "\\s*:\\s*(.*)", "i");
    var match = body.match(regex);
    return match ? match[1].trim() : "";
}

// Extract values
var name = extractValue("Name", body);
var emailAddress = extractValue("Email", body);
var phone = extractValue("Phone no", body);

// Now create a record in your custom table
var gr = new GlideRecord('sys_user1'); // Use your actual table name
gr.initialize();
gr.u_name = name;       // Change u_name to your field name in sys_user1
gr.u_email = emailAddress;  // Change u_email accordingly
gr.u_phone_no = phone;      // Change u_phone_no accordingly
// Map additional fields as needed here

gr.insert();

 Maik