Power Apps  

How to Fix Patch Not Working & Duplicate Records in Power Apps

Introduction

While working with Power Apps and SharePoint, developers often face unexpected issues when saving data using the Patch() function.

Common problems include:

  • Duplicate records getting created

  • Records not updating correctly

  • Patch silently failing

  • Wrong record getting modified

  • Form submission working but Patch not working

In this article, we will explore a real-world scenario where Patch() creates duplicate records instead of updating the existing record — and how to fix it properly using best practices.

Real-World Scenario

You have a SharePoint list named:

ProjectRequests

Columns:

  • ID (System generated)

  • Title (Single line of text)

  • EmployeeID (Single line of text)

  • ProjectName (Single line of text)

  • Status (Choice)

  • RequestDate (Date)

You want to:

  • Create a new record if it does not exist

  • Update the record if it already exists

The Problem – Duplicate Records Getting Created

You wrote this formula:

Patch(
    ProjectRequests,
    Defaults(ProjectRequests),
    {
        Title: TextInput_Title.Text,
        EmployeeID: TextInput_EmpID.Text,
        ProjectName: TextInput_Project.Text,
        Status: Dropdown_Status.Selected.Value,
        RequestDate: Today()
    }
)

⚠️ Issue:
Every time the user clicks Save, a new record is created, even when updating.

Why?

Because Defaults(ProjectRequests) always creates a new record.

Understanding Patch Behavior

Patch() requires:

Patch(DataSource, BaseRecord, ChangeRecord)

If BaseRecord is:

  • Defaults(DataSource) → creates new record

  • Gallery.Selected → updates existing record

  • LookUp() → updates matched record

Correct Approach – Update If Exists, Otherwise Create

Step 1: Check If Record Exists

We assume uniqueness based on:

EmployeeID + ProjectName

Use:

Set(
    varExistingRecord,
    LookUp(
        ProjectRequests,
        EmployeeID = TextInput_EmpID.Text &&
        ProjectName = TextInput_Project.Text
    )
);

Step 2: Conditional Patch

Now write:

If(
    IsBlank(varExistingRecord),
    
    Patch(
        ProjectRequests,
        Defaults(ProjectRequests),
        {
            Title: TextInput_Title.Text,
            EmployeeID: TextInput_EmpID.Text,
            ProjectName: TextInput_Project.Text,
            Status: Dropdown_Status.Selected.Value,
            RequestDate: Today()
        }
    ),
    
    Patch(
        ProjectRequests,
        varExistingRecord,
        {
            Title: TextInput_Title.Text,
            Status: Dropdown_Status.Selected.Value,
            RequestDate: Today()
        }
    )
);

Now:

  • If record exists → Update

  • If not → Create

✅ No duplicates
✅ Controlled logic
✅ Clean behavior

Handling Silent Patch Failures (Advanced Issue)

Sometimes Patch does not throw visible errors.

To handle this properly, use IfError().

Step 3: Add Error Handling

IfError(
    Patch(
        ProjectRequests,
        varExistingRecord,
        {
            Status: Dropdown_Status.Selected.Value
        }
    ),
    Notify("Something went wrong while saving.", NotificationType.Error),
    Notify("Record saved successfully.", NotificationType.Success)
);

This ensures:

  • User sees success message

  • Errors are captured properly

Step 4: Prevent Double Click Duplicate Submission

Another common issue is users clicking Save multiple times quickly.

Solution:

Add a loading variable

Set(varLoading, true);

Patch(
    ProjectRequests,
    varExistingRecord,
    {
        Status: Dropdown_Status.Selected.Value
    }
);

Set(varLoading, false);

Set button property:

DisplayMode = If(varLoading, DisplayMode.Disabled, DisplayMode.Edit)

This prevents duplicate clicks.

Step 5: Use Form Mode Properly (Alternative Professional Approach)

Instead of manual Patch logic, you can:

EditForm(Form1);
NewForm(Form1);
SubmitForm(Form1);

Use:

OnSuccess → Notify("Saved successfully")
OnFailure → Notify("Error occurred")

Forms automatically manage:

  • New vs Edit mode

  • Record binding

  • Error handling

Performance and Best Practices

✔ Always use LookUp() to identify update records

✔ Never use Defaults() when updating

✔ Avoid using ID manually for new records

✔ Use IfError() for production apps

✔ Disable Save button during submission

✔ Add unique column validation at SharePoint level

Before vs After Implementation

ScenarioResult
Using Defaults() alwaysDuplicate records
No LookUp logicWrong record update
No error handlingSilent failure
Controlled Patch logicStable and reliable

Output

After implementing this solution:

  • No duplicate records

  • Correct record updates

  • Controlled user experience

  • Error-safe production-ready logic

Conclusion

Handling Patch() properly in Power Apps is critical in enterprise applications.

The most common mistake developers make is always using Defaults() without checking whether a record already exists.

By implementing:

  • LookUp-based validation

  • Conditional Patch logic

  • Error handling

  • Click prevention logic

You can ensure stable, scalable, and production-ready Power Apps solutions.