Get workflow input using api

How do I retrieve the input used on a workflow based on workflow id using java api?

I can’t see anything obvious in WorkflowServiceStubs.newInstance().blockingStub()

You would need to get the first event in your workflow history (WorkflowExecutionStarted event) and get the inputs from it. Note the inputs are going to be of type Payload and you would need to use either the default, or if you are using a custom data converter you would need to use it to convert Payload to object type.

Here is full sample:

public static void getWorkflowInputs(WorkflowExecution wfExec) {
        GetWorkflowExecutionHistoryRequest req =
                GetWorkflowExecutionHistoryRequest.newBuilder()
                        .setNamespace(client.getOptions().getNamespace())
                        .setExecution(wfExec)
                        .build();
        GetWorkflowExecutionHistoryResponse res =
                service.blockingStub().getWorkflowExecutionHistory(req);

        HistoryEvent firstHistoryEvent = res.getHistory().getEventsList().get(0);
        Payloads input = firstHistoryEvent.getWorkflowExecutionStartedEventAttributes().getInput();
        for(Payload payload : input.getPayloadsList()) {
            // here you need to know the type, or convert it to object
            // if you use custom data converter use it here instead of default
            // if you wanted to. for sample we know input was string type
            String myInput = DataConverter.getDefaultInstance()
                    .fromPayload(payload, String.class, String.class);
            // ...
        }

You could call this method with for example:

getWorkflowInputs(WorkflowExecution.newBuilder()
                .setWorkflowId("<workflow id>")
                .setRunId("<workflow run id>")
                .build());
1 Like