Build custom software when the process is a genuine differentiator, when no SaaS product meets a hard requirement such as in-country data residency or integration with a local system, or when per-user licence costs outgrow the cost of owning the code. Buy SaaS when the process is standard and a mature product already does it well.
Often the right answer is both: buy the commodity platform and build the thin layer that makes it fit. If a product already does the job, we will tell you to buy it. The full reasoning is in build vs buy for custom software.