Rabbi Frand deals with this question. The Ohr HaChaim asks the question
The Ohr HaChaim asks — how could this happen? There is a principle
“Harm does not come to those sent on a mission to do a mitzvah”
(shiluchei mitzvah einan nizokin) [Pesachim 8a]? What happened to the
rule of shiluchei mitzvah einan nizokin when Yosef carried out his
There are two answers given to this question. One answer is based on the idea that Yosef was sent to Shechem to find his brothers. However, he decided to go to Dosan since they were not at Shechem. That is, the mission was to go to Shechem and see what was going on. If the brothers were no longer in Shechem then he should have returned home to report. As a result he was no longer a shaliach when he continued on to Dosan. This answer is rejected because of the following halacha.
(The HaMakneh (by Rav Pinchas haLevi Horowitz [1731-1805] in Maseches
Kiddushin asks on this answer of the Ohr HaChaim, and says that this
argument is not based on normative halachic ruling. The Halacha is
that if a person says to an agent “Give this Get [divorce document] to
my wife in Shechem” and he gives it to her in a different city, the
Get is invalid (because the agent did not carry out the instructions
of the husband). However, if the husband says to the agent, “Give
this Get to my wife – “she can be found in Shechem” — then even if the
agent finds her in a different city and gives her the divorce document
there, it is valid. We say that when the husband specifies, “she can
be found in Shechem” — he is merely helping the agent find his wife
(mar’eh makom hu lah), rather than insisting that the divorce be
carried out in a specific location. Thus, the HaMakneh rejects the
first answer of the Ohr HaChaim.)
The second answer is based on the fact that Yosef, and his entire family (including Yaakov and his brothers) were actually going to be helped by this turn of events.
The Ohr HaChaim gives a second answer to his question, in which he
redefines the definition of “harm” [nezek]
“Harm whose ultimate purpose is good, is not considered harm.”
Ultimately, what happened to Yosef was not a bad thing. Shiluchei
Mitzvah einan nizokin means no bad will befall an agent of mitzvah.
This was not bad because this mission led to Yosef’s winding up in
Egypt, and ultimately saving the world! Admittedly, he went through
some difficulties to get there, but the bottom line was that the
result was not only salvation for his family, but also salvation for
the entire world. Such “trouble” from which great salvation emerges,
is not considered “nezek.”
This idea espouses a very important truth that is not always easy to
realize or accept. Many times, people experience tremendous
challenges, certain that they are experiencing tragic misfortune, yet
the challenges eventually turn out not to be misfortune at all, but
rather a true salvation.
Indeed, why did Yaakov Avinu send Yosef to his brothers when he knew how they felt. He knew that the brothers were watching the sheep in Shechem. He knew that the shepherds and other servants were there and that it was a good sized town. As a result the brothers would not have harmed Yosef. Rav Hirsch points out that it also tested Yosef to see his attitude towards his brothers. Yosef passed the test by going. The brothers were not in Shechem so Yosef would not have found them (and he would have returned to his father saying that they were not in Shechem) had he not stumbled on a stranger (the malach Gavriel) who had happened to overhear them saying that they were going to Dosan. This is an example of how Hashem twists matters so that they come out the way He wants no matter what plans a man may make.
Vayeishev 37:15 Rav Hirsch uses the term blundering.
Then a man found him, and behold, he was straying in the field, and
the man asked him, saying, "What are you looking for?"
We see then that Yosef would not have found his brothers at all had Hashem not decided that this is what needed to be done to save the Bnai Yisrael.