cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Oskarkuus
Level: Powered On

Call code, easy way to reuse code?

Is there any way to "call" on a code instead of building in every different place.



For example when working with many variables

"set(variablename;Editform(form1) & Editform(form2))"



Instead of writing above example i would like to set this code somewhere and then just call it instead...

"call code"



I know i can do this in VBA code. Set a module with a code and then call upon that sub in a module?

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Call code, easy way to reuse code?

Hi,

 

Agree with @timl that there's no easy way to do this, also adding in here that buttons are not the most efficient way to go as you can't actually run the Select() function against a button that is not on that same screen that you are running the Select() from, so you'd have to hide it on that screen.

 

What I do is to use toggles.

 

Set the default value of the toggle to a variable.

Set the OnCheck to your code that you want to call.

Have whatever you are trying to call code from set that variable to true and then false (thus triggering the code via OnCheck)

 

 

This can be done from a separate screen and you could then have a screen full of code triggers - ama releasing video on this as part of my next branding template update, but the info I mentioned above pretty much covers it 😸

 

Cheers,

Sancho




Don't forget to 'Mark as Solution' if a post answered your question and always 'Thumbs Up' the posts you like or helped you!

View solution in original post

6 REPLIES 6
Super User
Super User

Re: Call code, easy way to reuse code?

Hi @Oskarkuus 

Unfortunately, there isn't an exact way to do this. There's a post in the ideas forum about adding macros/functions which you can vote on. If enough people vote for it, Microsoft will hopefully add this as a new feature.

https://powerusers.microsoft.com/t5/Power-Apps-Ideas/Create-custom-functions-macros/idi-p/6187 

A workaround is to add your code to a button - this could be on a hidden screen.

In the parts of your app where you want to call this code, you can call the Select function to 'click' your hidden button.

https://docs.microsoft.com/en-gb/powerapps/maker/canvas-apps/functions/function-select 

 

Super User
Super User

Re: Call code, easy way to reuse code?

Hi,

 

Agree with @timl that there's no easy way to do this, also adding in here that buttons are not the most efficient way to go as you can't actually run the Select() function against a button that is not on that same screen that you are running the Select() from, so you'd have to hide it on that screen.

 

What I do is to use toggles.

 

Set the default value of the toggle to a variable.

Set the OnCheck to your code that you want to call.

Have whatever you are trying to call code from set that variable to true and then false (thus triggering the code via OnCheck)

 

 

This can be done from a separate screen and you could then have a screen full of code triggers - ama releasing video on this as part of my next branding template update, but the info I mentioned above pretty much covers it 😸

 

Cheers,

Sancho




Don't forget to 'Mark as Solution' if a post answered your question and always 'Thumbs Up' the posts you like or helped you!

View solution in original post

Super User
Super User

Re: Call code, easy way to reuse code?

 @iAm_ManCat - Thanks for clarifying the 'button' trick 🙂 I forgot about the same screen limitation.

Eelman
Level 10

Re: Call code, easy way to reuse code?

@iAm_ManCat nice solution. I use hidden buttons now but also have to repeat code on different screens. I'm going to give these toggles a try. It will make code management sooooo much easier.

 

Do you have any issues with code running concurrently? I found out the hard way that any code that runs after my hidden button code - but relies on it's output - can fail because the hidden button code would appear to run concurrently to the code after? eg

 

<someCode>

Select(hiddenButton)

<someMoreCode>

 

someMoreCode doesn't always get the output (depending on speed) from the hiddenButton code which can cause issues. In cases like this I've had to move dependent code into the hiddenButton code, which can then cause extra lag.

Super User
Super User

Re: Call code, easy way to reuse code?

Yes, it would have the same concurrency issue, but I would put nearly all of the code into the toggle rather than only triggering a part of it.

 

And yes, I would think that there are some things you may not necessarily be able to trigger this way, but it does help to keep the majority of it in a central location for maintenance purposes 🙂




Don't forget to 'Mark as Solution' if a post answered your question and always 'Thumbs Up' the posts you like or helped you!
Super User
Super User

Re: Call code, easy way to reuse code?

Issue with this toggle trigger has been raised here:

https://powerusers.microsoft.com/t5/Building-Power-Apps-Formerly/Reuse-code-with-toggle-button/m-p/4...

 

The remote toggle trigger no longer works on IE/Edge, and I think its due to the way the browsers are loading the screens/items into memory - I can trigger the toggle remotely, but it only executes the moment that I navigate to the screen the toggles are on!

 

So I guess it's back to buttons for now! 😵

 

[EDIT]

 

The solution is to have a reference to that variable on the same screen that you are calling from, ie just a label set to that variable.

I didn't notice this on my existing samples vs a blank test as normally I would have conditionals or other logic on that variable around the same screen:

image.png




Don't forget to 'Mark as Solution' if a post answered your question and always 'Thumbs Up' the posts you like or helped you!

Helpful resources

Announcements
thirdimage

Power Apps Super User Class of 2020

Check it out!

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors (Last 30 Days)
Users online (2,814)