cancel
Showing results for 
Search instead for 
Did you mean: 

User-defined functions

Ability for users to create their own functions with input parameters and output properties. This would allow users to streamline their code and make their apps much easier to maintain.

 

Proposal:

  • Create new “DefineFunction” function that accepts input parameters and returns values specified by the function itself.

“DefineFunction” definition:

DefineFunction(FunctionName, [InputParameters], [OutputParameters], FunctionBody)
  • FunctionName = Name of the new Function that can be called later in the application
  • InputParameters = Comma delimited list of parameters, each parameter acts as its own self-contained context variable visible only to the function being defined
  • OutputParameters = Comma delimited list of properties that can be updated inside of the function and referenced outside of the function
  • FunctionBody = List of standard PowerApps functions delimited by semi-colons; can make use of the input parameters and return properties defined earlier
  • Additionally, all variables or collections defined within the Function would be accessible only from within the function.

Creating a function:

DefineFunction(
    “AddRecord”,
    [ID, RecordName],
    [ReturnStatus],
    UpdateContext({
        new_Record:
        Patch([Items], Defaults([Items]), {

            Item_ID: ID,

            ItemName: RecordName
        })
    });
    If(
        IsBlank(new_Record),
        Notify(“Unable to add record”, NotificationType.Error);
        Set(ReturnStatus, “Failure”),
        Notify(“Successfully added record”, NotificationType.Success);
        Set(ReturnStatus, “Success”)
    )
)

Calling a custom function and accessing its return values:

If(
    AddRecord({
        ID: 1,
        RecordName: “Hamburger”
    }).ReturnStatus = “Success”,
    Notify(“Happy Face!”, NotificationType.Success)
)

This is a relatively simple example but the benefits of encapsulating more complex, repeated operations in a function are pretty clear.

Status: New