phpprocessmakerprocessmaker-api

Tutorial on how to develop and debug PHP scripts in Processmaker tasks


I am trying to develop a workflow in Processmaker 4 community that require some scripted tasks to be executed but I am facing in issues in things that should be simple such as:

  1. How to debug the code in Processmaker scripts. Is there access to stdout and sterr somehow?
  2. How to access request variables form the PHP script.
  3. How to access the files that have been uploaded to a process from the PHP script.

Does anyone know of a good resource explaining in detail how to create PHP scripts in Processmaker?

So far I have found the following documentation https://github.com/ProcessMaker/docker-executor-php/tree/develop/docs/sdk which certainly seems to open the door on how to access files and Processmaker information but I am still struggling trying to find a way to properly debug fairly complex scripts.


Solution

  • You can debug your script using the Script Editor (at /designer/scripts edit your script) by inserting echo statements at within the code. These statements will be displayed in the "Output" panel, helping you track the script's progress and identify any issues. Additionally, you can access request variables using the $data variable. For testing purposes, you can enter sample values in the "Sample Input" panel and observe how the script handles them.

    Please consider that writing to STDOUT in a script task will result in a runtime exception, since it is not permitted. The use of STDOUT is intended solely for debugging purposes. Exercise caution while employing it to avoid unexpected errors during script execution.

    The following is an simple example:

    <?php
    // Adding two numbers with debugging code
    
    // Define two numbers to add
    $num1 = $data['num1'];
    $num2 = $data['num2'];
    
    // Debug code to display the initial numbers
    echo "Initial numbers: ";
    echo "Number 1: " . $num1 . " ";
    echo "Number 2: " . $num2 . "  ";
    
    // Function to add two numbers and return the result
    function addNumbers($a, $b) {
        $sum = $a + $b;
    
        // Debug code to display the sum of the numbers
        echo "Inside the function: ";
        echo "Sum: " . $sum . "  ";
    
        return $sum;
    }
    
    // Call the function and store the result
    $result = addNumbers($num1, $num2);
    
    // Debug code to display the final result
    echo "Final result: ";
    echo "Sum of " . $num1 . " and " . $num2 . " is: " . $result . " ";
    
    return [
        'result' => $result,
    ];
    

    For this Sample Input:

    enter image description here

    Will return the following output:

    enter image description here