/
Event Management (POC)

Event Management (POC)

Search the IFS Workflow Wiki
 

Back to Best Practices  



High Level Approach

This will be a workflow that will run every week (or any other interval) and, using Enterprise Upstream’s public API, will grab the data in the Workflow Staging Table in EU. New records are staged via a database trigger that fires after certain events, generally changes in data. These new records get extracted into the workflow, processed and then the workflow determines which sub-process (another workflow) to start based on a type code. Multiple records will begin multiple workflows.

Check for Events

To obtain events from EU, use the workflowStaging endpoint from EU’s public API and store the response into an array variable.

1. $resource_url = @@apiUrl. 2. '/api/financial/v1/workflowStaging'; 3. $curl = curl_init($resource_url); 4. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 5. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); 6. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.@@apiToken, 'P2-Context: '.@@p2Context)); 7. $response = curl_exec($curl); 8. $httpResponse = curl_getinfo($curl, CURLINFO_HTTP_CODE); 9. curl_close($curl); 10. $contentsUtf8 = utf8_encode($response); 11. $responseArray = json_decode($contentsUtf8, true); 12. $events = $responseArray['_embedded']['workflowStagings'];

eventGrid will hold each event and its parsed variables. EventExists is used to determine whether there is an event(s) to process. EventIndex and EventCount are used together to determine if all events have finished processing (if eventCount == eventIndex – 1 all events have processed). Iterate through each event and check that it is a new event; no need to re-process events. To remove any whitespace, recreate the array by taking the keys and values of the array and calling trim on each. Afterwards, array_combine will take the keys and values and recreate the array.

13. @=eventGrid = array(); 14. @@eventExists = false; 15. @@eventIndex = 1; 16. @@eventCount = 0; 17. if ($httpResponse == 200 || $httpResponse == 201) { 18. $i = @@eventIndex; 19. foreach($events as $event) { 20. if ($event['status'] == 'T') { 21. $variableResponse = json_decode($event['workflowValues'], true); 22. $variables = $variableResponse['variables'][0]; 23. $keys = array(); 24. $values = array(); 25. foreach($variables as $key => $val) { 26 array_push($keys, trim($key)); 27. array_push($values, trim($val)); 28. } 29. $variables = array_combine($keys, $values);

To dynamically determine which variable needs to be stored, look at the current key and store the variable accordingly. This will need to be updated if a new sub-process is added. After the proper variables are stored, store everything for that event into a grid (or array variable).

30. for ($j = 0; $j < count($keys); $j++) { 31. $curKey = $keys[$j]; 32. if ($curKey == 'wellUserKey') { 33. $wellUserKey = $variables[$curKey]; 34. } else if ($curKey == 'codeType') { 35. $codeType = $variables[$curKey]; 36. } else if ($curKey == 'codeValue') { 37. $codeValue = $variables[$curKey]; 38. } 39. } 40. @=eventGrid[$i] = array('eventGridType' => $event['workflowType'], 'eventGridStatus' => $event['status'], 'eventGridMessage' => $event['statusMessage'], 'wellUserKey' => $wellUserKey, 'codeType' => $codeType, 'codeValue' => $codeValue); 41. $i++; 42. @@eventCount++; 43. @@eventExists = true; 44. } 45. } 46. } else { 47. //error messaging for bad http status 48. }

Kick off Sub-Process

If eventExists is true, the next step is to determine which event type it is and kick off the proper sub-process. First, all variables that were stored in eventGrid must be stored in their own variable (This is because @=eventGrid[@@eventIndex]['eventGridType'] cannot be sent directly to the sub-process, only single variables can be). Take this time to also increment the event index to keep track of how many events have been processed. Then on each sub-process, determine which variables to pass. Variables that aren’t passed won’t be used

1. @@eventType = @=eventGrid[@@eventIndex]['eventGridType']; 2. @@wellUserKey = @=eventGrid[@@eventIndex]['wellUserKey']; 3. @@codeType = @=eventGrid[@@eventIndex]['codeType']; 4. @@codeValue = @=eventGrid[@@eventIndex]['codeValue']; 5. @@eventIndex++;

NOTE: Sub-Processes MUST be set to “Asynchronous”. This allows the sub-process to be created while the event management case still checks for more events. Otherwise, you must complete the newly created case (sub-process) before more events can be processed.

Finally, once all events have been processed (when eventCount == eventIndex – 1), update the staging table.

1. $resource_url = @@apiUrl. 2. '/api/financial/v1/workflow'; 3. $curl = curl_init($resource_url); 4. $curl_post_data = array(); 5. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 6. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); 7. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($curl_post_data, true)); 8. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.@@apiToken, 'P2-Context: '.@@p2Context)); 9. $response = curl_exec($curl); 10. $httpResponse = curl_getinfo($curl, CURLINFO_HTTP_CODE); 11. curl_close($curl); 12. if ($httpResponse == 200 || $httpResponse == 201) { 13. $msg = 'New cases have been created.'; 14. $g = new G(); 15. $g - > SendMessageText($msg, 'INFO'); 16. } else { 17. $msg = 'There were errors updating the staging table.'; 18. $g = new G(); 19. $g - > SendMessageText($msg, 'ERROR'); 20. }

Configuration steps for implementing additional Sub-Processes

      For each additional sub-process added, all required variables must be added to the following blocks of code. Otherwise, the variables required will not be accessible to pass to the sub-process.

1. for($j = 0; $j < count($keys); $j++) { 2. $curKey = $keys[$j]; 3. if ($curKey == 'wellUserKey') { 4. $wellUserKey = $variables[$curKey]; 5. } else if ($curKey == 'codeType') { 6. $codeType = $variables[$curKey]; 7. } else if ($curKey == 'codeValue') { 8. $codeValue = $variables[$curKey]; 9. } 10. } 1. @@eventType = @=eventGrid[@@eventIndex]['eventGridType']; 2. @@wellUserKey = @=eventGrid[@@eventIndex]['wellUserKey']; 3. @@codeType = @=eventGrid[@@eventIndex]['codeType']; 4. @@codeValue = @=eventGrid[@@eventIndex]['codeValue']; 5. @@eventIndex++;

The event type must also be added to the Check for Events gateway in order to properly route to the new sub-process.

 

7d607c95-21c2-4ff5-a7e8-ee0807d49282.png

Challenges/Potential Limitations

  • Parsing the event variables from the staging table cannot be 100% dynamic. Hardcoding must occur to be able to handle the different possible variables.

  • Since the endpoint returns every record in the staging table, there may be performance issues if this table becomes large enough (1,000,000+ records).

    • Possibly have the endpoint only return those records that are in Transfer.

    • Possibly have an “audit” table so all records can be kept, but the main table can be cleared every so often.

  • Adding new sub-processes requires configuration steps to be taken due to the parsing of the variables not being 100% dynamic.

  • The designer screen will become very large depending on how many sub-processes are being dealt.




Copyright© 2024 IFS AB. Copying prohibited. All rights reserved.