Thanks to visit codestin.com
Credit goes to orkes.io

Skip to main content

Write Workflows Using Code

You can write workflows in code to create dynamic workflows that can't be predefined. Support for defining workflows in code is part of all the supported SDKs.

Create workflows in code

The workflow in the following examples performs these steps:

  1. Get user details: Retrieves the user information based on the userId provided as the workflow input.
  2. Determine notification type: Uses a Switch task to check the preferred notification method from the input and determine how to notify the user.
  3. Send email: If the notification preference is email, send a message to the user’s email address.
  4. Send SMS: If the preference is SMS, send a text message to the phone number.

user notification workflow

Select your preferred language and use the code example to define and register the workflow in your Conductor environment.


ConductorWorkflow<WorkflowInput> workflow = new ConductorWorkflow<>(executor);
workflow.setName("user_notification");
workflow.setVersion(1);

SimpleTask getUserDetails = new SimpleTask("get_user_info", "get_user_info");
getUserDetails.input("userId", "${workflow.input.userId}");

SimpleTask sendEmail = new SimpleTask("send_email", "send_email");
// get user details user info, which contains the email field
sendEmail.input("email", "${get_user_info.output.email}");

SimpleTask sendSMS = new SimpleTask("send_sms", "send_sms");
// get user details user info, which contains the phone Number field
sendSMS.input("phoneNumber", "${get_user_info.output.phoneNumber}");

Switch emailOrSMS = new Switch("emailorsms", "${workflow.input.notificationPref}")
.switchCase(WorkflowInput.NotificationPreference.EMAIL.name(), sendEmail)
.switchCase(WorkflowInput.NotificationPreference.SMS.name(), sendSMS);

workflow.add(getUserDetails);
workflow.add(emailOrSMS);

//Execute the workflow and get the output
WorkflowInput input = new WorkflowInput("userA");
input.setNotificationPref(WorkflowInput.NotificationPreference.EMAIL);
CompletableFuture<Workflow> workflowExecution = simpleWorkflow.executeDynamic(input);
Workflow workflowRun = workflowExecution.get(10, TimeUnit.SECONDS);