Replace an element?

I want to replace a task with sub process ( collapsed ) dynamically ? Please guide me in achieving it.
This

with this

Thanks in advance.

What did you try to do? Could you be specific in what problems are you facing?

Here is a good starting point for looking on how to implement custom element replacement.