Python: Native Methods

Common variables and dictionaries

Before getting to the native methods themselves, there's an important bit of information to be aware of when creating low-code triggers: the usage of currentData, beforeSave and afterSave.

Those three dictionaries hold useful information about information sent by the user and regarding the database itself.

  • currentData: this is the dictionary that contains the information the user has sent by performing an action. 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.

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).

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]', '[email protected]', '1','Test']);
#result will be a python array with the created user's values

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!")