Abdullah bin Amr bin As reported,

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.'"

Azami 3388

Sahih

عن عبد الله بن عمرو

عن النبي ﷺ قال: ما أحد من الناس يصاب ببلاء في جسده إلا أمر الله عز وجل الملائكة الذين يحفظونه فقال: اكتبوا لعبدي في كل يوم وليلة ما كان يعمل من خير، ما كان في وثاقي.

الأعظمي 3388صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Funerals
Chapter
Patience in tribulation and illness
Section
Sick and the traveler are credited with what they used to do while resident and healthy
Numbering
Jami al-Kamil 3388 (2nd edition)