Collections provides a Javascript API and an in-app code editor.
The Javascript API is limited to Sources, you can use Javascript to search and create documents querying external services (E.g. Google Books). To create a Source, open a collection → tap on button → Edit → Sources.
The API is accessible through the app object.
app.query
This object contains informations about the current query. The query type is determined by the input method (text or barcode).
app.query.isText()bool
Returns true for a text query.
app.query.isBarcode()bool
Returns true for a barcode query.
app.query.valueobject
The query value. For text and barcode queries, the value is a string.
app.params
In a Source, this object will store parameters passed from the search script to the document script.
app.log(object)
Write a message to the console. Equivalent to console.log()
Result
app.result()
This function must be called at the end of the script to return a result. This function has a different declaration depending on the context.
app.result(resultsarray of search results)
In a search script, you should call it by passing an array of search results.
app.result(builderdocument builder)
In a document script, you should call it by passing a document builder.
app.fail()
Call this function to terminate the script execution. You can call it at any point in the code.
Networking
app.request(urlstring)request
Create a new HTTPS request with the specified URL. Note: the app supports only secure HTTPS requests, any http URL is converted to https.
request.methodstring
Set or get the method for the request. "GET" or "POST"
request.bodystring
Set or get the body used by the POST request.
request.addHeaderValue(valuestring, keystring)
Add a header value specifying a value and a key.
request.send()response
Sends the request and returns a response object.
response
The response returned by request.send()
json()JSON object
Parse the response as JSON. It returns undefined if the object can't be parsed.
image()image
Parse the response as image. It returns undefined if the image is not valid.
xml()XML Document
Parse the response as XML. It returns undefined if the response can't be parsed. Please visit XML Parser for the XML parser documentation.
statusCodeinteger
Get the response status code. If the request fails, the status code is -1.
successbool
True if the server sends any response. False otherwise (no connection).
app.image.fromURL(urlstring)image
A shorthand to request an image from web, it returns an image object, or undefined if the request fails.
let request = app.request("https://www.google.com");
let response = request.send();
let data = response.json();
Storage
app.storage
You can use app.storage as a key-value persistence storage. app.storage must be a dictionary and can store any serializable object like string, number, date, array. The dictionary is initialized before evaluating a script.
Set list items (as an array of suggestions) for the specified field. mergeValues is optional. If mergeValues is true, existing values will be merged with the new values.
Set documents (as an array of builders) for the specified field. mergeValues is optional. If mergeValues is true, existing values will be merged with the new values.
Set managed documents (as an array of builders) for the specified field. mergeValues is optional. If mergeValues is true, existing values will be merged with the new values.
Set a group (as a group builder) for the specified field. mergeValues is optional. If mergeValues is true, existing values will be merged with the new values.
document.setEntity(namestring)
Set the entity for the document by passing the entity name.
document.setIdentifier(fieldAliasstring)
Set a field as the identifier to find an existing document. Optional.
document.setTitle(fieldAliasstring)
Set a field as the title of the suggestion. By default, the first field added to the builder is used as the title.
For URL, Email, Phone, Barcode you can simply use setString.
For Time interval you can use setDecimal specifying the value in seconds.
For "Date and time" you can use setDate.
For Color you can use setString specifying the hex value (e.g. #00FF00).
When you create a document, you usually want to set existing list items or documents to it. To solve this problem, the app uses the concept of suggestion.
A suggestion contains an identifier and all the data needed to create the item. The app will find the item matching the specified identifier, and if that item doesn't exist, it will create a new one.
Create a new suggestion for a list item specifying an alias and a name. By default, the alias is used as the identifier.
suggestion.aliasAsIdentifier()
Use the alias as the identifier.
suggestion.nameAsIdentifier()
Use the name as the identifier.
let listItem = app.listItem.suggest("fantasy", "Fantasy");
document.setListItem(listItem, "genre");
Suggest a document
To suggest a document, create a new document builder and call builder.setIdentifier(fieldAlias) to set a field as the identifier. If you are creating a new document, you don't need to call this method.
let author = app.document.builder();
author.setIdentifier("name");
author.setString("J. K. Rowling", "name");
let book = app.document.builder();
book.setString("Harry Potter", "name");
book.setDocument(author, "author");
The example creates an author with the name field as the identifier. The author is then saved in a book, the app will find an author with "J. K. Rowling" as name or it will create a new one if that doesn't exist.
A value container is an abstract wrapper that provides utilities such as testing equality, comparison or formatting to string.
getValue()Javascript object
Get the underlying Javascript object.
equals(anotherContainerValue container)bool
Returns true if the value is equal to another value.
compare(anotherContainerValue container)integer
Returns 0 if the values are equal. -1 if the first value is less than the second one. 1 if the first value is greater than the second one.
toString()string
Returns the value as a formatted string.
// During a document update
let document = app.currentDocument;
let title = document.getValue("title");
// Find a document by short ID
let document = app.document.findById("ABC123");
let title = document.getValue("title");
Updating a document
A document can be updated from a source. To begin with, enable the feature from → Edit → Sources → Source → Document update enabled.
Then open a document, and from the detail view, tap on → Update from... → Source.
app.currentDocumentdocument
When updating a document, this property contains the current document.
app.actionaction
The action performed by the script (create or update). It's an enum-like value, valid values are listed below.
app.actions.CREATE
app.actions.UPDATE
Utility
app.date.isValid(datedate)bool
Returns true if the date object is valid.
app.date.fromUnixTimestamp(timestampint)date
Create a date from an Unix timestamp. It returns a date or undefined if the date is not valid.