Native Methods

Specific functions to help you automate.

Contextual functions for your jestor

There's a lot you can do by programming in jestor, but there are also some things that you aren't able to code through pure PHP. For example, you won't have direct access to the database itself. However, jestor has native methods that allow you to fetch, create and update information on your jestor's database. They're functions specifically designed to help you manipulate your jestor's data however you want to.

📘

Understanding functions

Though the functions below are very easy to understand, it helps to have a knowledge of how functions work in general. Here's a link if you want to read more about it.

Functions: https://www.php.net/manual/en/language.functions.php

Jestor.create

This function uses two arguments to create a record on a table:

  • A string reflecting the API/trigger name of the table in which the record will be created.
  • An array pairing fields and values of the record to be created.

The example below depicts the creation of a record on the Deals table.

$newdeal = array(
    "name" => "Deal #0001",
    "amount" => 500,
    "status" => "In Progress"
);
Jestor.create("deals",$newdeal);

When creating a record, you can also attribute all the information of the newly created record to a variable. This is useful when you want to create a record and have its id for other part of the automation.

$newdeal = array(
    "name" => "Deal #0001",
    "amount" => 500,
    "status" => "In Progress"
);
$createddeal = Jestor.create("deals",$newdeal);

In the case above, if I wanted the id of the new record, it would be available at $createddeal["id_deals"];

Jestor.error

This function stops the automation and reverts every affected record to the state before the user's input, showing them an error message. The message to be displayed should be a string passed as an argument of the function.

In the example below, we're checking if the record the user's creating is empty on the field "amount" and applying Jestor.error to TRUE cases.

if(empty($objectNew["amount"])){
    Jestor.error("Insert an amount for the purchase!");
}

For the user, it'll look something like this:

Error when creating purchases without Amount.Error when creating purchases without Amount.

Error when creating purchases without Amount.

Jestor.update

This function uses two arguments to update a record on a table:

  • A string reflecting the API/trigger name of the table containing the record to be updated.
  • An array pairing fields and values of the record to be update.

🚧

Regarding the second argument

The array must contain an id => value pairing, as the function needs the id of the record to be updated. So if I want to update Andrew's phone number and Andrew is record of id 3 on the Employees table, the array must contain the "id_employees" => 3 pairing along with the field/value pairings to be updated.

If I tried to update a vendor's email, for example, I would do something like this:

$vendorupdate = array(
  "id_vendors" => 1,
    "email" => "[email protected]",
);
Jestor.update("vendors",$vendorupdate);

Jestor.loadData

This function uses two arguments to fetch records on a table:

  • A string reflecting the API/trigger name of the table where the search will be performed.
  • An array containing the search's filters and conditions.

The second argument's array can have different parameters, such as:

where: an array containing the conditions a record must fulfill. For example, the Jestor.loadData below will search on the Plans table for records that have the "status" field's value as "Active".

$conditions = array(
  "status" => "Active",
);
$search = Jestor.loadData("plans",["where" => $conditions]);

You can pass more than one condition (which will then work with an AND logic). Also, you don't have to always work with exact matches. You can pass operators along with the field (such as !=, >, < and others).

The example below will search for records that have Status equals Active AND have Price over 50.

$conditions = array(
  "status" => "Active",
  "price >" => 50
);
$search = Jestor.loadData("plans",["where" => $conditions]);

sort: the order in which the search's results will be presented. By default, a search will bring the results by ascending order of creation, but by using a "sort" parameter you can bring the results by ascending (asc) or descending (desc) order of any other field.

$search = Jestor.loadData("plans",["sort" => "price asc"]);

limit: a maximum amount of results the search will return. For example, it's possible to limit the number of results to the first 10 results, which can make searches quicker.

$search = Jestor.loadData("plans",["limit" => 10]);

offset: skips the first n results of your search, which can be useful when doing searches in batches. In the example below, we skip the first 50 results of the search.

$search = Jestor.loadData("plans",["offset" => 50]);

You can, of course, send a mix of where, sort, limit and offset.

$conditions = array(
  "status" => "Active",
  "price >" => 50
);
$search = Jestor.loadData("plans",["where" => $conditions,"sort" => "price asc", "limit" => 10, "offset" => 50]);

An important thing to note is that a Jestor.loadData can fetch more than one record. Because of this, the variable that receives the search results (in the case above, $search) is actually an array in which each position is an entire record. So, for example, $search[0] is actually another array containing all the information of the first record of the search, $search[1] is an array containing the information of the second record and so on.

Jestor.remove

This function uses two arguments to remove records on a table:

  • A string reflecting the API/trigger name of the table where the search will be performed.
  • The id of the record to be deleted.

The example below deletes the record on the table Plans with id_plans = 3.

Jestor.remove('plans',3);

Jestor.createNewFile

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 "Hello world!" and put it on an attachment field.

$text = "Hello world!";
$file = Jestor.createNewFile($text,"txt");
$objectNew["attachment"] = $file;

Jestor.downloadFile

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.

$downloadedFile = Jestor.downloadFile("https://example.com/file.jpg");

Do note that you cannot pass the content downloaded directly onto an attachment or picture field. You would have to use Jestor.createNewFile() to create a file first and then save it to the field, such as the example below.

$downloadedFile = Jestor.downloadFile("https://example.com/file.jpg");
$file = Jestor.createNewFile($downloadedFile,"jpg");
$objectNew["picture"] = $file;

Jestor.getContentFile

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.

$contentFile = Jestor.getContentFile($objectNew["picture"]);

Just like Jestor.downloadFile(), you cannot pass the content directly onto an attachment or picture field. You would have to use Jestor.createNewFile() to create a file first and then save it to the field, such as the example below.

$contentFile = Jestor.getContentFile($objectNew["picture"]);
$file = Jestor.createNewFile($contentFile,"jpg");
$objectNew["picture"] = $file;

One thing important about files inside Jestor is that, if all you're doing is passing a file from a field to the other, just copying it will work with no need of using Jestor.getContentFile(). This function is usually used when you need to treat the file, such as encoding it as base64 to show it on a customized page.

#this will work if all you're trying to do is copy a file to a different field

$search = Jestor.loadData("docs",["where" => ["id_docs" => 1]]);
$objectNew["attachment"] = $search[0]["file"];

#but you'll need to do this if you want to treat the file content somehow. In this case, encoding as base64

$contentFile = Jestor.getContentFile($objectNew["picture");
$file64 = base64_encode($contentFile);

Jestor.curlCall

This method makes a cURL call, which can be used to fetch or send information to API endpoints or webhooks. This function needs two arguments: a URL and an array containing the call parameters (including data).

The example code below creates an array with data, encodes it and sends it to the specified URL. The response is then stored in the variable $response.

$url = "https://example-url.com";

$data = array(

"name" => "Example",
"amount" => 1000

);

$payload = json_encode($data);

$response  = Jestor.curlCall($url,array(

CURLOPT_HTTPHEADER => array(

'Content-Type: application/json'

),

CURLOPT_POST => true,

CURLOPT_POSTFIELDS,

CURLOPT_POSTFIELDS => $payload

));

This method can even be used to send API calls to your own Jestor (though it's usually better and easier to use Jestor's native methods when dealing with data inside your own org). The example code below will list return data from all Invoices where amount equals 500 or 1000.

$url = "https://yourorg.api.jestor.com/object/list";

$data = array(

"object_type" => "invoices",
"sort" =>  "amount asc",
"page": 1,
"size": "100",
"filters" => array(array(
    "field" => "amount",
  "operator" => "in",
  "value" => array(
        500,
    1000)))
  
);

$payload = json_encode($data);

$response  = Jestor.curlCall($url,array(

CURLOPT_HTTPHEADER => array(

'Content-Type: application/json',
'Authorization: Bearer your_token'

),

CURLOPT_POST => true,

CURLOPT_POSTFIELDS,

CURLOPT_POSTFIELDS => $payload

));

What’s Next