Skip to main content

Notifications

Power Apps - Power Apps Pro Dev & ISV
Answered

Power Apps - If statements for buttons

Posted on 1 Dec 2024 14:38:17 by

I have some issues configuring a button in my app.

I have a "Save button" that needs to check for conditions before allowing the user to save.
I have tried different codes, but the issue seems to be that it is not able to read in real-time whether the user has indeed met the conditions when trying to save. For example, if the user has chosen a certain value in a dropdown (like "Standard"), the message "Please write a justification..." still appears. The same issue happens for all the fields the user is supposed to fill out. I have also tried different options, such as checking the length of strings, using different functions like IsEmpty, IsBlank, and =Blank().

I have also tried different structures in the code (see two examples below):


Example 1

// Initialize a flag to track validation status
Set(isValid; true);
// Check if Dropdown is selected (cannot be empty)
If(
    IsBlank(Dropdown.Selected.Value) Or Dropdown.Selected.Value = "";
    Notify("Please select an option before saving"; NotificationType.Error);
    Set(isValid; false) // Set flag to false if condition fails
);
// Check if Dropdown is a specific value and TextInput is blank
If(
    (Dropdown.Selected.Value = "SpecificValue" Or Dropdown.Selected.Value = "AnotherValue") And IsBlank(TextInput.Text);
    Notify("For selected options, please write a justification in the comment section before saving"; NotificationType.Error);
    Set(isValid; false) // Set flag to false if condition fails
);
// Check if at least one other field is selected
If(
    IsBlank(Dropdown1.Selected.Value) And 
    IsBlank(Dropdown2.Selected.Value);
    Notify("Please select at least one option before saving"; NotificationType.Error);
    Set(isValid; false) // Set flag to false if condition fails
);
// Now that we've validated, check if the flag is still true
If(
    isValid;
    Patch(
        DataSource;
        LookUp(DataSource; ID = CustomerID.Text);
        {
            Dropdown: Dropdown.Selected.Value;
            Dropdown1: Dropdown1.Selected.Value;
            Dropdown2: Dropdown2.Selected.Value;
            TextInput: TextInput.Text;
            Field2: Field2.Text;
            Timestamp: Timestamp.Text
        }
    );
    Notify("Data successfully saved"; NotificationType.Success);
    // Reset fields after submission
    Reset(CustomerID);
    Reset(Dropdown);
    Reset(Dropdown1);
    Reset(Dropdown2);
    Reset(TextInput);
    Reset(Field2) // Reset additional fields
)
 
Example 2
If(
    // Check if Dropdown is empty (no value selected)
    IsBlank(Dropdown.Selected.Value) Or Dropdown.Selected.Value = ""; 
    Notify("Please select an option before saving"; NotificationType.Error);
    false; // Stop execution if Dropdown is not selected
    // Check if Dropdown is specific value and TextInput is blank
    (Dropdown.Selected.Value = "SpecificValue" Or Dropdown.Selected.Value = "AnotherValue") And IsBlank(TextInput.Text);
    Notify("For specific options, please write a justification in the comment section before saving"; NotificationType.Error);
    false; // Stop execution if TextInput is empty for specific options
    // Check if no other fields are selected (all Dropdowns are empty)
    IsBlank(Dropdown1.Selected.Value) And 
    IsBlank(Dropdown2.Selected.Value);
    Notify("Please select at least one option before saving"; NotificationType.Error);
    false; // Stop execution if no Dropdown is selected
    // Check if Field2 text field is empty
    IsBlank(Field2.Text);
    Notify("Please provide the 'Field2' field before saving"; NotificationType.Error);
    false; // Stop execution if Field2 is empty
    // If all conditions pass, return true to execute Patch
    true
);
// Only run Patch if all validations pass
If(
    true; // Proceed if all conditions are met
    Patch(
        DataSource;
        LookUp(DataSource; ID = CustomerID.Text);
        {
            Dropdown: Dropdown.Selected.Value;
            Dropdown1: Dropdown1.Selected.Value;
            Dropdown2: Dropdown2.Selected.Value;
            TextInput: TextInput.Text;
            Field2: Field2.Text;  // Updated to reflect correct field name
            Timestamp: Timestamp.Text
        }
    );
    // Notify success after Patch operation
    Notify("Data successfully saved"; NotificationType.Success);
    // Reset fields after submission
    Reset(CustomerID);
    Reset(Dropdown);
    Reset(Dropdown1);
    Reset(Dropdown2);
    Reset(TextInput);
    Reset(Field2);  // Reset Field2 field
)
 
  • Verified answer
    WarrenBelz Profile Picture
    WarrenBelz 143,202 on 01 Dec 2024 at 21:00:44
    Power Apps - If statements for buttons
    The first fundamental issue I see (I assume you are using European syntax) is that you are using ; everywhere instead of a function separator ;; where this is required. Firstly try this
    UpdateContext({isValid: true});;
    If(
       Len(Dropdown.Selected.Value) = 0;
       Notify(
          "Please select an option before saving"; 
          NotificationType.Error
       );;
       UpdateContext({isValid: false});
       (
          Dropdown.Selected.Value = "SpecificValue" Or 
          Dropdown.Selected.Value = "AnotherValue"
       ) And Len(TextInput.Text) = 0;
       Notify(
          "For selected options, please write a justification in the comment section before saving"; 
          NotificationType.Error
       );;
       UpdateContext({isValid: false});
       Len(Dropdown1.Selected.Value) = 0 And 
       Len(Dropdown2.Selected.Value) = 0;
       Notify(
          "Please select at least one option before saving"; 
          NotificationType.Error
       );;
       UpdateContext({isValid: false})
    );;
    If(
       isValid;
       Patch(
          DataSource;
          {
             ID: Value(CustomerID.Text);
             Dropdown: Dropdown.Selected.Value;
             Dropdown1: Dropdown1.Selected.Value;
             Dropdown2: Dropdown2.Selected.Value;
             TextInput: TextInput.Text;
             Field2: Field2.Text;
             Timestamp: Timestamp.Text
          }
       );;
       Notify(
          "Data successfully saved"; 
          NotificationType.Success
       );;
       Reset(CustomerID);;
       Reset(Dropdown);;
       Reset(Dropdown1);;
       Reset(Dropdown2);;
       Reset(TextInput);;
       Reset(Field2)
    )​​​​​​​

    Please click Does this answer your question 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 a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    Buy me a coffee

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,202

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,877

Leaderboard