Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.

Moderator: amosbatto

Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
By Carlcab
#794253
I am trying to generate an output document using the Rest API and AJAX on a button click but when i run the below code I get an AJAX error

"ERROR!! {"error":{"code":400,"message":"Bad Request: This output document with app_doc_id: 4746131735758b0b92b3fb1043607486 does not exist!"}}"

I got all the information for the output document through the getOutputDocuments Rest API. The doc_uid below is the same as the output document that I am trying to update.

app_doc_uid 69949576459830ffeb1ae96053632593
doc_uid 4746131735758b0b92b3fb1043607486
app_doc_type OUTPUT PDF
app_doc_index 3

Here is the AJAX code I have created to call the Generate Output Document Rest API:
Code: Select all
function generateDocument() {
var host = PMDynaform.getHostName();              // get the hostname
var ws = PMDynaform.getWorkspaceName();           // get the current workspace
var token = PMDynaform.getAccessToken();          // get the access Token
var app_uid = $("#APP_UID").getValue();
var out_doc_uid = "4746131735758b0b92b3fb1043607486";
var delIndex = "3";
  $.ajax({
        url: "/api/1.0/"+ws+"/cases/"+app_uid+"/"+delIndex+"/output-document/"+out_doc_uid,        // endpoint URL
        data: {},
        type: "POST",
        beforeSend: function(xhr){xhr.setRequestHeader("Authorization", "Bearer "+token);},       // Header with the access token
        success: function(xhr, status, error){
          alert("SUCCESS!!"+xhr.responseText);
        },
    	error: function(xhr, status, error){
  			alert("ERROR!!"+xhr.responseText);
		}
        });
  return true;
}

$("#Submit").find("button").click(generateDocument);
Any help is much appreciated.
By amosbatto
#794372
Are you sure that "4746131735758b0b92b3fb1043607486" is the ID of the Output Document? It should be the number that you see when you click on the "UID" button in the list of Output Documents in the process designer.
By Carlcab
#794450
The UID for the output document was correct but now I have found that I don't have permissions to generate the output document.
I don't see anywhere in the documentation where it says what permissions I need to generate output documents. Does the case need to be accessed from the users inbox to generate output documents?
By amosbatto
#794471
This might be caused by the new security restriction in PM 3.1.3 and later on downloading files.
Also, remember that this code will not work if you aren't running a case, so it won't work when in the DynaForm designer. I notice that your delegation index number is fixed in your code, which might cause problems if you reassign the case, which will increment the delegation index. It is better to pass the delegation index in a hidden field to the DynaForm and then use that in the JavaScript:

If those things aren't the problem, then probably the logged-in user needs to be assigned to the task in the case where generating the output Document. Looking through the code, however, I don't see this restriction in the source code, but I only looked two levels deep and it might be in one of the functions which is called.
By Carlcab
#815119
I finally figured out how to generate an output document when the case is not in the users inbox.

In processmaker\workflow\engine\src\ProcessMaker\Services\Api\Cases\OutputDocument.php
edit the below function:
Code: Select all
    public function doPostOutputDocument($app_uid, $del_index, $out_doc_uid)
    {
        try {
            /* $userUid = $this->getUserId(); */
            $userUid = 'Set to a user ID that has supervisor permission for that process';
            $case = new \ProcessMaker\BusinessModel\Cases();
            $outputDocument = new \ProcessMaker\BusinessModel\Cases\OutputDocument();
            /* $outputDocument->throwExceptionIfCaseNotIsInInbox($app_uid, $del_index, $userUid);
            $outputDocument->throwExceptionIfOuputDocumentNotExistsInSteps($app_uid, $del_index, $out_doc_uid); */
            $response = $outputDocument->addCasesOutputDocument($app_uid, $out_doc_uid, $userUid);

            //Return
            return $response;
        } catch (\Exception $e) {
            throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage());
        }
    }
In processmaker\workflow\engine\src\ProcessMaker\BusinessModel\Cases\OutputDocument.php
comment out line 447
Code: Select all
//$oCase->thisIsTheCurrentUser( $sApplication, $index, $sUserLogged, '', 'casesListExtJs' );
Then the rest api will be able to create the output document regardless of the user's permissions.
By amosbatto
#815138
Good find. I'll add a version of this endpoint to my extraRest plugin, which allows either the assigned user to the case or Supervisors assigned to the process to generate output documents.
By amosbatto
#815161
It should work on the mobile app, but I haven't tested all the endpoints on the mobile app. Let me know if you find any problems.
By Carlcab
#815170
I am trying to add a custom rest endpoint to your plugin but I cant access the post parameters that I am passing. I can return the $app_uid successfully but the $data parameter is always empty.
I don't know if I have created the annotations correctly or if i'm not creating the data correctly on the Jquery side. Can you please point out where i am going wrong.

Jquery Ajax
Code: Select all
var arrayTest = {
    data: ["test1"]
};
var data = JSON.stringify(arrayTest);

$.ajaxSetup({
    beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'Bearer ' + token); }// Header with the access token
});
$.ajax({
    url: "/api/1.0/" + ws + "/extrarest/notification/" + $("#APPLICATION").getValue() + "/cico/",
    data: {data},
    type: "POST"
}).done(function (response) {
    console.log(response);
}).fail(function (xhr, status, error) {
    console.log("ERROR!! <br />" + xhr.responseText);
});
PHP custom rest api
Code: Select all
/**
* Send email notification when a new CICO task is selected as required
* 
* @url POST /notification/:app_uid/cico
* @access protected
* 
* @param string  $app_uid         Case's unique ID. {@from path}{@min 32}{@max 32}
* @param string  $data                 JSON string. {@from body}
* 
* @return string
*/
public function emailReminderCICO($app_uid, $data) {
    try {
        return $data;
    } catch (\Exception $e) {
        throw (new RestException(Api:: STAT_APP_EXCEPTION, $e -> getMessage()));
    }
}
By amosbatto
#815205
Glad to hear that you got it working. I updated the documentation last Friday on creating custom REST endpoints to better explain this:
http://wiki.processmaker.com/3.0/Creati ... _Endpoints

By the way the shared/sites/{workspace}/routes.php file will be overwritten every time you disable and reenable the plugin, so it is better to write your changes to the endpoint in the extraRest/Extra.php file.
Can I connect to firebird database?

If you want to connect the database remotely using[…]

Task with multiple users

Well i have worked for days on this project and th[…]

Multilple Flow Possibility

Hi Is it possible to have multiple flow in a sing[…]

Hi, Please tell to processmaker team for tran[…]