Parse a Number out of an Inbound Email Subject line and Populate a Field

marrerocmm
Tera Contributor

I am trying to parse a ticket number from an integrated system out of an email subject line and populate a custom ticket number field on the incident form with that number. Does anyone know how to do this? Thank you in advance!

7 REPLIES 7

Tony Chatfield1
Kilo Patron

Hi, normally you would use javascript string methods within your inbound action,

JavaScript String Methods (w3schools.com)

Specific details are not possible as this is dependent on formatting of the email subject and as you post contains no clear details of your configuration or message format.
Here is the basic syntax, you will be able to run this on your instance in a background window for testing and can update the email object so that the subject matches the format you will be receiving.

var email = {"subject":"this is a testNumber: 12345 and some text after the number",}

gs.info('email subject : ' + email.subject);

var numberPrefix = 'testNumber: ';
gs.info('numberPrefix.length : ' +  numberPrefix.length);

var indexStart = email.subject.indexOf(numberPrefix) + numberPrefix.length;
var indexEnd = email.subject.indexOf(' ', indexStart);
var myResult = email.subject.substring(indexStart, indexEnd);

//commented out so the script runs in background without current object.
//current.yourCustomField = myResult;

gs.info('myCustomFieldValue : ' + myResult);

 

Hi @Tony Chatfield

Thank you so much for your help. The email subject details are "Service Desk Ticket # [123456] - Created". How would you modify your script above with those details?

Thank you in advance!

 

Hi, in the example I provided you would need to update the numberPrefix to reference your subject line and while it would be possible to set a prefix of '[' this could cause issues if additional '[' was added to the subject line and so matching a larger string is less likely to cause issues.  The indexEnd can match ']' as the code looks for the first ']' after the numberPrefix and so the result will always be correct. Try this

var numberPrefix = 'Ticket # [';
var indexStart = email.subject.indexOf(numberPrefix) + numberPrefix.length;
var indexEnd = email.subject.indexOf(']', indexStart);
var myResult = email.subject.substring(indexStart, indexEnd);

 

Thanks for sharing this. It worked for me.