Writing a will for a charitable cause, especially if one is rich, is something recommended and rewarded. It is considered an ongoing perpetual charity in which the Prophet (peace and blessings be upon him) said: “When a person dies, his works cease except three: An ongoing charity he has set up or endowed, beneficial knowledge he has imparted, or a righteous offspring who prays for him.”(Muslim)
However, a will should be made within the limits of one third of the entire estate.
In this regard, Dr. Monzer Kahf, a prominent economist and counselor said:
It is not permissible to write a will that may violate any part of the Islamic inheritance system that is mostly outlined in the Qur’an and it is compolsary (wajib).
If you live in a Muslim country and your family is Muslim, any last will you leave, will only be implemented to the extent of one third. This is because the Prophet (peace and blessings be upon him) said that one may leave a maximum of one third for good causes.
Of course, this is so if the intention is not to prevent or change the distribution of inheritance as any intention to change the distribution of inheritance is sinful.