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)),{TitleSmiley TongueermitTitleTextInput.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)
         ),
         {
            TitleSmiley TongueermitTitleTextInput.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
Level 10

Agreed, indenting should be easier. Currently the only way to make one is to press Ctrl+Enter, which is tied to a carriage return. You don't always need a return though.

Level 8

+1 for this.

Level 10

Fully agree, we need to be able to place comments

Level: Powered On

Agree fully. While using Project Siena I found myself creating complex functions like the ones in this post and I typically found myself copying the functions out into NotePad (or NotePad++) just so I could read them, let alone editing them.

Level 8

Hi,

 

I use the Beautify JS to get the format. Please do not expect all format is correct. But you should try to use it. Here is link http://jsbeautifier.org/

Level: Powered On

Thanks for the tip, I'll check it out..

 

ive been working with PowerBI and I don't mind the formula builder in PowerBI.. it has some basic syntax highlighting and matches ( and ) so you at lease know if you have all your closing ")"..

Level 8

Hi James,

 

    If you are working with PowerBI you may also interesting this one http://www.daxformatter.com/

That is auto format for DAX syntax.

 

Regards,

Level: Powered On

Thanks for the comments.

 

I've used Notepad++ at times which is ok. JSBeutifier works nicely too, thanks.

 

DAX Formatter didn't work for me, I doesn't format the formula because of a syntax error, which is natural as PowerApps formula is different than DAX.

 

But all of these are lacking the intellisence, which is pretty nice in PowerApps Studio. I think the default Formula text box could stay, but you should also have an option to open the formula up in a better editor (formatting + intellisence).

Microsoft Employee

Totally agreed. In addition, it is better to allow comment lines in order to explain a little bit on what the code is going to do. I think if powerapps follows the similar approach as MS office to support to support visual basic for application will be excellent. Thanks.

 

Best Regards,

Terence

Anonymous
Not applicable

Skip the code...this app is suppose to be code free.  The formulas are enough to deal with and should be changed to a graphical interface like InfoPath is.  Building Apps is far more complicated than the tutorials make it out to be.