Python: Native Methods

Common variables and dictionaries

Before exploring the native methods themselves, it's important to note a crucial aspect of creating low-code triggers: the use of currentData, beforeSave and afterSave.

These three dictionaries contain valuable information about the data sent by the user and about the database itself.

  • currentData: this is the dictionary that contains the information the user has sent by acting. For example, if a user drags a card in a kanban to the "In Progress" stage, then currentData['stage'] will contain "In Progress".
  • beforeSave: this is the dictionary that contains the information about the record before it is changed (that is, before currentData is saved to the database). For example, if a user drags a card in a kanban from "New" to the "In Progress" stage, then currentData['stage'] will contain "In Progress", and beforeSave["stage"] will contain "New". Bonus: if you change the beforeData in a synchronous, before create/update trigger, this new information will be saved to the record.
  • afterSave: this is the dictionary that contains the information about the record after it is changed (that is, after currentData is saved to the database). For example, if a user drags a card in a kanban from "New" to the "In Progress" stage, then currentData['stage'] will contain "In Progress", as will afterSave["stage"]. Bonus: some information will only be available in afterSave. For example, in an After a record is created trigger, the record's id will only be contained in afterSave, as the id is only registered after the record is saved to the database.
  • currentUser: this is the dictionary that contains the information about user who has triggered the automation. Particularly useful for identification, as it contains id_user, email, and name.

Two other dictionaries are only present when using low-code Functions instead of Triggers:

  • recordData: this holds the information of the record when the Function is invoked through a Button + Run a function combination. In this scenario, it should also be noted that N:1 connection fields will bring the first level of nested information. For example, if you have a "client" field that points to a record in the "Clients" table, you can access information such as recordData['client']['name'] without needing to run a separate search.
  • params: this holds the information you receive when activating the Function through a webhook. For example, if you send the JSON payload {"status":"New"} to the webhook, you can access that information through params['status'] in the Function.

Native Methods

This is a list of native methods and how to use them in low-code triggers.

Create Record

To create a record use the table method using the table name as a parameter, then the insert method using a Dictionary as a parameter. Example:

result = jestor.table('table_name').insert({'name': 'test'})
#result will be a python array with the created record's values

Edit Record

To edit a record use the table method using the table name as a parameter, then the update method using the id of the record that will be changed and a Dictionary as a parameter. Example:

recordId = 1
result = jestor.table('table_name').update(recordId, {'name': 'test'})
#result will be a python array with the updated record's values

Delete Record

To delete a record use the table method using the table name as a parameter, then the delete method using the record id as a parameter. Example:

recordId = 1
jestor.table('table_name').delete(recordId)

List Records

To list records use the table method using the table name as a parameter, then the get method. Example:

result = jestor.table('table_name').get()
#result will be a python array with the fetched records' values

## To apply filters use the Filter object:

filters = [
	Filter('name', 'test', Operators.CONTAINS, 'string'),
]
#instead of the Operators class, you can also pass an operator string such as "=="

result = jestor.table('table_name').get(filters)
#result will be a python array with the fetched records' values

The Filter object receives the following parameters:

  • field: str (field name).
  • value: str (field value).
  • operator: str (operators can be found in the Operators class).
  • type: str (field type).

Count Records

To Count records use the count method using the table name and filter as a parameter.

Example:

filters = [
  Filter("filter", "value", "operator")
]
total = jestor.count(["table_name", filters])

The Filter object receives the following parameters:

Create User

To create a user use the user() method, then the createUser() method passing a list as a parameter. This list must contain the fields in order [email, password, profile_id, name]. Example:

result = jestor.user().createUser(['[email protected]', 'passworda@123', '1','Test']);
#result will be a python array with the created user's values

Inactive User

To inactive a user use the user() method, then the InactiveUser()_ method passing the user id. Example:

result = jestor.user().InactiveUser([id_user])
#result will be thea user data or null if not found the user

List Users

To list users use the user() method, then the get method. The get method accepts the following parameters:

  • filters: List[Filter] = None,
  • limit: int = 100,
  • page: int = 1,
  • sort: str = None

Example:

result = jestor.user().get();
#result will be a python array with the fetched users' values

## To apply filters use the Filter object:

filters = [
	Filter('name', 'test', Operators.CONTAINS, 'string'),
]
#instead of the Operators class, you can also pass an operator string such as "=="

result = jestor.user().get(filters=filters,sort='name asc')
#result will be a python array with the fetched users' values

List Tasks

To list the tasks use the fetchTasks method. The fetchTasks method accepts a parameter list type, which can contain the fields in order [[Filter], limit, page]. Example:

result = jestor.fetchTasks();
#result will be a python array with the fetched tasks' values

## To apply filters use the Filter object:

filters = [
	Filter('description', 'test', Operators.CONTAINS, 'string'),
]
#instead of the Operators class, you can also pass an operator string such as "=="

limit = 10
page = 1
result = jestor.fetchTasks([filters, limit, page])
#result will be a python array with the fetched tasks' values

Files

To manipulate files use the File object. It takes the following parameters:

  • table: str
  • recordId: int = None
  • field: str = None
tableName = 'table_name'
recordId = 1
field = 'file_field'
files = jestor.file(tableName, recordId, field)

Adding, removing and editing files in an existing registry:

tableName = 'table_name'
recordId = 1
field = 'file_field'
files = jestor.file(tableName, recordId, field)

files.add({'name': 'file_test', 'content': '<content string>', 'contentType': 'pdf'})

idFile = 'A1CVwLAxzjjYprO5Bjdfi-5'
files.remove(idFile)

idFile = 'E0DOdSKgvkkMncI8Fnhst-2'
files.update(idFile, {'name': 'file_update_name'})


result = jestor.table('table_teste').update(recordId, {field: files.toJson()})

#result will be a python array with the updated record's values

Adding files to a new record:

tableName = 'table_name'
files = jestor.file(tableName)

files.add({'name': 'file_test', 'content': '<content string>', 'contentType': 'pdf'})

result = jestor.table(tableName).insert({'name': 'create new record', 'files': files.toJson()})

#result will be a python array with the created record's values

POST

You can POST data to an external endpoint using the post method.

url = "htpps://url.com"

headers = {
	"Content-Type":"application/json"  
}

body = {
	"name":"example"  
}

result = jestor.post([url,headers,body])
#result will contain the call's response

GET

You can make GET calls to an external endpoint using get method.

url = "htpps://url.com"

headers = {
	"Content-Type":"application/json"  
}

result = jestor.post([url,headers])
#result will contain the call's response

stopCodeExecution()

During synchronous triggers, you can stop the code execution, rollback all changes and show the user a message with stopCodeExecution().

if currentData["stage"] == "Done" and currentData["amount"] == 0:
  stopCodeExecution("You can't close deals with no $ amount!")

Generate PDF

This method allows you to create a custom PDF from a string containing custom HTML, saving it locally to Jestor. The example below is a trigger creates a PDF file and updates a record, adding the file to a files field.

tableName = 'table'
files = jestor.file(tableName)

html = '''
<body>
    <div class="container">
        <div class="card" id="makepdf"> 
            <h2>Welcome to Jestor</h2>
            <ul>
                <li><h4>We are going to generate a pdf from the area inside the box</h4></li>
                <li><h4>This is an example of generating pdf from HTML during runtime</h4></li>
            </ul>
        </div>
        </div>
</body>
''' 

pdf = jestor.generatePDF([html, "A4", "portrait"])
files.add({'name': 'file_test', 'localFile': pdf})

result = jestor.table(tableName).update(afterSave['id_table'],{'name': 'new record', 'attachments': files.toJson()})

Create New File

This function uses two arguments to create a file from some sort of content:

A variable containing the content.
The extension of the file to be created.
The example below will create a .txt file that contains the text "test file content" and put it on an attachment field.

tableName = "table"
recordId = "id_table"
newFile = jestor.createNewFile(["test file content","txt"])

jestor.table(tableName).update(recordData[recordId],{ "files": newFile})

Create New File Base64

This function uses two arguments to create a file from some sort of content:

A variable containing the content base in 64 format.
The extension of the file to be created.
The example below will create a .txt file that contains the text "test file content" and put it on an attachment field.

tableName = "table"
recordId = "id_table"
newFile = jestor.createNewFileFromBase64(["dGVzdCBmaWxlIGNvbnRlbnQ=","txt"])

jestor.table(tableName).update(recordData[recordId],{ "files": newFile})

Get Content File

This function gets the content from a file inside Jestor itself. The example below will download the .jpg file from the picture field we passed as an argument and return in base64 format.

base64Content = jestor.getContentFile([currentData["pictures"]])

Download File

This function downloads a file from a given URL. The example below would attempt to download the .jpg file from the fake URL we passed as an argument and return in base64 format.

base64Content = jestor.downloadFile(["https://example.com/file.jpg"])
newFile = jestor.createNewFileFromBase64([_downloadFile,"jpg"])

tableName = "table"
recordId = "id_table"
newFile = jestor.createNewFile(["dGVzdCBmaWxlIGNvbnRlbnQ=","txt"])

jestor.table(tableName).update(recordData[recordId],{ "files": newFile})

Example of using Low-code (Python)