‘Abdullah ibn ‘Umar reported that the Messenger (may the peace and blessings of Allah be upon him) said:
Three men of a people before you, were on a journey when they were overtaken by a storm and therefore they took shelter in a cave. A rock slipped down from the mountain and blocked the exit from cave.
"The only way for deliverance left is to beseech Allah in the name of some virtuous deed." thereupon one of them supplicated,
"O Lord, my parents were very old, and I used to offer them their nightly drink of milk before my children and the other members of the family. One day I went astray far away in search of green trees and could return only after my parent had gone to sleep. When I had milked the animals, and brought their nightly drink to them. They were fast asleep, but I did not like to disturb them, nor would give any part of the milk to my children and other members of the family till after my parents had their drink. Thus, with the vessel in hand, I awaited their awakening till the flush of dawn, while the children cried out of hunger at my feet. When they woke up, they had their drink. O Lord, if I did this thing seeking only your pleasure, then do relieve us of the distress wrought upon us by this rock."
Thereupon, the rock moved a little but not enough to let them pass out.
"O Lord, I had a cousin that I loved more passionately than any man could love a woman. I tried to seduce her, but she would refuse, till in a season of great hardship due to famine, she approached me (for help) and I gave her one hundred and twenty Dinars on the condition that she would have sexual intercourse with me. She agreed, and when we got together, and I was just going to have intercourse with her, she pleaded, 'Fear Allah, and do not break the seal unlawfully', whereupon I moved away from her, despite the fact that I desired her most passionately; and I let her keep the money I had given her. O Lord, if I did this thing seeking only your pleasure, then do move the distress in which we find ourselves."
Again, the rock moved a little but not enough to let them pass out.
"O Lord, I hired some labourers and paid them their dues, but one of them left leaving behind what was due to him. I invested it in business and the business prospered greatly.
After a while, the labourer came back and said: 'O servant of Allah, hand over to me my wages.' I said to him: 'All that you see is yours; camels, cattle, goats and slaves." He said: 'Don't play joke with me, O servant of Allah.' I assured him: 'I am not joking. So, he took all of it sparing nothing. O Lord, if I did this seeking only for your pleasure, do relieve us of our distress."
The rock then moved away, and all the three came out of the cave safe and sound.
From the story within this hadith we learn of one of the permissible forms of tawassul, which is to ask Allah by your good deeds which you have done in the past. Note that every time one of the men mentioned his good deed, he put a condition on it that Allah judge whether it was performed sincerely, as a deed which is performed without sincerity will not be accepted by Allah. We also learn some of the noble types of deeds, which include looking after one’s parents, avoiding a sin even when one is in the last seconds of committing it, and being careful and wise with responsibilities that are trusted to you.