3

Could someone please explain how Fork from Elsa Workflow works?

I have set up HTTP Endpoint. After that I send HTTP Request and I get HTTP Response in JSON format. I want to set condition on that response (for example name='John') so that I could display only that row from database.

What Elsa activity should I use? Is Fork activity correct choice or other?

NikolaCSI
  • 31
  • 3

1 Answers1

5

Fork

The Fork activity simply forks workflow execution into multiple branches. When you add this activity, you specify a list of one or more branch names. These branch names will be scheduled as activity outcomes.

For example, if you add a Fork activity with branches Do Some Request and Timeout, the Fork activity will show these branches as outcomes.

When the Fork activity executes, both branches will execute. This enables scenarios where you for example want to wait for some user input or some other job to finish, but not indefinitely: you have a second branch that waits for a timeout event using e.g. the Timer activity.

A sample workflow that describes the usage of the Fork activity in a similar scenario can be found here.

That describes the use case of the Fork activity. But you will want to use the If activity instead.

If

Going back to your use case with the HTTP Request activity and setting a condition, what you want to use instead of Fork is the If activity.

When you connect an If activity to HTTP Request, you will be able to write a JS expression that must evaluate to true or false.

For example, let's say your Send HTTP Request activity performs a GET request on https://reqres.in/api/users/2

Make sure that the Read Content checkbox is ticked. Also make sure to give your Send HTTP Request activity a name. For example, SendHttpRequest1.

With that in place, you can now write the following JS expression in the Condition field of the If activity:

activities.SendHttpRequest1.ResponseContent().data.first_name == 'Janet'

Note that activities.SendHttpRequest1.ResponseContent() returns an ExpandoObject that represents the received JSON response from the demo API endpoint I used in my example.

Sipke Schoorstra
  • 3,074
  • 1
  • 20
  • 28
  • Hello @Sipke, so grateful for your time and information about Elsa Control Flow. I was wondering how can I display the result that I get after **IF**. When I send HttpRequest (target is API which points database MS SQL Server) I connect an **IF** activity and that is the situation when I stuck. What should I do after IF to display the result which satisfies the condition from **IF**? – NikolaCSI Jul 08 '21 at 20:28
  • Same principle. If you want to e.g. display the result by sending back an HTTP response, you use the `WriteHttpResponse` activity and set its Content field to e.g. `activities.SendHttpRequest1.ResponseContent().data.first_name` to simply render the name (using the JS syntax). Better yet would be to use the Liquid syntax so you can write a more proper response (even if it is JSON). More info: https://elsa-workflows.github.io/elsa-core/docs/next/expressions/expressions-liquid. Let me know if you have more questions. – Sipke Schoorstra Jul 08 '21 at 20:34