Using Snippets to Share Project Logic

Posted on October 18, 2022
Text

Snippets are small sections of Project logic that can be reused across multiple workflows. They are a way to make consistent changes across multiple Projects. This saves time and ensures quality when the same actions can be repeated through several Projects. Snippets can contain just a few lines of logic or can be much more complex if needed.

Error reporting is one common use for Snippets. Here is an example with a Module that sends an email to an administrator. The email will have the error message and job log attached:

Image
the module an Email
the module an Email
Image
Using Snippets to Share Project Logic
Using Snippets to Share Project Logic

Creating the Snippet

Text

To implement this into some current Projects so that if they fail an email alert will be received, here are the steps. First, we need to get the XML that we will be working with. You can click the Show XML button in the Project to view the code and copy the relevant section. Here, we will grab everything inside of the OnError Module:

Image
Creating the Snippet
Creating the Snippet
Text

Copy this code into a text document. Now, you will need to add the <importFile> tags to the beginning and end of the XML. Save the file as an XML with any name you like and place it in a location where it will be accessible by the GoAnywhere MFT application.

Image
Creating the Snippet
Creating the Snippet

Implementing the Code

Text

After saving the file, go back into GoAnywhere and find a Project to use with the Snippet. In this example, our Snippet is a Module that will report errors with an email, so a Project was created ahead of time that will intentionally throw an error. The file that is specified in the Search and Replace Task does not exist, but the Project will not know that until it runs and looks for it.

Image
Compiling the Snippet
Compiling the Snippet
Text

Go to the Component Library and find the Import Project Source Task. Since our Snippet in this example is encompassing a Module, we will add it under the Project Component. Browse to the location of the Snippet or enter the path manually.

Image
Calling a Module in the Snippet
Calling a Module in the Snippet
Text

In this example, we want the Project to call our Module in the Snippet when an error occurs, so we’ll go to the Project Component and set On Error – call:OnError.

Image
Runing the project
Runing the project
Text

Validate the Project to save it and ensure it compiles correctly. Once it is confirmed that everything is fitting together correctly, we can go ahead and run the Project. The job starts to run and encounters an error, so it calls the OnError Module in the Snippet. The email is received with the information we set up:

Image
Screenshot of error message on the email
Screenshot of error message on the email

Altering Snippets

Text

Snippets also allow you to make quick changes to any Projects that are using the file. Here, we can simply change the wording of our email alert, and it will be reflected in all the workflows where the Snippet is used.

Image
Email Error Snippet
Email Error Snippet
Image
Screenshot of error message on the email after altering
Screenshot of error message on the email after altering

Not using GoAnywhere MFT yet? Start a free trial and test it out for 30 days.

 

START FREE TRIAL