Automation Action: Execute Script
Execute custom C# or Visual Basic .NET code.
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 ThinkAutomation executes a Script action it calls the execute method. This is the entry point. Inside your script you can add any number of additional methods. The returned value from the execute method will be assigned to the Assign Return Value To variable.
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.
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:
message.SetValue("name","value");
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 incoming 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. |
From | The from address. |
ReplyTo | The reply-to address. |
ToAddress | The to address. |
CC, BCC | The cc / bcc address. |
Size | The message size. |
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 path = Attachment.Location;
}
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;
string hvalue = 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)
{
try
{
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
:
message.DebugPrint("test");
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.