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

Here is the workflow that we are going to create using various language-specific SDKs in code:

user notification workflow


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);