Affan in his narration said, Abu Rabia informed us, saying, I heard Anas bin Malik say, the Messenger of Allah ﷺ said, "When Allah tests the Muslim servant with an affliction in his body, Allah says, 'Record for him the righteous deeds he used to do.' If He restores his health, He washes him clean and purifies him, and if He takes his soul, He forgives him and shows him mercy."
حدثنا حسن وعفان قالا حدثنا حماد بن سلمة عن سنان بن ربيعة
عفان في حديثه قال أخبرنا أبو ربيعة قال سمعت أنس بن مالك قال قال رسول الله ﷺ إذا ابتلى الله العبد المسلم ببلاء في جسده قال الله اكتب له صالح عمله الذي كان يعمله فإن شفاه غسله وطهره وإن قبضه غفر له ورحمه
Its isnad is hasan, because of Abu Rabia Sinan bin Rabia, who is maqbul in his hadith, though they spoke critically of him. Haythami graded the hadith sahih, 2/304, and said, its narrators are trustworthy. Ibn Abu Shayba also related it, 3/233, in the Book of Funerals, on what they said about the reward for fever and illness.