The Bodhisatta was once born in Benares and became its king. At that time people were in the habit of sacrificing animals to the gods in order to win their favour, but the Bodhisatta proclaimed that no living creature should be slain. Being enraged at the loss of their food, the Yakkhas sent one of their number to the Bodhisatta. He came to the Bodhisatta's bed at night meaning to strike him a deadly blow. Thereupon Sakka's throne grew hot, and learning the cause, Sakka himself came and stood guard over the Bodhisatta. The latter saw the Yakkha standing over him ready to strike but powerless, and only learnt later, to his great encouragement, that Sakka had been there to protect him (J.iii.145-7).
The reason for the telling of the story is given in the Mahākanha Jātaka.