web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Duplicate records addi...
Power Apps
Answered

Duplicate records adding from collection to database

(0) ShareShare
ReportReport
Posted on by 50

Hi, 

 

On click of save button, I updated data to collection using patch and again updated the collection to database using patch function.

Next time, when I again add another row, the first row is also adding to database.

 

This is causing duplicates in the database. Please help me with this

Categories:
I have the same question (0)
  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    @vakula 

    Please show the Patch() functions that you are using for the collection and the datasource.

  • Vakula Profile Picture
    50 on at

    I am first checking if the data already exists in the collection, then adding.

    Note: I have reduced the columns here, but I have almost 10 Columns.

     

    Set(
    varExist,
    LookUp(
    MainRules,
    ItemPoolName = ComboBox_ItemPoolName.Selected.Result && StartDate = ComboBox_StartDate.Selected.Value && Percentage = TextInput_Percentage.Text
    )
    );
    If(
    !IsBlank(varExist),
    Notify(
    "Cannot Add",
    NotificationType.Error
    ),
    If(
    !IsBlank(
    Patch(
    MainRules,
    Defaults(MainRules),
    {
    ItemPoolName: ComboBox_ItemPoolName.Selected.Result,
    StartDate: ComboBox_StartDate.Selected.Value,
    Percentage: TextInput_Percentage.Text,
    }
    )
    ),

    If(
    !IsBlank(
    Patch(
    '[dbo].[RulesData]',
    DropColumns(
    MainRules,
    "RowNumber"
    )
    );
    ),
    Notify(
    "successful",
    NotificationType.Success
    ),
    Notify(
    "Error",
    NotificationType.Error
    )
    )
    )
    )
    );

  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    Hi @vakula ,

    Try this instead in your button's OnSelect property.   It assumes that your patch works as written.

     

    With({varexist:!IsBlank(
     LookUp(
     ColMain, ItemPoolName = ComboBox_ItemPoolName.Selected.Result 
     && 
     StartDate = ComboBox_StartDate.Selected.Value && 
     Percentage = TextInput_Percentage.Text
     )
     )
     },
     
     If(varexist, Notify("Cannot Add",NotificationType.Error),
     Patch(
     '[dbo].[RulesData]',DropColumns(
     MainRules, "RowNumber"
     )
     )
     )
    );
    If(
     IsEmpty(
     Errors(
     '[dbo].[RulesData]',Patch(
     '[dbo].[RulesData]',DropColumns(
     MainRules, 
     "RowNumber"
     )
     )
     )
     ),
     Notify("Patch Successfully",NotificationType.Success),
     Notify("There is an error with your Patch function",NotificationType.Error)
    ) 

     

  • Vakula Profile Picture
    50 on at

    But, In this case, collection will not be updated right?

  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    @vakula 

    I missed that.  I think I may have screwed up in copying your formula.  I'll start over and check it.  In principle, I would separate out the patch to the collection and the patch to the datasource and also separate out the error check.  

  • Vakula Profile Picture
    50 on at

    Should I patch it at the last of the code? from database to Collection?

  • Vakula Profile Picture
    50 on at

    But with your approach, I changed my code. It seems to be working.

     

    Here is what I did. First checked for duplicates using lookup, Then If no duplicates, then inserted data directly into database and then update my collection with database data.

     

    Earlier, I was updating the collection first and then inserting the data into database, that was inserting duplicates.

  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    @vakula 

    Great!  I may have screwed up but at least I helped get us on the right track.  I am told that even a blind squirrel can sometimes find a nut!Smilegif.gif

     

  • Vakula Profile Picture
    50 on at

    But that's causing me another problem. After updating the collection from database, the form is clearing out all the values... 🙂

  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    @vakula 

    Ack! you never mentioned a form.  The solution would be completely different if you are submitting a form and recreating a collection.  In that case I would have just used SubmitForm() instead of patching the datasource and then recreated the collection with a ClearCollect(). Your validation could be built into the form's OnSuccess and OnFailure properties. 

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 530

#2
WarrenBelz Profile Picture

WarrenBelz 459 Most Valuable Professional

#3
Haque Profile Picture

Haque 314

Last 30 days Overall leaderboard