To quote the Gemara in Berachos 10a:
For background: King Chizkiya was ill. Yeshaya gave him a Nevuah that he would die from this illness, and after telling Chizkiya that there was nothing that could be done at this point, as the decree had already been handed down, Chizkiya had this to say:
בן אמוץ כלה נבואתך וצא כך מקובלני מבית אבי אבא אפי' חרב חדה מונחת על צוארו של אדם אל ימנע עצמו מן הרחמים
Son of Amotz, cease your prophecy and leave! Thus have I accepted from the house of my forefather: even if a sharp sword is placed on a person’s neck, he should not withhold himself from mercy.
As the verses in Yeshaya 38 continue, Chizkiya davened that he recover, and indeed he did, gaining for himself another 15 years on the throne.
According to Rashi, the tradition to which Chizkiya refers is the fact that, as described at the very end of Sefer Shmuel, his forefather David saw the malach prepared to smite Yerushalayim but still prayed.
What we see from here is that even if all seems lost, the doors of prayer are never closed. One is always able to pray.
In fact, this is actually codified in Halacha. One who is on his deathbed should say Vidui, whose basic text, as recorded by the Shulchan Aruch (YD 338:2), reads as follows:
מוֹדֶה אֲנִי לְפָנֶיךָ ה' אֱלֹהַי וֵאלֹהֵי אֲבוֹתַי שֶׁרְפוּאָתִי וּמִיתָתִי בְּיָדְךָ, יְהִי רָצוֹן מִלְּפָנֶיךָ שֶׁתְּרַפְּאֵנִי רְפוּאָה שְׁלֵמָה, וְאִם אָמוּת, תְּהֵא מִיתָתִי כַּפָּרָה עַל כָּל חֲטָאִים וַעֲוֹנוֹת וּפְשָׁעִים, שֶׁחָטָאתִי וְשֶׁעָוִיתִי וְשֶׁפָּשַׁעְתִּי לְפָנֶיךָ, וְתֵן חֶלְקִי בְּגַן עֵדֶן, וְזַכֵּנִי לָעוֹלָם הַבָּא הַצָּפוֹן לַצַּדִּיקִים.
I admit to You, Hashem, my G-d and the G-d of my forefathers, that my healing and my death are in Your hands. May it be Your will before You that You heal me with a complete healing, but if I die, may my death be an atonement for all of my sins which I have sinned before You, and give my portion in Gan Eden, and may I merit the World to Come which is hidden in waiting for the righteous.
While a large portion of this prayer describes the imminent death which the one praying will seemingly be subjected to, what it is really saying is that Hashem should spare him and let him live, but that if he should die anyway, at least let it be an atonement for his sins. Like by Chizkiya and David, the one praying should never despair, even if there is a sharp sword on his throat.
Perhaps you may argue that the OP’s case is different, though, as it’s someone else davening for him, rather than the patient davening for himself. To that I point to an earlier Gemara in Berachos, on 5b:
אין חבוש מתיר עצמו מבית האסורים
A prisoner cannot free himself from prison.
In context, this means that while a sick person cannot pray to be saved from his own ailment, someone else can. Certainly in our case, where a sick person can save himself, someone else can!