We Use Cookies

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with this.

See our cookie policy.

Automation Action: Execute Script

Execute custom C# or Visual Basic .NET code.

Built-In Action

Executes custom C# or Visual Basic .NET code. Requires the Professional Edition.

You can create a custom script to execute custom logic. Scripts can be written in C# or Visual Basic .NET. Select the Language to use before you edit your script code. Each Script Action must be given a Script Name.

When your Automation executes a Script action it calls the execute(currentMessage message) method which returns a string. This is the entry point. You can edit the code inside this method and you can add any number of additional methods within the ThinkAutomationScript class. The returned string value from the execute method will be assigned to the Assign Return Value To variable which you can then use in your Automation.

Scripts are compiled by ThinkAutomation when they are executed for the first time. Once compiled, scripts will execute as fast as built-in actions. Scripts will be recompiled if they are changed.

If you want to re-use a script on multiple Automations, you can create a Custom Action.

Example script:

using System;
using System.Text;
using ThinkAutomationCoreClasses.Scripting;
public class ThinkAutomationScript
    public string execute(currentMessage message)
            // Action execution code
            // Get automation variable values using message.GetValue("name")
            // Set automation variable values using message.SetValue("name","value")
            message.AddToLog("Returned the subject in lower-case");
            return message.Subject.ToLower();
        catch (Exception e)
            // Pass the error back to the automation log
            return "";
Accessing Automation Variables

Scripts can access and update existing Extracted Fields or Variables. To access a value use:

string name = message.GetValue("name");              

Where "name" is an existing Variable or Extracted field name. Values are always returned as strings.

You can drag and drop a variable onto the script editor - it will be converted to: message.GetValue("variablename").

You can also access any of the built-in variables, solution constants, global constants & system variables:

string messageTo = message.GetValue("%Msg_ToWithNames%"); // the enclosing % signs are optional.              

To set an existing variable or extracted field use:


Where "name" is an existing variable or extracted field name. The "value" can be any string value.

Accessing Message Properties

The message object contains read-only properties for the currently executing message. For example: message.Subject can be used to access the subject directly. Main properties:

Property Name Details
MessageId The id of the incoming message.
MimeText The full mime text of the incoming message.
Subject The message subject.
BodyPlainText The plain text body. If the incoming message is html without a plain text body then this property will return the plain text version of the html (with all tags removed).
BodyPlainTextLastReply The plain text body with previous replies and quoted text removed.
BodyHTML The html body.
Dated The message date (datetime).
From The from address.
ReplyTo The reply-to address.
ToAddress The to address.
CC, BCC The cc / bcc address.
Size The message size (integer).
AutomationName The name of the Automation currently executing.
SolutionName The name of the Solution containing the Automation.
SolutionEmail The email address assigned to the Solution.
TempPath The path to the temporary files folder for the Solution.
Accessing Attachments

The message.Attachments property contains a list of currentMessageAttachment objects for the current message.

currentMessageAttachment Attachment;
foreach (var Attachment in message.Attachments)
  string name = Attachment.Name;
  int size = Attachment.Size;
    string contentType = Attachment.ContentType; // for example 'application/pdf'
  string path = Attachment.Location; // this contains the temporary location of the attachment file

The Location property of the Attachment object contains the path to the temporary location of the attachment file during Automation execution.

You can also access related items (embedded attachments in email messages) using message.RelatedItems.

Accessing Headers

The message.MessageHeaders property contains a list of currentMessageHeader objects for the current message.

currentMessageHeader Header;
foreach (var Header in message.MessageHeaders)
  string hname = Header.Name;   // the header name
  string hvalue = Header.Value; // the header value
Adding To The Automation Log

You can add an entry to the Automation Log using:

message.AddToLog("script message");              
Handling Exceptions

To add an error to the Automation Log use:

message.AddErrorToLog("A script error occurred");              

Inside any methods you create in your script you should always use Try .. Catch blocks to catch any Exceptions and then use the message.AddErrorToLog method to pass details of the error back to the Automation. This will then show any script errors in your Automation log. For example:

static int ordervalue(currentMessage message)
    int qty = Convert.ToInt32(message.GetValue("qty"));
    decimal price = Convert.ToDecimal(message.GetValue("price"));
    return qty * price;
  catch (Exception e)
    // Pass the error back to the automation log
    message.AddErrorToLog("Script Error:" & e.Message);
    return 0;
.NET References

Scripts can reference other .NET Framework assemblies compatible with .NET Framework 4.7 or higher. Use the References tab to add additional references. You can add any of the .NET framework System assemblies. Any other .NET referenced assembly must be located in the ThinkAutomation program files folder (unless you use a NuGet package - see below).

NuGet Packages

You can also add NuGet packages to scripts. Click the NuGet Packages button to open the NuGet Package Manager. Enter a search term and click the search button to view available packages. Select a package and click the Add Reference button to download & install the package. A reference to the package (and any dependencies) will be added to your script. See: https://www.nuget.org for more information.

Http Helper Class

If you need to make http requests inside your execution code, you can optionally use the helpersHttp class which simplifies the process. See: Using The Http Helper Class for more information.

Validating A Script

Click the Check button to validate that the script compiles and executes successfully. The Error tab will show any script errors, the Output tab will show any output from message.DebugPrint, message.AddToLog or message.AddErrorToLog calls.

Adding To The Output Window

To assist with script development you can add data to the Output window using message.DebugPrint :


Any calls to message.DebugPrint will show in the Output window in the script editor when the Check button is used, and are ignored when the script executes during Automation execution.

Generating Scripts Using ChatGPT

If you have an OpenAI account you can add your OpenAI API key to the ThinkAutomation Server Settings - Integrations - ChatGPT section. This will enable the Ask ChatGPT button on the script editor toolbar.

Click the Ask ChatGPT to open the Generate Script Using ChatGPT window. Enter a description of what you need your script to do. Be specific with your description and what you want it to return. If you need your script to access Automation %variables% use the term 'automation variable'.

For example:

Send an SMS message using the Esendex API. The SMS message is in the automation variable 'SMSMessage'. The recipient phone number is in the automation variable 'PhoneNumber'. Return 'sent' along with the message id if the message is sent successfully, otherwise return the error message and add the error to the error log.              


Read the current account balance from Stripe using the Stripe API. Return the balance. The API key is in the automation variable 'StripeAPIKey'.              

When mentioning automation variables in your description this can may any %variable%, Extracted Field or Constant.

ThinkAutomation gives ChatGPT information about how scripts work and the objects available.


If the message subject contains 'quote', save any PDF attachments to a folder called 'Quotes'. Add the current date & time to the saved file name to ensure its unique. Return the number of attachments saved.              

If the message subject contains 'quote' and the automation variable 'IsCustomer' is equal to 'true', create a string in iCalendar text format and return it. Also set the automation variable 'Appointment' to 'set'. The start and end dates should be 30 days from the message date. The summary is the message subject. The organizer is the message to address.              

Find all links in the message html and return them one per line with duplicates removed.              

Click the Ask ChatGPT button to send the request to ChatGPT. Your script will be displayed. Click OK to add the generated script to the script editor.


ChatGPT code generation use useful to get you started and in may cases it can generate a script that can be used with no changes, however it is not foolproof. You should always test the script first. ThinkAutomation instructs ChatGPT to not use external libraries - however it may still do so - in which case these would need to be added as references.