BPM variable scope

I have created a set of variables inside my BPM.



I am setting them within a custom code block. This works fine.

I then proceed to the next custom code block and consume some of the variables. At this point, the variable values have been set to null.

I assumed that the BPM variables created and set via custom code would persist throughout the execution scope of the BPM, but it seems to only set them for the scope of that particular code block. It only “sticks” for the life of the BPM if I use the Set Variable widget.

Is this expected?

You are re-declaring your variables (locally) inside the code block. If you look at it from outside your current code looks like this

DateTime today;

void CodeBlock()
{
          DateTime today=DateTime.Today; // Re-declares the variable as a local variable that only has scope inside this code block
}

Console.WriteLine(today); // Today is still null here, because you haven't assigned any value to it.

You should not re-declare it inside the code block. Just assign it a value

void CodeBlock()
{
 today=DateTime.Today; 
}
4 Likes