Environment Variable and Magic Variable Syntax, Usage, SDK, and Script Examples

Reference Environment Variables and Magic Variables in your Scripts.

Environment Variable Syntax, Usage, SDK, and Examples

‌ProcessMaker Platform uses two global variables that Scripts can call. Variable usage depends on the programming language that the Script uses. Below is a description of these global variables:‌

  • Data: The data variable is a JSON object that contains all Request data to the moment a Script runs.

  • Config: The config variable is a JSON object that contains any special configuration to be passed to the Script prior to it running. In Script Task elements of a Process model, special configurations are entered into the Script Configuration setting. See Reference a Request Variable from a Script Configuration Setting as to the best practice when configuring Scripts from Script Task elements in a Process model.

Every Script Executor from which a Script runs has the following default Environment Variables from which a Script may get its value. Refer to the tabs below how to get these Environment Variable values for each supported programming language. Below is a description of these default Environment Variables.

Environment Variable

Description

HOST_URL

Domain for the ProcessMaker Platform instance.

API_HOST

API_TOKEN

‌Refer to the tabs below how to use variables in supported programming languages.

Below is a sample Script that uses PHP. Refer to the comments denoted with // that describe how the sample functions:

  • How to get an Environment Variable.

  • How to get a value from the configuration object.

  • How to get a value from a data object.

  • Call the Software Development Kit (SDK).

<?php
$output = [];
// Get a ProcessMaker Platform Environment Variable, in this case TEST_VAR.
$output['envVar'] = getenv('TEST_VAR');
// Get a value from the config object.
// In this example, 'test' in the JSON config: {"test":"test config value"}
$output['configTest'] = $config["test"];
// Get a value from the data object.
// In this example, the user_id for the _request.
$output['requestUserId'] = $data['_request']['user_id'];
// Get the email address for user id 1 using the API/SDK.
// Use the global `$api_config` to set credentials automatically.
$usersApi = new ProcessMaker\Client\Api\UsersApi(null, $api_config);
$user = $usersApi->getUserById("1");
$output['userEmail'] = $user->getEmail();
return $output;

Retrieve All Tasks for a User

The following sample PHP script provides an example to get all Tasks currently assigned to a user. This example also demonstrates the use of optional arguments such as the Request ID or a Task filter.

// Sample PHP script to get all current Tasks for a user
<?php
$apiInstance = $api->tasks();
$tasks = [];

// Returns all Tasks that the user has access to
$result = $apiInstance->getTasks();
foreach ($result->getData() as $task) {
    $tasks[] = [
        'id' => $task->getId(),
        'name' => $task->getElementName(),
        'processRequestId' => $task->getProcessRequestId(),
        'status' => $task->getStatus(),
        'userId' => $task->getUserId(),
    ];
}

// Optional arguments
$process_request_id = 5; // int | Process request_id
$filter = 'Form Task'; // string | filter by Taskid, node name, or Request data
$order_by = 'id'; // string | Field to order results by
$order_direction = 'asc'; // string | 
$include = 'process,user'; // string | Include data from related models in payload. Comma separated list.

$result = $apiInstance->getTasks($process_request_id, $filter, $order_by, $order_direction, $include);
foreach ($result->getData() as $task) {
    $tasks[] = [
        'id' => $task->getId(),
        'name' => $task->getElementName(),
        'status' => $task->getStatus(),
        'userEmail' => $task->getUser()['email'],
        'processName' => $task->getProcess()['name']
    ];
}

return ['tasks' => $tasks];

Get a Task using the Task ID

The following sample PHP script provides an example to retrieve a single Task using its Task ID.

// Sample PHP script to get the Task for a certain TaskID
<?php
$apiInstance = $api->tasks();
$taskId = 15;
$task = $apiInstance->getTasksById($taskId);

return [
    'task' => [
        'name' => $task->getElementName(),
        'status' => $task->getStatus(),
        'userId' => $task->getUserId()
    ]
];

Complete a Task

The following sample PHP script provides an example of completing a Task when the Task ID is known.

// Sample PHP Script to complete a Task
<?php
$apiInstance = $api->tasks();
$taskId = 15;
$task = $apiInstance->getTasksById($taskId);

$process_request_token_editable = new \ProcessMaker\Client\Model\ProcessRequestTokenEditable();
$process_request_token_editable->setStatus('COMPLETED');
$process_request_token_editable->setData(['addToRequestData' => 'a value']);

$result = $apiInstance->updateTask($taskId, $process_request_token_editable);

// If there are no error, then the Task was successfully completed
return ['success' => true];
return ['tasks' => $tasks];

© Copyright 2000-2024 ProcessMaker Inc. All rights reserved.