Page 1 of 1

Total cases using REST

Posted: Thu Mar 26, 2020 7:17 am
by manalidhuri
I want to get total number of created cases using REST service.
How can I get this?

Re: Total cases using REST

Posted: Thu Apr 09, 2020 3:35 pm
by RicardoMG
Hello!
At the moment processmaker can not return all cases, but you can review what you can do REST API Cases - cases in this link: https://wiki.processmaker.com/3.3/REST_API_Cases/Cases
An example similar to what you were asking was made.
First I got the token using Postman, see the attached images.
You must take into account that to obtain the token you first have to Registering an External Application to Use REST, you have to follow this steps: https://wiki.processmaker.com/3.1/OAuth_2.0

Then you will have the client_id and client_secret, use that data with a user registered in processmaker to obtain the token.

Now i am going to perform using a trigger to get the number of cases started in processmaker, this is the code in the trigger:
Code: Select all
$pmRestRequest = function ($method, $pmServer, $endpoint, $aVars = null, $accessToken = null) {
        echo ($pmServer.$endpoint);
        if (empty($accessToken) and isset($_COOKIE['access_token']))
        $accessToken = $_COOKIE['access_token'];
    if (empty($accessToken)) { //if the access token has expired
        //To check if the PM login session has expired: !isset($_COOKIE['PHPSESSID'])
        header("Location: ./../login/login"); //change to match your login method
        die();
    }
    //add beginning / to endpoint if it doesn't exist:
    if (!empty($endpoint) and $endpoint[0] != "/")
        $endpoint = "/" . $endpoint;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $pmServer . $endpoint);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $accessToken));
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $method = strtoupper($method);
    switch ($method) {
        case "GET":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
            break;
        case "DELETE":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            break;
        case "PUT":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        case "POST":
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($aVars));
            break;
        default:
            throw new Exception("Error: Invalid HTTP method '$method' $endpoint");
            return null;
    }
    $ret = curl_exec($ch);
    $oRet = new StdClass;
    $oRet->response = json_decode($ret);
    $oRet->status   = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($oRet->status == 401) { //if session has expired or bad login:
        header("Location: ./../login/login"); //change to match your login method
        die();
    }
    elseif ($oRet->status != 200 and $oRet->status != 201) { //if error
        if ($oRet->response and isset($oRet->response->error)) {
            print "Error in $pmServer:\nCode: {$oRet->response->error->code}\n" .
                "Message: {$oRet->response->error->message}\n";
        }
        else {
            print "Error: HTTP status code: $oRet->status\n";
        }
    }
    return $oRet;
};
$cases=1;
$token="7ee4ff6dcadd345b07349e92c12e087bb849d27d";
$oRet = $pmRestRequest('GET', "http://192.168.0.109:8080",'/api/1.0/workflow/case/start-cases', null, $token);
if ($oRet and $oRet->status == 200) {
    foreach ($oRet->response as $oUser) {
		$cases = $cases + 1;
        print "{$oUser->usr_firstname} {$oUser->usr_lastname} ({$oUser->usr_username})\n";
    }
	@@number = $cases;
}
In the variable @@number obtaining the number of the cases started.

It should be noted that I only get the number of cases initiated by the user with which the token was obtained, in my case the user is "admin"

I hope this information helps you