code: void updateWorkflowOutput(final Workflow workflow, @Nullable Task task) {
          List<Task> allTasks = workflow.getTasks();
          if (allTasks.isEmpty()) {
              return;
          }
  
          Task last = Optional.ofNullable(task).orElse(allTasks.get(allTasks.size() - 1));
  
          WorkflowDef workflowDef = workflow.getWorkflowDefinition();
          Map<String, Object> output;
          if (workflowDef.getOutputParameters() != null && !workflowDef.getOutputParameters().isEmpty()) {
              Workflow workflowInstance = populateWorkflowAndTaskData(workflow);
              output = parametersUtils.getTaskInput(workflowDef.getOutputParameters(), workflowInstance, null, null);
          } else if (StringUtils.isNotBlank(last.getExternalOutputPayloadStoragePath())) {
              output = externalPayloadStorageUtils.downloadPayload(last.getExternalOutputPayloadStoragePath());
              Monitors.recordExternalPayloadStorageUsage(last.getTaskDefName(), ExternalPayloadStorage.Operation.READ.toString(), ExternalPayloadStorage.PayloadType.TASK_OUTPUT.toString());
          } else {
              output = last.getOutputData();
          }
  
          workflow.setOutput(output);
          externalPayloadStorageUtils.verifyAndUpload(workflow, ExternalPayloadStorage.PayloadType.WORKFLOW_OUTPUT);
      }
docs: Updates the workflow output.
  
  @param workflow the workflow instance
  @param task     if not null, the output of this task will be copied to workflow output if no output parameters are specified in the workflow defintion
  if null, the output of the last task in the workflow will be copied to workflow output of no output parameters are specified in the workflow definition