The Prophet ﷺ said, "There is no person afflicted with an ailment in his body except that Allah commands the angels who guard him, saying, 'Record for My servant each day and night the good deeds he used to do, as long as he remains held by Me.'"
عن النبي ﷺ قال: ما أحد من الناس يصاب ببلاء في جسده إلا أمر الله عز وجل الملائكة الذين يحفظونه فقال: اكتبوا لعبدي في كل يوم وليلة ما كان يعمل من خير، ما كان في وثاقي.
Imam Ahmad narrated it (6482), from Ishaq bin Yusuf Azraq, who said, Sufyan Thawri narrated to us, from Alqama bin Marthad, from Qasim, meaning Ibn Mukhaymara, from Abdullah bin Amr, and he related it.He also narrated it (6825) from Ishaq bin Yusuf, paired with Waki, both of whom said, Sufyan narrated to us with his chain, and it contains, "Write down for My servant the like of what he used to do while he was sound, for as long as he remains confined in My bond." Its chain of transmission is sahih.Hakim also narrated it (1/384) through the route of Sufyan, and said, "Sahih according to the standard of the two Shaykhs."Haythami said in Majma (2/303), "Ahmad and Bazzar narrated it, as did Tabarani in Kabir, and the men of Ahmad's chain are the men of the Sahih."I say, it is as he said, though Bukhari narrated from Qasim bin Mukhaymara in a suspended form, but Hakim and Haythami do not distinguish between the original connected transmission and the suspended one.