cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Post Partisan
Post Partisan

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
Highlighted
Super User II
Super User II

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


@iAm_ManCat

Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


View solution in original post

6 REPLIES 6
Highlighted
Super User III
Super User III

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 

 

Highlighted
Super User II
Super User II

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


@iAm_ManCat

Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


View solution in original post

Highlighted
Super User III
Super User III

Re: Call code, easy way to reuse code?

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

Highlighted
Super User II
Super User II

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.

Highlighted
Super User II
Super User II

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 🙂


@iAm_ManCat

Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


Highlighted
Super User II
Super User II

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


@iAm_ManCat

Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Find your favorite faces from the community presenting at the Power Platform Community Conference!

Watch Now

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (5,377)