Script
In this article
Overview
You can use the Script Snap to execute Javascript, Python, or Ruby scripts using the JVM ScriptEngine mechanism. Accounts are not required to work with this Snap.
You cannot create external process (like the popen
function) on Cloudplex through the Script Snap or a Custom Snap. While external process creation on Groundplex is possible. You can request support@snaplogic.com to disable this if required.
Snap Type
The Script Snap is a write-type Snap.
Support for Ultra Pipelines
Works in Ultra Pipelines if you pass the original document to the write()
method for the output view. For example, in the JavaScript template, the body of the loop is per the following:
Note the following two arguments in the output.write()
method:
The first argument is the input document—doc.
The second argument is the data for the output document—wrapper.
Both the arguments are required so that the lineage of the output document can be tied to the input document. This is important for an Ultra pipeline responding to web requests. The initial request becomes an input document to the first Snap in the SnapLogic pipeline, eventually resulting in an output document from the last Snap in the pipeline. The pipeline must maintain the lineage of each document so that each response can be correlated to the request that generated it.
Limitations and Known Issues
For JavaScript, objects written to the output view should be composed of serializable Java types. Some downstream Snaps, such as the Copy Snap, require this. To write out a map to the output view in a JavaScript, use a Java Map implementation, such as HashMap
or LinkedHashMap,
per the following:
Breaking Changes
The following breaking changes apply to the pipelines using the Script Snap (or the deprecated Execute Script Snap) with the Python engine.
To implement Python in the Script Snap, we use Jython. We have upgraded the Jython engine from version 2.7-b3 (a beta version from 2014) to version 2.7.2 (March 2020). The following are the resultant issues and workarounds:
An open bug in 2.7 introduced a backward-incompatible change in the SnapLogic platform wherein the Jython engine automatically converts BigInteger values to primitive long values. This impacts all your scripts that perform numeric manipulation of integer values from documents (SnapLogic uses the BigInteger type to represent integers in documents). Your pipelines and Snaps with the Script Snap (or the deprecated Execute Script Snap) that use numeric manipulation scripts with integer or BigInteger data type may fail during execution. We recommend you prospectively replace integer or BigInteger values with long values.
Example:
sum = a.intValue() + b.intValue()
Here,a
andb
are of BigInteger type that now fail as Jython 2.7.2 automatically and transparently callslongValue()
on any BigInteger value it encounters. Soa
andb
would need to use the long and not BigInteger type.The known fix is to rewrite the above calculation as
sum = a + b
by removing occurrences of.intValue()
or.longValue()
from your Python scripts.Before the 4.22 release (August 2020), when using the Script Snap with the Scripting language option selected as Python, requesting a key that did not exist in a dictionary (for example,
my_dict['missing_key']
) would returnNone
. Starting from the 4.22 release, the same request returns aKeyError
exception. If you need to continue returningNone
, use the .get(key) method instead (for example,my_dict.get['missing_key']
).- zlib.compress()
: The zlib library compresses the JSON files retrieved from the SnapLogic APIs and backs-up Pipelines and accounts to a database. The following Python code, when trying to compress displays anascii … ordinal not in range(128)
error.
Original code:in_doc["json"] = zlib.compress(in_doc["json"])
Fix: in_doc["json"]= zlib.compress(in_doc["json"].encode("utf-8"))
{dictionary}.values().toArray()[i]:
Before the 4.22 release (August 2020), to subscript a{dictionary}.values()
method, you had to append thetoArray()
method tovalues()
;e
lse, you would see theFailure: ‘java.util.LinkedHashMap$LinkedValues’ object is unsubscriptable
error. After the 4.22 release,toArray()
returns Failure: ‘list’ object has no attribute ‘toArray’. However, the requirement fortoArray()
is no longer necessary for the subscript.
Original code:sLine = data.values().toArray()[0]
Fix: sLine =data.values()[0]
Snap Views
Type | Format | Number of Views | Examples of Upstream and Downstream Snaps | Description |
---|---|---|---|---|
Input | Document
|
|
| This Snap has at most one document input view. |
Output | Document
|
|
| This Snap has at most one document output view. |
Error | Error handling is a generic way to handle errors without losing data or failing the Snap execution. You can handle the errors that the Snap might encounter when running the pipeline by choosing one of the following options from the When errors occur list under the Views tab:
Learn more about Error handling in Pipelines. |
Snap Settings
Asterisk ( * ): Indicates a mandatory field.
Suggestion icon (): Indicates a list that is dynamically populated based on the configuration.