Ask questions about installing and configuring ProcessMaker 3
By fibo2358
#814862
Hello,

I would like to import the cases from a flat file (to create new cases in a process based on a csv data).
Could you please tell me what is the recommended way to do it?

Best Regards,
fibo2358
User avatar
By amosbatto
#814929
You can use an external script that calls the newCaseImpersonate() web service or the REST POST /cases/impersonate endpoint to create the new cases in the CSV file.

Let's say that you have the following CSV file named "newCases.csv" to start two new cases:
Code: Select all
Process UID, Task UID, User UID, clientName, contractAmount, contractStartDate
8258361815b303b807b2c38017194441, 8386840825b303b8235d666042551392, 00000000000000000000000000000001, "Acme Inc.", 3480.50, 2018-06-27
8258361815b303b807b2c38017194441, 8386840825b303b8235d666042551392, 00000000000000000000000000000001, "EZ Accounting", 2099.99, 2018-07-15
Where the first line contains the column headers and the second and third lines are new cases to create and "clientName", "contactAmount" and "contractStartDate" are variables to set in the new case.

Note: If you don't want to use unique IDs in your CSV file, then you can use the titles of processes and tasks and the username of the user, but that will mean using the processList(), taskList() and userList() web services in your script to look up the IDs.

Then, create the following PHP script in a file named "createCasesFromFile.php":
Code: Select all
<?php
ini_set('soap.wsdl_cache_enabled', '0');
ini_set('error_reporting', E_ALL);
ini_set('display_errors', True);

//set to URL where the ProcessMaker web services are located:
$wsUrl      = 'http://localhost:321/sysworkflow/en/neoclassic/services/wsdl2';
$wsUsername = 'admin';  //set to the username that will login
$wsPassword = 'admin';  //set to that user's password

//structure for sending variables to a case:
class variableStruct {
   public $name;
   public $value;
}

// Check if CSV file of users was passed as a parameter to this script
if ($argc < 2) {
   die("Error: Must specify a CSV file of cases to create.\n");
}

$fCsv = fopen($argv[1], "r") or 
   die("Error: Unable to open file '{$argv[1]}'.\n");
   
//Login to web services
$client = new SoapClient($wsUrl);
$params = array(array('userid'=>$wsUsername, 'password'=>$wsPassword));
$result = $client->__SoapCall('login', $params);

if ($result->status_code == 0) {
   $sessionId = $result->message;
}
else {
   die("Unable to connect to ProcessMaker.\nError Number: $result->status_code\n" .
      "Error Message: $result->message\n");   
}

$caseCount = $errorCount = $nRow = 0;
$requiredFields = 3;
$aVarNames = array();

while (($aFields = fgetcsv($fCsv, 0, ",")) !== FALSE) {
   $nRow++;
   
   if ($nRow == 1) {
      $aVarNames = count($aFields) <= $requiredFields ? array() : array_slice($aFields, $requiredFields);
      continue;
   }
   elseif (count($aFields) < $requiredFields) {
      print "\nLine $nRow: Skipping line because it doesn't have at least $requiredFields fields.\n";
      $errorCount++;
      continue;
   }
   
   $processId = trim($aFields[0]);
   $taskId    = trim($aFields[1]);
   $userId    = trim($aFields[2]);
   $aCaseVars = array();
   
   for ($i = 0; $i < count($aVarNames); $i++) {
      if (isset($aFields[$requiredFields + $i])) {
         $oVar = new variableStruct();
         $oVar->name  = trim($aVarNames[$i]);
         $oVar->value = trim($aFields[$requiredFields + $i]);
         $aCaseVars[] = $oVar;
      }
   }
   
   $params = array(array(
      'sessionId' => $sessionId, 
      'processId' => $processId,
      'taskId'    => $taskId,
      'userId'    => $userId,
      'variables' => $aCaseVars
   ));
 
   $result = $client->__SoapCall('newCaseImpersonate', $params);
 
   if ($result->status_code == 0) {
      print "Line $nRow: new case ID: $result->caseId, case number: $result->caseNumber \n";
      $caseCount++;
   } else {
      print "\nLine $nRow: error creating case: $result->message \n";
      $errorCount++;
   }   
}      

print "\n$caseCount new cases were created. $errorCount lines in file contained errors.\n";

?>
Then, the above script can be executed from the command line like this:
php createCasesFromFile newCases.csv

A 1xbet clone script is a pre-designed software so[…]

4rabet clone script is enabling entrepreneurs to e[…]

Parimatch clone script is enabling entrepreneurs t[…]

In the world of cryptocurrency, a wallet is an app[…]