cancel
Showing results for 
Search instead for 
Did you mean: 

Better code editor and User defined functions

I think it would be a good idea to have better code editor and user defined functions/macros or what ever the name, to make the code inside PowerApps more readable and reuseable. It is errorprone to write long nested strings of commands inside a simple multiline textbox.

 

For example this is OnSelect code of a button on an app I'm developing. I'll try to explain how to make it more readable below.

 

If(CreateNew=true,Collect('[dbo].[AssignmentPermit]',{SafetyManagementPlanId: SafetyManagementPlanGallery.Selected.Id, Title: PermitTitleTextInput.Text, TargetName: PermitTargetNameTextInput.Text, Identifier: PermitIdentifierTextInput.Text, SafetyManagementAuthorizerName: PermitSafetyManagementAuthorizerNameTextInput.Text, SafetyManagementAuthorizerPhone: PermitSafetyManagementAuthorizerPhoneTextInput.Text, PermittedCompany: PermitPermittedCompanyTextInput.Text, PermittedContactName: PermitPermittedContactNameTextInput.Text, PermittedContactPhone: PermitPermittedContactPhoneTextInput.Text, AdditionalInformation: PermitAdditionalInformationTextInput.Text}),Patch('[dbo].[AssignmentPermit]',First(Filter('[dbo].[AssignmentPermit]',Id=AssignmentPermitId)),{Title:PermitTitleTextInput.Text,TargetName: PermitTargetNameTextInput.Text, Identifier: PermitIdentifierTextInput.Text, SafetyManagementAuthorizerName: PermitSafetyManagementAuthorizerNameTextInput.Text, SafetyManagementAuthorizerPhone: PermitSafetyManagementAuthorizerPhoneTextInput.Text, PermittedCompany: PermitPermittedCompanyTextInput.Text, PermittedContactName: PermitPermittedContactNameTextInput.Text, PermittedContactPhone: PermitPermittedContactPhoneTextInput.Text, AdditionalInformation: PermitAdditionalInformationTextInput.Text}));If(CreateNew=true,UpdateContext({CreateNew:false,AssignmentPermitId:Last(Filter('[dbo].[AssignmentPermit]',SafetyManagementPlanId=SafetyManagementPlanGallery.Selected.Id)).Id}))

 

Identing as a way of making code readable, what I would like to have is ability to switch line editor to auto-ident mode.

 

 

 

If(
   CreateNew=true,
   Collect(
      '[dbo].[AssignmentPermit]',
      {
         SafetyManagementPlanId: SafetyManagementPlanGallery.Selected.Id,
         Title: PermitTitleTextInput.Text,
         TargetName: PermitTargetNameTextInput.Text,
         Identifier: PermitIdentifierTextInput.Text,
         SafetyManagementAuthorizerName: PermitSafetyManagementAuthorizerNameTextInput.Text,
         SafetyManagementAuthorizerPhone: PermitSafetyManagementAuthorizerPhoneTextInput.Text,
         PermittedCompany: PermitPermittedCompanyTextInput.Text,
         PermittedContactName: PermitPermittedContactNameTextInput.Text,
         PermittedContactPhone: PermitPermittedContactPhoneTextInput.Text,
         AdditionalInformation: PermitAdditionalInformationTextInput.Text
      }
   ),
   Patch(
      '[dbo].[AssignmentPermit]',
      First(
         Filter(
            '[dbo].[AssignmentPermit]',
            Id=AssignmentPermitId)
         ),
         {
            Title:PermitTitleTextInput.Text,
            TargetName: PermitTargetNameTextInput.Text,
            Identifier: PermitIdentifierTextInput.Text,
            SafetyManagementAuthorizerName: PermitSafetyManagementAuthorizerNameTextInput.Text,
            SafetyManagementAuthorizerPhone: PermitSafetyManagementAuthorizerPhoneTextInput.Text,
            PermittedCompany: PermitPermittedCompanyTextInput.Text,
            PermittedContactName: PermitPermittedContactNameTextInput.Text,
            PermittedContactPhone: PermitPermittedContactPhoneTextInput.Text,
            AdditionalInformation: PermitAdditionalInformationTextInput.Text
         }
      )
   );

   If(
      CreateNew=true,
      UpdateContext(
         {
            CreateNew:false,
            AssignmentPermitId:Last(
               Filter(
                  '[dbo].[AssignmentPermit]',
                  SafetyManagementPlanId=SafetyManagementPlanGallery.Selected.Id
               )
            ).Id
         }
       )
   )

Commenting would be nice feature as well - I don't know if you have it already, I might have just overlooked it.

 

 

Reuseability of code could be real user defined functions or simply code blocks.

 

1. Define reuseable code block MyCodeBlock1. This is simple, not a real function as it does not need parameters. It simply functions as its code would be right there where it is inserted.

 

         Title: PermitTitleTextInput.Text,
         TargetName: PermitTargetNameTextInput.Text,
         Identifier: PermitIdentifierTextInput.Text,
         SafetyManagementAuthorizerName: PermitSafetyManagementAuthorizerNameTextInput.Text,
         SafetyManagementAuthorizerPhone: PermitSafetyManagementAuthorizerPhoneTextInput.Text,
         PermittedCompany: PermitPermittedCompanyTextInput.Text,
         PermittedContactName: PermitPermittedContactNameTextInput.Text,
         PermittedContactPhone: PermitPermittedContactPhoneTextInput.Text,
         AdditionalInformation: PermitAdditionalInformationTextInput.Text

2. So the original code would be like this.

 

 

If(
   CreateNew=true,
   Collect(
      '[dbo].[AssignmentPermit]',
      {
         SafetyManagementPlanId: SafetyManagementPlanGallery.Selected.Id,
         ReuseCode(MyCodeBlock1)
      }
   ),
   Patch(
      '[dbo].[AssignmentPermit]',
      First(
         Filter(
            '[dbo].[AssignmentPermit]',
            Id=AssignmentPermitId)
         ),
         {
            ReuseCode(MyCodeBlock1)
         }
      )
   );

   If(
      CreateNew=true,
      UpdateContext(
         {
            CreateNew:false,
            AssignmentPermitId:Last(
               Filter(
                  '[dbo].[AssignmentPermit]',
                  SafetyManagementPlanId=SafetyManagementPlanGallery.Selected.Id
               )
            ).Id
         }
       )
 

Well. Just a few ideas there.

 

Cheers,

 

Olli

 

Status: Planned
Comments
Anonymous
Not applicable

agreed , edit a simple formula is horror.

no overview and it feels each cursor move or click have seconds delay.

PLEASE make it like feels i am in visual studio editor.

i need to have that feeling that my editing is under control.

maybe also try construct 2, how it using visual editing.

Audrie-MSFT
Power Automate
Status changed to: Planned
 
joshbooker
Kudo Kingpin

Improved expression editing is a good idea.  The above is a rather good example of the kind of expressions that are common in PA.  Anyone (attn: product team) who thinks the above expression is 'simpler' for 'power-users'/'citizen devs' than a stright up javascript function is living in a fantasy.

 

We need user defined javascript functions and control extensions.

 

As has been mentioned for months on this forum....it's just like Excel VBA.  Many non-dev types are comfortable in VBA.  If they have a need beyond that supported by the expression language in excel, they pop into vba and make it happen.  If they are not comfortable, the bingle search and find a nice blog of how to:  paste this vba in your code, then put that function name in your expression....done!

 

Adding user defined javascript will enable partners and developers to provide those solutions for users on edge cases while enabling us to be more productive -bust out of the confines of expressions.

 

 

Anonymous
Not applicable

my feeling at expression editing.

the expression is fragile if there are much rows, it get out of control.

 

 

 

Anonymous
Not applicable

Definitely a must have in the studio! 

I have found myself a lot in the situation when the code editor is just too small and I had to scroll up and down trying to build up a formula. Not to mention that the bigger the formula gets the slower it scrolls and the intellisense loads slower.

 

 

 

Maybe you can make the left and right panels collapse, making space for a code editor on one of the sides. Just an idea 🙂 

yegdeveloper
Advocate II

Totally agree.

I have to use SHIFT+ENTER to create legible, indented code.

 

It should be automatic and would massively speed up development.

tting_idmod
New Member

Kudos to the idea, code readability is super important for maintenanability any business app. 

Anonymous
Not applicable

Simple macros and commenting are definitely a must implement 🙂

 

Kudos, boi

tex1
Frequent Visitor

Strange there is no official Microsoft comment on this one... Commenting and coding assistance are critical for maintenance and faster/less eror-prone development. You can only dumb down low-code environments so far and there is always a need to do something "just a bit" more complex.

davidsemitekol
Frequent Visitor

C'mon Microsoft, this was asked over 3 years ago!  PowerApps is a great tool but we need a better way to write the code, like in NotePad++