Did you find yourself too often in the situation of copy/pasting some functionality or function in your canvas app? This is not only annoying and time consuming, but it also increases complexity and creates a greater risk for errors. Let me show you how to tackle this problem.

The Developer Point of View

Let’s put the developer glasses on and look into your functions as reusable pieces. To be able to address the code from different locations we bind the code block to the OnSelect event of a button. This way we are able to use the Select() function to call the code. Let’s look at this with an example.

An Example

Here you can see an example of a function which calculates a discount. This code can then be triggered from multiple locations by using Select(buttonName). The total variable will contain the correct result. Another benefit with this approach is that you can easily debug this function buttons. To do this, introduce a variable bound to a button which allows to switch between true/false. That variable can be a debugcontext holding the visible property of the function buttons to either show or hide them. Doing so gives you the opportunity to trigger the functions yourself.

Pay Attention

Using the Select() function in canvas apps puts the function on an execution queue. This is called deferred execution. What it means is that the Select is not necessary executed directly, nor can there be any certainty that two Select() functions will be executed sub sequentially. For example, if you have two buttons who are executed after each other but have dependencies on each other, problems may arise and the desired result will differ from what you expect. This can be the case if buttons do external calls to third party systems to fetch data.

Do you already use buttons as functions? What is your experience with this approach? Reach out to me via comments or social media!