The Bodhisatta was once born in Bārāṇasī 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ākaṇha Jātaka.