Search code examples
salesforceapex

Write Unit Test for object that triggers a flow


I have a custom object that upon creation triggers an autolaunched flow that creates some related objects and sets some other fields. This flow is set to run after the object is saved to the database.

I have a few unit tests that create the custom object as part of setup, and later in the test it relies on the associated related objects that get created by the flow.

These unit tests run fine locally, but when I am applying change sets to my org I see these tests failing a decent number of times because the flow hasn't had time to run and finish setting up the related objects.

There is obviously a timing issue here, but how do I make my unit tests less flaky? How do I ensure in my unit tests that associated flows have had time to run?


Solution

  • So the flow in question was actually not activated.

    What happened was my changeset included the newly created flow and the tests that were failing. Even though the Flow was activated on the sandbox with which I created the outbound changeset, it appears when that flow is deployed to a new sandbox it isn't activated.

    The fix was to create a new change set with just the new flow, deploy and activate the flow, then deploy the remaining changeset.