cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
hadimarashy
Frequent Visitor

Avoiding repeating code

I have a very large form, and for each question, I used the below design to make the form shorter and look better. So I am capturing the note, image and action on a popup.

 

Power_Apps.png

 

The (Add Note, Media and Action) label and icon for all questions have the same OnSelect values. Below is the code for Action OnSelect for label and icon.

If(IsBlank(LookUp(temp_action,action_category = Parent.DataField,action_required)),
Set(add_action_show_fields,1);
Set(weekly_safety_inspection_add_inspection,1);
, Set(view_action_fields,1));Set(temp_category,Parent.DataField);

Now there is a duplication of codes, and if I change anything, I have to replicate the code on all locations manually.

 

Thanks in advance for any suggestions.

6 REPLIES 6
RandyHayes
Super User
Super User

@hadimarashy 

What is it in your Formula that you are repeating and what is the need/use for it?  You showed a bunch of variables, but not mentioning what their purpose is.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
hadimarashy
Frequent Visitor

Hi @RandyHayes ,


Thank you for replying.


I am storing my data in SQL server, and I use a collection to temporary hold data named temp_action". I am checking if the collection contains any value for the DataCard. I used Parent.DataField to get the name of the field in the collection.

 

If there is no info for that field, I set add_action_show_fields and weekly_safety_inspection_add_inspection to 1 to open a popup and show the blank fields. If there is data, I set view_action_fields to 1 and open another popup to view the information.

 

Also, I set the temp_category to add the information to the right field in the collection.

If(IsBlank(LookUp(temp_action,action_category = Parent.DataField,action_required)),
Set(add_action_show_fields,1);
Set(weekly_safety_inspection_add_inspection,1);
, Set(view_action_fields,1));Set(temp_category,Parent.DataField);

I hope that makes sense.

RandyHayes
Super User
Super User

@hadimarashy 

So, in general, yes, you want to always avoid repeating Formulas in PowerApps as much as possible.  However, there are times when it is necessary.  IN such cases, the formulas should be generic. Meaning that if there is a change to something, the formula does not need to be changed.

 

Overall, I am not clear as to what purpose you are trying to achieve as this appears to be all in an EditForm.

You have shown a formula but have not mentioned where this formula is in your app.  So it is a little difficult to suggest some changes that might help you more.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Hi @RandyHayes ,

 

So I am trying to create a form to conduct weekly inspections for construction sites.

 

A user can create several notes for each question, take multiple images, and add numerous actions.

 

Power Apps 2.png

 

Therefore, I have three different tables for Images, notes and actions and I created popups to allow the user to collect multiple images, notes and actions.

 

Power Apps 4.pngPower Apps 3.png

 

All questions have the same OnSelect values for Image (to open image popup), Notes(to open note popup) and action(action popup). I use Parent.DataField name to check if any data exists and save the data into the right field.

 

Unfortunately, I can not use the Select function because of the Parent.DataField gets the first DataCard name.

 

 

RandyHayes
Super User
Super User

@hadimarashy 
So first of all...looking at that picture, you should be utilizing a Gallery for all of these as you really have repetition as it is in the questions.  I cannot tell for sure from the picture if you are, but you seem to suggest that you have created a form with this in and, since you were concerned over repetition, I am guessing it is not a gallery.

 

Can you clarify that?

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Hi @RandyHayes ,

 

I used one gallery for the pass and fail questions for each question. However, I had to create a group for "Note, Media and Action". Because the OnSelect of Note and Media and Action are different also, text and colour will change if there are values in the collection.


Please see the below picture.

 

Power Apps 5.png

 

Thank you for your help.

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (4,904)