Help

Re: Automation script

Solved
Jump to Solution
1151 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Richard_Foxwort
7 - App Architect
7 - App Architect

I want to create an Automation that will sort inbound emails into categories for me, and label them

I already capture the email text content via Zapier which creates a new record for each email and, among other things, writes the email text body to a Multiline text field in the record.

I then need to classify each email, and label the record accordingly. I have about 12 different message types. Each record includes a Single Select field with 12 options, I use this field to label each record according to message type.

My message types are pretty simple and each record can be classified just by looking for and matching a unique text string within the text body from each email - ie the message body can be very long, and I’m looking for particular unique strings within the message.

This seems like it should be a job that Airtable Automation could do pretty easily, but I’m a noob with javascript.

Essentially what I want is a script that will look at each new email and check if the text email body includes one of my unique strings, and depending on which string matches, it will then update the label for the record. If nothing matches, I want it to be labelled ‘Other’.

The message types are mutually exclusive so as soon as it finds the any match, the record can be labelled and the script can wait until the next email record gets written to my Airtable

I’m trying to figure out how to use this example to do the string matching bit, which I think I understand. Not sure how to do the labelling piece

1 Solution

Accepted Solutions
Kamille_Parks
16 - Uranus
16 - Uranus

First set up an automation to run when a new record is created or when it enters a particular view.

Then add a Run a Script action. You’ll need a script input variable, set it up like so:
“Input variable name”: emailBodyText => “Input variable value”: Record (Step 1: Trigger) ... {Email Body Text Field}

Then edit the script to be something like this:

const text = input.config().emailBodyText

const classifications = [
    {uniqueString: "Lorem ipsum", category: "Category 1"},
    {uniqueString: "luctus lobortis", category: "Category 2"},
    {uniqueString: "Aliquam erat", category: "Category 3"},
    // You should have twelve of these objects in this array since you have twelve categories
]

const matchesFound = text ? classifications.filter(x => {
    return text.includes(x.uniqueString)
}) : []

output.set('categoryName', matchesFound.length ? matchesFound[0].category : null)

Lastly add another action for Update A Record. Pick the same table, use the RecordID from your trigger (Step 1), and add your SingleSelect field using the output from your Script action step.

See Solution in Thread

8 Replies 8
Kamille_Parks
16 - Uranus
16 - Uranus

First set up an automation to run when a new record is created or when it enters a particular view.

Then add a Run a Script action. You’ll need a script input variable, set it up like so:
“Input variable name”: emailBodyText => “Input variable value”: Record (Step 1: Trigger) ... {Email Body Text Field}

Then edit the script to be something like this:

const text = input.config().emailBodyText

const classifications = [
    {uniqueString: "Lorem ipsum", category: "Category 1"},
    {uniqueString: "luctus lobortis", category: "Category 2"},
    {uniqueString: "Aliquam erat", category: "Category 3"},
    // You should have twelve of these objects in this array since you have twelve categories
]

const matchesFound = text ? classifications.filter(x => {
    return text.includes(x.uniqueString)
}) : []

output.set('categoryName', matchesFound.length ? matchesFound[0].category : null)

Lastly add another action for Update A Record. Pick the same table, use the RecordID from your trigger (Step 1), and add your SingleSelect field using the output from your Script action step.

That’s a beautifully compact script, @Kamille_Parks! I hope you don’t mind a small addition/suggestion, which would be to compare the lower case versions of both the unique string and the email body text, primarily because the includes() method is case-sensitive. I suggest writing all of the unique strings in lower case in the classifications array of objects, and change one line:

    return text.toLowerCase().includes(x.uniqueString)

Good catch! That’ll make matches more fool-proof.

Thanks Kamille! Much appreciated.

I’ve pretty much got this working already and it will save a lot of time!

I have a couple of questions:

  • In the array of classifications, does the last '{uniqueString … ’ line need to with a comma?
  • How to include a catch-all so that any unmatched records will be classified as ‘Other’

Thanks

No, the last item in that array doesn’t need the comma after it.

Assuming you already have “Other” as an option for that SingleSelect field, change null in the last line to "Other". For your reference, what that last line is doing is: create a variable that equals the first matching category, otherwise set the variable to “Other” (previously null aka empty).

Thanks Kamille this is great!

Out of respect for @Kamille_Parks for providing the primary script, I would kindly ask that you mark her comment containing the script as the solution. My one-line tweak is nothing without what she did.

Oh, thanks for pointing that, out - I actually ticked both and did not realise by ticking yours second I’d be un-ticking the first one. Should have realised tagging the ‘right’ answer is mutually exclusive. Thanks also for your contribution which has been helpful for me on other things too.