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

Chaitanya Redd1
Tera Guru

Can you try below script

(function process(inboundAction, email, event, logger) {
    var body = email.body_text || email.body; // Fallback if body_text is empty
    logger.log("Email body:\n" + body);

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

    // More flexible regex matching
    var serialMatch = body.match(/asset serial(?: number)?\s*[-:]\s*([^\n\r,]+)/i);
    var stateMatch = body.match(/asset state\s*[-:]\s*([^\n\r,]+)/i);
    var centreMatch = body.match(/centre number\s*[-:]\s*([^\n\r,]+)/i);

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

    logger.log("Extracted values - Serial: " + serial + ", State: " + state + ", Centre Number: " + centreNum);

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

    var assetGR = new GlideRecord("alm_hardware");
    assetGR.addQuery("serial_number", serial);
    assetGR.query();

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

        // Install status mapping
        var stateMap = {
            "instock": "6",
            "in stock": "6",
            "retired": "7",
            "in use": "1",
            "pending": "2"
            // Add more if needed
        };

        var normalizedState = state.toLowerCase();
        if (stateMap[normalizedState]) {
            assetGR.install_status = stateMap[normalizedState];
            logger.log("Install status updated to: " + stateMap[normalizedState]);
        } else {
            logger.log("No valid install status mapping for: " + state);
        }

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

        assetGR.update();
        logger.log("Asset record updated successfully.");
    } else {
        logger.log("No asset found with serial number: " + serial);
    }
})(inboundAction, email, event, logger);

@Chaitanya Redd1 

Still this did not change the values for the selected location