Start Buisness Process from Human Task using REST API

business-process
rest-api
manual-task

#1

BP creation

Prepare input data:

var campaignUuid = "ff244048-031f-4fc4-82f3-b658d1b571f2"; 
// create csv input data (contains only 1 column) 
var dataToSend = "merged_input_fields_json\n" + "\"" + JSON.stringify(combinedObject).replace(/"/g,"\"\"") + "\"";
var processData = btoa(dataToSend);              //encode to base64 format
var startProcessData = {"campaignUuid":campaignUuid,"mainData": processData};

To get the campaign UUID

Go to Campaigns → open campaign → open “Enter properties” tab, copy UUID value

AJAX call to create and start BP

var startTaskUrl = instanceBaseUrl+"/workfusion/api/v2/workfusion/task/file";
 var startProcessPromise = $.ajax({
   type: "POST",
   url: startTaskUrl,
   contentType: 'application/json; charset=utf-8',
   dataType: 'text',
   async: true,
   data: JSON.stringify(startProcessData)
});
startProcessPromise.then(startBPSuccess, startBPError);

Get ID of the started process

var startBPSuccess = function(id){
  processStatusCheck(id);
};

Check BP execution status

checkStatusUrl = instanceBaseUrl + "/api/v2/workfusion/task/{uuid}/steps";
waitForStatus(checkStatusUrl.replace("{uuid}", uuid), "COMPLETED", retriesCount, processCompletionSuccess, processCompletionFailure);
function waitForStatus(url, status, maxRetries, processCompletionSuccess, processCompletionFailure) {
  var count = 0;
  var restartCheck = setInterval(function() {
    count++;
    if (count <= maxRetries) {
      setProcessCompleteStatus(bpResponseElementId, processInProgressMessage, "blinkText normalResponseStatus");
      $.ajax({
        url: url,
        type: "GET",
        complete: function (jqXHR, textStatus) {
          var finalStepUuid = checkIsTaskCompleted(jqXHR,status);
          if (finalStepUuid) {
            clearInterval(restartCheck);
            processCompletionSuccess(status, finalStepUuid);
          }
        }
      });
    } else {
      clearInterval(restartCheck);
      alert("Exceeded maxRetries value");
    }
  }, checkProcessStatusInterval * 1000);
};
function checkIsTaskCompleted(response, requiredStatus) {
  var finalStepUuid = undefined;
  var responseObj = JSON.parse(response.responseText);
  for(var step in responseObj){
    if(responseObj[step].finalStep == true && responseObj[step].runStatus === requiredStatus){
      finalStepUuid = responseObj[step].runUuid;
    } 
  }
  return finalStepUuid;
};

Get snapshot of the final step

function processCompletionSuccess(status, stepUuid){
   setProcessCompleteStatus(bpResponseElementId, successProcessMessage, sucessProcessClass + " normalResponseStatus");
   retrieveSnapshot(stepUuid);
};

function retrieveSnapshot(processUuid) {
   var startProcessPromise = $.ajax({
     type: "GET",
     url: retriveSnapshotUrl.replace("{uuid}", processUuid),
     contentType: 'text',
     headers: {'Authorization': 'Basic ' + btoa(userId + ":" + pwd)},
     dataType: 'text',
     cache:false,
     async: true
   });
 startProcessPromise.then(snapshotRetrieveSuccess, snapshotRetrieveError);
};

function snapshotRetrieveSuccess (response){
   var decodedResponseString = atob(JSON.parse(response).content);
};