It is narrated from Abdullah bin Arqam (may Allah be pleased with him) that he was leading his companions in prayer one day, then he went to relieve himself (appointing someone to lead in his place), and when he returned, he said: I heard the Messenger of Allah (ﷺ) say, "When any of you feels the need to relieve himself, he should do so before performing the prayer."
Hadith Referenceمسند الإمام الشافعي / كتاب الصلاة / 325
Hadith Takhrijأخرجه الترمذي: الطهارة، باب ما جاء إذا أقيمت الصلاة ووجد أحدكم الخلاء فليبدأ بالخلاء: 142 - وقال حسن صحيح۔ وأبو داود : الطهارة، باب أيصلي الرجل وهو حاقن: 88 ۔ والنسائي: الإمامة، باب العذر في ترك الجماعة : 853 ۔ وصححه ابن خزيمة : 932، 1652 ۔ والحاكم : 1/ 168، 257۔