に代わって => instead of; replacing; on behalf of ~ N3
~ひょうしに(~拍子に)   => the moment [A], unintentionally / inadvertently caused something to happen N1
代わりに => instead of; as a substitute for​; in exchange for; in return for N3
[代「か」]わり => Not A, but B, B in place of A, Instead of A, B; In exchange for A, B N3
Comparison, Exchange