# Workflow with a "state"

**URL:** <https://community.temporal.io/t/workflow-with-a-state/6851>\
**Category:** Community Support\
**Tags:** java-sdk, general-impl\
**Created:** [January 5, 2023, 9:08am UTC](https://community.temporal.io/t/workflow-with-a-state/6851 "2023-01-05T09:08:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Artur](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@Artur](https://community.temporal.io/u/Artur)\
**Post date:** [January 5, 2023, 9:08am UTC](https://community.temporal.io/t/workflow-with-a-state/6851/1 "2023-01-05T09:08:29Z")

</div>

Hi all,  
so I have a workflow with 3 activities  
The first activity calls an API and returns a model.  
Second’s + Thirds payload is the model from the First activity.  
So second activity triggers an install.  
The third one monitoring the installation (polling).

I saw that once the monitoring is failed for some kind of reason,  
Temporal again runs all 3 activities, so it triggers another installation.  
As I understood, we should not use “if/else” in workflows because it breaks the deterministic.  
My question is, can I add an if statement for the payload? I mean if the payload is not null, just go straight to monitoring activity?

Thanks

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [January 5, 2023, 3:50pm UTC](https://community.temporal.io/t/workflow-with-a-state/6851/2 "2023-01-05T15:50:07Z")

</div>

This is not expected behavior unless you use local activities.

> As I understood, we should not use “if/else” in workflows because it breaks the deterministic.

This is an incorrect understanding. You can use any Java conditional, and other language constructs inside the workflows. Determinism means that they should produce the same result given the same values of the variables. So

```auto
if (a > 5) {
}

```

is deterministic. But

```auto
if (a > random(5)) {
}

```

is not, as the condition can take a different branch even if the value of `a` is the same.

---

<div class="post-metadata">

**Author:** ![Artur](https://avatars.discourse-cdn.com/v4/letter/a/53a042/32.png) [@Artur](https://community.temporal.io/u/Artur)\
**Post date:** [January 15, 2023, 8:01am UTC](https://community.temporal.io/t/workflow-with-a-state/6851/3 "2023-01-15T08:01:36Z")

</div>

So I can use a local variable inside a workflow? initiate it to null,  
assign the result from activity1  
create an “if” for the second/third activities.  
As it is not really random, it is a question of null or not.  
Or, once it can be null or not, based on the same input - it is random?

---

<div class="post-metadata">

**Author:** ![Jaraxxusx](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jaraxxusx](https://community.temporal.io/u/Jaraxxusx)\
**Post date:** [January 15, 2023, 10:10am UTC](https://community.temporal.io/t/workflow-with-a-state/6851/4 "2023-01-15T10:10:34Z")

</div>

Hello. Code inside activity could do anything. Code inside workflow must be deterministic, what means that same workflow method call should produce same sequence of events.  
Example:

```auto
void workflowMethod(int param) {
    int result = activity1(param)

   activity2(result)
}

```

when activity1 is completes temporal store its result in internal db. This means that it wont call that activity again, so the code in that activity can do anything

```auto
int activty1(int param) {
   return new Random().nextInt()
}

```

But you cant do something like that:

```auto
void workflowMethod(int param) {
    int result = activity1(param)

    
    int test = new Random().nextInt()
   
    // no deterministic error here
   activity2(result, test)

   Workflow.sleep(1minute)
}

```

what will happen here:  
temporal scheduled activity 1  
temporal execute activity 1 (return random int) and store its result in db

next we generating random number inside workflow, for example it will return 4  
nex temporal scheduled activity 2 with params: anything and random number = 4  
temporal executes it and store result in db

next sleeping for 1 minute

and when workflow is awake temporal will reexecute a worflow methods with cached results from events:

it will schedule activity 1 and get result from cache,  
next it will generate a random number again (it wasnt cached), for example it will be equal to 6  
next it will schedule activity 2 with params anything and 6, and validation will be failed because params should be anything and 4, thas why NoDeterministic error would be thrown

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [January 17, 2023, 8:04pm UTC](https://community.temporal.io/t/workflow-with-a-state/6851/5 "2023-01-17T20:04:16Z")

</div>

> [@Artur](#):
>
> So I can use a local variable inside a workflow? initiate it to null,  
> assign the result from activity1  
> create an “if” for the second/third activities.  
> As it is not really random, it is a question of null or not.  
> Or, once it can be null or not, based on the same input - it is random?

It will be assigned the result of activity1. So it cannot be null after that. So such code is absolutely deterministic.
