Skip to main content

Write Workflows Using Code

Workflows can be written in code, allowing the creation of dynamic workflows that can't be pre-defined. Support for defining workflows using code is part of all the supported SDKs.

Creating 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

Use the code example for your preferred language 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);