cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
gclifford
Helper I
Helper I

Best way to handle reuseable code?

Hello,

 

I have a bunch of code being used repetitively in my app - an example is that I need to update a collection, clearcollection(x,y,x,y), and the formula is very long and complicated.  Currently I have this same clearcollection command copy/pasted onto many pages within the application so that it gets updated as needed.  Is there a way to wrap this into a function of some kind and then just call the function on each page so that if I ever need to update the formula for the collection I can do in a single place?

 

I have many other use cases like the above - this is just an example.

 

G

17 REPLIES 17
ZabiBabar
Continued Contributor
Continued Contributor

Hey @gclifford , I do not know of a way to store functions and commands in a variable or your own function. However, you can use component libraries for this purpose. Libraries accept input and return an output but even then, I don't think that will work for the clearCollect cases but for other cases, it might work. 

 

You can vote for this idea https://powerusers.microsoft.com/t5/Power-Apps-Ideas/Reusable-formulas/idi-p/549066.

 

Best,
Zabi
Mark this post as a solution, or kudo it if you found it be helpful. You can choose more than multiple answers as solutions, including your own answer. 🙂

eka24
Super User III
Super User III

It depends on the collection in particular. The ClearCollect itself could be reused.

Some of the functions are Set, UpdateContext and With.

Can give more information on what you want to achieve?

------------

If you like this post, give a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users find it.

Hi @gclifford ,

 

In Power Apps, unfortunately we don't have a way to do the code reusability 😥 .... If I read you right you want to implement the way how we create a class in .Net project where we consolidate all the reusable code and refer across the project.

As the tag name of Power App is NO CODE / LOW CODE 😄 So the product team has not given that opportunity for us. I have also experienced this issue and spent time hours on this but there is no way. Well we can create a screen to define colors and other regular controls but not for creating or clearing collections.

 

I hope this resolved your issue if you see any challenge/need further help please let me know I am always happy to do it for my community.

Regards,
Krishna
If this post helps you give a 👍 and if it solved your issue consider Accept it as the solution to help the other members find it more.

 


I hope this resolved your issue if you see any challenge/need further help please let me know I am always happy to do it for my community.

Regards,
KrishnaV
Business Applications MVP
Twitter | Linkedin | YouTube | GitHub
If this post helps you give a and if it solved your issue consider Accept it as the solution to help the other members / new members of the community.

@gclifford 
I like to use the toggle control to store code that is used multiple places in the same app.  It serves the same purpose as a 'function' in other pro-developer languages.


To start, create a new toggle and put your re-usable put code in the OnCheck property.

 

your_code_here

 

Next, put this code in the Default property of the toggle

 

varDefaultToggle

 

Finally, put this code wherever you want to trigger the 'function' code to run

 

Set(varDefaultToggle, true); Set(varDefaultToggle, false)

 

Thanks @KrishnaV for asking me to participate in this thread.

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up." 

Hi @gclifford ,

If you are on the same screen, you can do it with a hidden button with the code on it and use

 

Select(ButtonName)

 

which is the same as selecting the button. @mdevaney 's workaround however is very clever and is a better option - however I was wondering if remotely setting a control triggers the OnSelect property.

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

 

Hi @mdevaney ,

 

WOOOOOOW.... what an idea Matthew 😊 I really respect your knowledge and idea.

@gclifford  I believe you got Matthew idea if not please let me know I can elaborate further.

 

Again, thank you for the pro tip, Matthew.

 

Regards,

Krishna


I hope this resolved your issue if you see any challenge/need further help please let me know I am always happy to do it for my community.

Regards,
KrishnaV
Business Applications MVP
Twitter | Linkedin | YouTube | GitHub
If this post helps you give a and if it solved your issue consider Accept it as the solution to help the other members / new members of the community.

@KrishnaV 
My favorite use cases for the 'toggle as a function' trick are:

  • Refreshing a collection with new data
  • Executing code when a component is clicked
  • Sending an email message 

 

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."

Hi @mdevaney ,

I cannot get this to work (and would love to use it) - I also tried a Reset(ToggleName) in the middle. The code works if you select the control (as you would expect), but I am getting no results by simply toggling the Variable.

 Hi @WarrenBelz ,

 

It worked for me but a small change:
1. Set the code for onChange event of the toggle

2. onSelect button on the other screen change the formula as

Set(varResetToggle, !varResetToggle);

 

I hope now your code will also execute this change.

 

Regards,

Krishna


I hope this resolved your issue if you see any challenge/need further help please let me know I am always happy to do it for my community.

Regards,
KrishnaV
Business Applications MVP
Twitter | Linkedin | YouTube | GitHub
If this post helps you give a and if it solved your issue consider Accept it as the solution to help the other members / new members of the community.

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Don't miss the call this month on June 16th - 8a PDT

Top Solution Authors
Top Kudoed Authors
Users online (70,699)