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.

Inbound Email actions

Suhail2297
Tera Contributor

Hello team,

I had a requirement for the inbound email action - 

When some user sends and email with the subject that starts with {Asset Reutilization} and in body { Asset serial number - 123 , Asset location - xyz , Asset centre number - 123 , Asset state - Instock  } 

Based on this body asper the details given we need to chnage the state of assest as requested in email and also the location based on centre number .

 

For this i got a script;

Name - Asset Reutilization Update

Type - New

Condition  - email.subject && email.subject.toLowerCase().indexOf("asset reutilization") > -1

Script - 

(function process(inboundAction, email, event, logger) {

     var body = email.body_text; // Plain text version of the email body
     var serial = "";
     var state = "";
     var centreNum = "";

     // Regex patterns to match both case styles (case-insensitive)
     var serialMatch = body.match(/asset serial(?: number)?\s*[-:]\s*([^\n,]+)/i);
     var stateMatch = body.match(/state\s*[-:]\s*([^\n,]+)/i);
     var centreMatch = body.match(/centre number\s*[-:]\s*([^\n,]+)/i);

     if (serialMatch) serial = serialMatch[1].trim();
     if (stateMatch) state = stateMatch[1].trim();
     if (centreMatch) centreNum = centreMatch[1].trim();

     if (!serial) {
         logger.log("No asset serial found in email body");
         return;
     }

     // Find the hardware record by serial number
     var assetGR = new GlideRecord("alm_hardware");
     assetGR.addQuery("serial_number", serial);
     assetGR.query();

     if (assetGR.next()) {
         logger.log("Asset found: " + assetGR.sys_id);

         // Map states to install_status values in ServiceNow
         if (state) {
             // Example: mapping text to install_status codes
             var stateMap = {
                 "instock": "6", // sys_choice value for In Stock
                 "retired": "7" // sys_choice value for Retired
             };
             var normalizedState = state.toLowerCase();
             if (stateMap[normalizedState]) {
                 assetGR.install_status = stateMap[normalizedState];
             }
         }
         if (centreNum) {
             // Update location based on centre number
             var locGR = new GlideRecord("cmn_location");
             locGR.addQuery("u_centre_number", centreNum);
             locGR.query();
             if (locGR.next()) {
                 assetGR.location = locGR.sys_id;
             }
         }
         assetGR.update();
         logger.log("Asset updated successfully");
     } else {
         logger.log("No asset found with serial: " + serial);
     }
 })(inboundAction, email, event, logger);
 
This script is not working ......can someone help me here?
Thankyou In Advance
 
Regards,
Suhail
6 REPLIES 6

kaushal_snow
Giga Sage

Hi @Suhail2297 ,

 

I have modified your code, check once

 

 

(function process(inboundAction, email, event, logger) {

var body = email.body_text; // Plain text version of the email body
var serial = "";
var state = "";
var centreNum = "";

// Regex patterns to match both case styles (case-insensitive)
var serialMatch = body.match(/asset serial(?: number)?\s*[-:]\s*([^\n,]+)/i);
var stateMatch = body.match(/state\s*[-:]\s*([^\n,]+)/i);
var centreMatch = body.match(/centre number\s*[-:]\s*([^\n,]+)/i);

if (serialMatch) serial = serialMatch[1].trim();
if (stateMatch) state = stateMatch[1].trim();
if (centreMatch) centreNum = centreMatch[1].trim();

if (!serial) {
logger.log("No asset serial found in email body");
return;
}

// Find the hardware record by serial number
var assetGR = new GlideRecord("alm_hardware");
assetGR.addQuery("serial_number", serial);
assetGR.query();

if (assetGR.next()) {
logger.log("Asset found: " + assetGR.sys_id);

// Map states to install_status values in ServiceNow
if (state) {
// Example: mapping text to install_status codes
var stateMap = {
"instock": "6", // sys_choice value for In Stock
"retired": "7" // sys_choice value for Retired
};
var normalizedState = state.toLowerCase();
if (stateMap[normalizedState]) {
assetGR.install_status = stateMap[normalizedState];
}
}
if (centreNum) {
// Update location based on centre number
var locGR = new GlideRecord("cmn_location");
locGR.addQuery("u_centre_number", centreNum);
locGR.query();
if (locGR.next()) {
assetGR.location = locGR.sys_id;
}
}
assetGR.update();
logger.log("Asset updated successfully");
} else {
logger.log("No asset found with serial: " + serial);
}
})(inboundAction, email, event, logger);

Try this once and let me know. If you find this helpful, please accept this as a solution and hit the helpful button..

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

Hello @kaushal_snow 

Thankyou for your quick response...

I used this code but still its not changing the hardware record

Nilesh Pol
Kilo Sage
Kilo Sage

@Suhail2297 ,

please verify with below sccript:

 

(function process(inboundAction, email, event, logger) {
var body = email.body_text;

var serial = "";
var state = "";
var centreNum = "";

// Regex patterns to match both case styles (case-insensitive)
var serialMatch = body.match(/asset serial(?: number)?\s*[-:]\s*(.+)/i);
var stateMatch = body.match(/state\s*[-:]\s*(.+)/i);
var centreMatch = body.match(/centre number\s*[-:]\s*(.+)/i);

if (serialMatch) serial = serialMatch[1].trim();
if (stateMatch) state = stateMatch[1].trim().toLowerCase();
if (centreMatch) centreNum = centreMatch[1].trim();

if (!serial) {
logger.log("No asset serial found in email body.");
return;
}

// Find the hardware record by serial number
var assetGR = new GlideRecord("alm_hardware");
assetGR.addQuery("serial_number", serial);
assetGR.query();

if (assetGR.next()) {
logger.log("Asset found: " + assetGR.sys_id);

var updated = false;

// Map states to install_status values in ServiceNow
var stateMap = {
"instock": "6", // sys_choice value for In Stock
"retired": "7" // sys_choice value for Retired
};

if (state && stateMap[state]) {
assetGR.install_status = stateMap[state];
updated = true;
} else if (state) {
logger.log("State value '" + state + "' not recognized.");
}

if (centreNum) {
var locGR = new GlideRecord("cmn_location");
locGR.addQuery("u_centre_number", centreNum);
locGR.query();
if (locGR.next()) {
assetGR.location = locGR.sys_id;
updated = true;
} else {
logger.log("No location found with centre number: " + centreNum);
}
}

if (updated) {
assetGR.update();
logger.log("Asset updated successfully.");
} else {
logger.log("No fields were updated on the asset.");
}

} else {
logger.log("No asset found with serial: " + serial);
}
})(inboundAction, email, event, logger);

 

f you find this helpful, please accept this as a solution and hit the helpful button.

Suhail2297_0-1754666974860.png

I applied your script and created one email in sys_email.DO to trigger this...but still the state has not been changed