Skip to content

Workflow input

Gurmit Teotia edited this page Dec 10, 2019 · 12 revisions

When starting a workflow you have the option to provide the workflow input as primitive type - int, float,bool... or as complex type in JSON format. In workflow there are three easy ways to access the workflow input:

  1. Use Workflow.Input property: Workflow.Input returns dynamic object. You can use it to access both primitive and complex workflow input. Complex workflow input must be in JSON format.

    var domain = new Domain("test", RegionEndpoint.EUWest1);
    
    //following API will start the workflow with complex input
    var startRequest = WorkflowStartRequest.For<BookingWorkflow>("id");
    startRequest.Input = new {BookFlight = true, BookHotel = false};
    await domain.StartWorkflowAsync(startRequest);
    
    //You can access and use workflow input as show below
    [WorkflowDescription("1.0")]
    public class BookingWorkflow : Workflow
    {
     public BookingWorkflow()
     {
       ScheduleActivity<BookFlight>().When(_=>Input.BookFlight);
       ....
          
       ScheduleActivity<BookHotel>().When(_=>Input.BookHotel);
       ....
     }
    }
  2. Use Workflow.InputAs method: Returns the deserialized custom object from workflow input. It support deserialization into primitive and custom type. For custom input, workflow must be started with JSON format input. Following example clarify it further

    var domain = new Domain("test", RegionEndpoint.EUWest1);
    
    //following API will start the workflow with complex input
    var startRequest = WorkflowStartRequest.For<BookingWorkflow>("id");
    startRequest.Input = new BookingInput{BookFlight = true, BookHotel = false};
    await domain.StartWorkflowAsync(startRequest);
    
    //You can access and use workflow input as show below
    [WorkflowDescription("1.0")]
    public class BookingWorkflow : Workflow
    {
     public BookingWorkflow()
     {
       ScheduleActivity<BookFlight>().When(_=>InputAs<BookingInput>.BookFlight);
       ....
          
       ScheduleActivity<BookHotel>().When(_=>InputAs<BookingInput>.BookHotel);
       ....
     }
    }
  3. Use raw format input: You can access raw form input by directly accessing the WorkflowStartedEvent as shown in below example.

    [WorkflowDescription("1.0")]
    public class CustomWorkflow : Workflow
    {
       
     private string RawInput => StartedEvent.Input;
    }

You can also replace default Workflow.Input property with your property to return the custom object as shown in following example:

[WorkflowDescription("1.0")]
public class BookingWorkflow : Workflow
{
 public BookingWorkflow()
 {
   ScheduleActivity<BookFlight>().When(_=>Input.BookFlight);
   ....
      
   ScheduleActivity<BookHotel>().When(_=>Input.BookHotel);
   ....
 }
 
 protected new BookingInput Input => InputAs<BookingInput>();

}

Clone this wiki locally