The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Inbound Email action

Suhail2297
Tera Contributor

Hello Everyone,

Need some help

 

I have an requirement to create a inbound email action to update and alm_hardware record for which ib have written the scipt and its working fine

ISSUE - Sometimes its working and soemtimes its not working , i tried to change the execution order also but still the same issue.

 

Below is the code and screenshot for the condition:

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*Optional EmailLogger*/ logger, /*EmailClassifier*/ classifier) {
    var emailBody = email.body_text.toLowerCase();

    // Regex pattern to capture multiple asset records
    // Expected format:
    // Asset serial number - 123, State - in stock, Centre number - 134
    var pattern = /Asset serial number\s*-\s*(\S+).*?State\s*-\s*([\w\s]+).*?Centre number\s*-\s*(\d+)/gi;

    var match;
    while ((match = pattern.exec(emailBody)) !== null) {
        var serialNumber = match[1].trim();
        var stateText = match[2].trim();
        var centreNumber = match[3].trim();

        var installStatus = '';
        var subStatus = null; // null means don't touch it

        // Map state to install_status (+substatus only for in stock)
        if (stateText.toLowerCase() == 'in stock') {
            installStatus = '6'; // In Stock
            subStatus = 'available';
        } else if (stateText.toLowerCase() == 'retired') {
            installStatus = '7'; // Retired
            subStatus = ''; // Do not update substatus
        }

        // Lookup location sys_id
        var locationSysId = '';
        if (centreNumber) {
            var locGR = new GlideRecord('cmn_location');
            locGR.addQuery('u_centre_number', parseInt(centreNumber, 10));
            locGR.query();
            if (locGR.next()) {
                locationSysId = locGR.sys_id.toString();
            }
        }

        // Update asset record
        if (serialNumber) {
            var hwGR = new GlideRecord('alm_hardware');
            hwGR.addQuery('serial_number', serialNumber);
            hwGR.query();
            if (hwGR.next()) {
                if (installStatus) hwGR.install_status = installStatus;
                if (subStatus !== null) hwGR.substatus = subStatus; // update only if defined
                if (locationSysId) hwGR.location = locationSysId;
                hwGR.update();
                logger.info('Updated asset: ' + serialNumber + ' | State: ' + stateText + ' | Centre: ' + centreNumber);
            } else {
                logger.warn('No hardware found for serial number: ' + serialNumber);
            }
        }
    }

})(current, event, email, logger, classifier);
 
Suhail2297_0-1756277085707.png

Regards,

Suhail

3 REPLIES 3

Mark Manders
Mega Patron

When is it working and when isn't it working? 

Personally, I would have used an inbound flow with this, because it triggers on any incoming email (new, fwd, reply) and you can work with variables to get the information you need to recognize the asset. 

 

If you continue using the script: add some logs to it, to see where it goes wrong. You can take an inbound email that didn't work and reprocess it after you add the logs. That way you can see why it wasn't processed accordingly. 

Also: check the email logs. Is the email processed by this action and just not updating the record, or is it skipping it completely? That could mean that the email isn't recognized to run this action.

 

There are too many variables here to really pinpoint to the cause, especially because you say 'it is working fine, just not always'. That means that there are emails just not recognized or processed correctly and we don't have access to your system to analyze the incoming emails that don't work.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

When im sending email to change the state to retired its working, but when i send an email to change the state to in stock its not working....Can you please help me here?

RaghavSh
Kilo Patron

@Suhail2297 Its veru easy to debug inbound actions.

 

1. Go to sys_email table and look for the email you sent.

2. Open the record and in the related list look for email logs, it will show you all the inbound which are skipped or processed with reason.

3. Look for your inbound actions and see the reason for skipped, it will hep you to figure out what is the issue.


Raghav
MVP 2023