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 Detection
Power Apps
Answered

Duplicate Detection

(1) ShareShare
ReportReport
Posted on by 76
I have this code in the Visible property of Gallery that detects duplicates and will show Visible whenever a duplicate is detected.  
!IsBlank(
   LookUp(
      BCP,
      UniqueID = TextInput2_1.Text && DataCardValue13_1.SelectedDate = DateClosed && Checkbox1.Value=true
   ).UniqueID
)
 
The only time a duplicate detection fails is when two people are entering the same locations at around the same time.  I.e. If I open two browser tabs and enter same locations before submitting, and then submit it will allow both items to be submitted. 
I thought that by adding Refresh(BCP) at the top would prevent it, but to no avail.  Here is the code for Submit:  
Refresh(BCP);
If(DuplicateDetector.Visible=false,
If(Label5.Text<>"0" ,
    
   Notify(
      "Please fill out required fields in Red",
      NotificationType.Error,
      2000
   
),
    Patch(  UpdateContext({varShow:true});Select(Button9);
        BCP,
      
        ForAll(
            Filter(
                Gallery3.AllItems,
                Checkbox1.Value
              
         A bunch of Patch Code Here
            }
        )
    ) ;
 Select(BTNLoadData); Notify("Success! To see your entries click on My BCP Entries button");
),
Notify("Entry Already Exists for Location & Date",NotificationType.Error))

  
Is there a way to prevent duplicates in this scenario?   
Categories:
I have the same question (0)
  • Suggested answer
    WarrenBelz Profile Picture
    153,781 Most Valuable Professional on at
    I assume here you want the second user to detect that the first has entered data in the field before they have. This question has been asked many times before and you are always going to have a small "window" of time where almost simultaneous entry will not be detected. The size of this window will depend mainly on the data connection speed and the user's device performance. A suggestion below to do the duplicate test first and not via another calculation. You do not need the Refresh as this is querying directly the current connected data.
    If(
      Checkbox1.Value && !IsBlank( LookUp( BCP, UniqueID = TextInput2_1.Text && DateClosed = DataCardValue13_1.SelectedDate ).UniqueID ), Notify( "Entry Already Exists for Location & Date", NotificationType.Error ), Label5.Text <> "0", Notify( "Please fill out required fields in Red", NotificationType.Error, 2000 ), UpdateContext({varShow:true}); Select(Button9); Patch( BCP, ForAll( Filter( Gallery3.AllItems, Checkbox1.Value )
    ​​​​​​​ ), { A bunch of Patch Code Here } ); Select(BTNLoadData); Notify( "Success! To see your entries click on My BCP Entries button" ) )
     
    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    LinkedIn    Buy me a coffee
  • BB-31072007-0 Profile Picture
    76 on at
    Hi Warren again-you've helped me a bunch in the past. TY!   It definitely works better.  It works when duplicate location is the only one selected or the duplicate happens to be the last location selected.  This is in a gallery with checkboxes, so users can check all of the ones they want entered at once, and then submit.  So if there is already an entry the duplicate detector pops up in the gallery on each row.  In scenario mentioned, there would be no pop up in the gallery.  Then the code runs that you provided, and if the duplicate was entered last it will detect it or if it was the only one entered.   So is there a way to scan the whole gallery of items checked and run the detection that way?  
  • Suggested answer
    WarrenBelz Profile Picture
    153,781 Most Valuable Professional on at
    Looking at this again, you might want to consider a more direct approach to updating the data source as you do each record, as this will make the detection of duplicates much easier. One other issue with doing it at the end is that you need to be able to tell the user which record is duplicated.
    So if you had the DisplayMode of CheckBox1
    If(
       Label5.Text <> "0",
       DisplayMode.Disabled,
       DisplayMode.Edit
    )
    then OnCheck of CheckBox1, check that record for duplicates and write it to the data source if none.
    If(
       !IsBlank(
          LookUp(
             BCP,
             UniqueID = TextInput2_1.Text && 
             DateClosed = DataCardValue13_1.SelectedDate
          ).UniqueID
       ),
       Notify(
          "Entry Already Exists for Location & Date",
          NotificationType.Error
       ),
       UpdateContext({varShow:true});
       Select(Button9);
       Patch(  
          BCP,  
          ThisItem,
          {
             A bunch of Patch Code Here
          }
       );
       Select(BTNLoadData); 
       Notify(
          "Success! Record has been saved"
       )
    )
    If you still want to head down the bulk track, you can try this in conjunction with my first suggestion on the DisplayMode of the Checkbox.
    With(
       {
          _Dups: 
          ForAll(
             Filter(
                Gallery3.AllItems,
                Checkbox1.Value
             ) As _Data,
             LookUp(
                BCP,
                UniqueID = _Data.TextInput2_1.Text && DateClosed = _Data.DataCardValue13_1.SelectedDate
             )
          )
       },
       If(
          CountRows(_Dups) > 0,
          Notify(
             "Entry Already Exists for " & Concat(
                _Dups,
                UniqueID & " - " & Text(
                   DateClosed,
                   "dd/mm/yyyy"
                ),
                ", "
             ),
             NotificationType.Error
          )
       ),
          UpdateContext({varShow:true});
          Select(Button9);
          Patch(  
             BCP,  
             ForAll(
                Filter(
                   Gallery3.AllItems,
                   Checkbox1.Value
                )
             ),
             {
                A bunch of Patch Code Here
             }
          );
          Select(BTNLoadData); 
          Notify(
             "Success! To see your entries click on My BCP Entries button",
             NotificationType.Success
          )
       )
    )

     
    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    LinkedIn    Buy me a coffee
  • BB-31072007-0 Profile Picture
    76 on at
    Warren, I'm going with the bulk route.  This works!  I checked it with two open browser tabs.   In this section however, I'm getting UniqueID isn't recognized and DateClosed isn't recognized.   So in order to test I had to remove that section, but of course don't know which location is the duplicate.  If I can get this to work it will be perfect!  
    If(   
          CountRows(_Dups) > 0,
          Notify(
             "Entry Already Exists for " & 
             Concat(
                _Dups,
                UniqueID & " - " & Text(DateClosed, "dd/mm/yyyy"),
                ", "
             ),
             
    Here is the complete code: 
    With(
       {
          _Dups:
          ForAll(
             Filter(
                Gallery3.AllItems,
                Checkbox1.Value
             ) As _Data,
             Filter(
                BCP,
                UniqueID = _Data.TextInput2_1.Text && 
                DateClosed = _Data.DataCardValue13_1.SelectedDate
             )
          )
       },
       If(   
          CountRows(_Dups) > 0,
          Notify(
             "Entry Already Exists for " 
             
             ),
         
          
      
              Patch(  UpdateContext({varShow:true});Select(Button9);
            BCP,
          
            ForAll(
                Filter(
                    Gallery3.AllItems,
                    Checkbox1.Value
                  
                 
                ) As _Data,
                
                {
                   Title: _Data.TextInputMarket.Text,
                   Address: _Data.TextInputAddress.Text,
                    City: _Data.TextInputCity.Text,
                    State: _Data.TextInputState.Text,
                    LocationConcat: _Data.DataCardValue17_1.Text,
                    Time:_Data.TimeDD.Selected,
                    DateClosed: _Data.DataCardValue13_1.SelectedDate,
                    Status: _Data.StatusCardValue16_2.Selected,
                    Reason: _Data.DataCardValue26_1.Text,
                    UniqueID:_Data.TextInput2_1.Text,
                    ISE:_Data.TextInputISE.Text,
                    Latitude:_Data.TextInputLatitude.Text,
                    Longitude:_Data.TextInputLongitude.Text, 
                    Dealer:_Data.TextInputDealer.Text,
                    AD:_Data.CBAD.Selected, 
                    DIR:_Data.CBDIR.Selected,
                    SrSpecialistSalesProgramExecution:_Data.CBSPE.Selected
                }
            )
        ) ;
    
          Select(BTNLoadData); 
          Notify(
             "Success! To see your entries click on My BCP Entries button",
             NotificationType.Success
          )
       )
    )
     
  • BB-31072007-0 Profile Picture
    76 on at
    I changed that part to this: 
      If(   
          CountRows(_Dups) > 0,
          Notify(
             "Entry Already Exists for " & 
             Concat(
                _Dups,
                TextInput2_1.Text & " - " & Text(DataCardValue13_1.SelectedDate, "dd/mm/yyyy"),
                ", "
             ),
             NotificationType.Error
          ),
    What happens is that the error pops up on the screen, but it displays the last location selected in the checkbox, so it gives the wrong UniqueID of which location was the duplicate.  If just one location is selected it works fine.  It still stops the duplicate which is the main thing.  Thanks for all your help with this.  
  • WarrenBelz Profile Picture
    153,781 Most Valuable Professional on at
    Firstly, please @Tag me in your responses - I am not getting notifications otherwise.
    They are the two file names from your supplied code which are from the data source BCP, so
    Filter(
       BCP,
       UniqueID = _Data.TextInput2_1.Text && 
       DateClosed = _Data.DataCardValue13_1.SelectedDate
    )
    should have these fields in the variable _Dups. Your modified code will not work as it will simply select the last set of controls in the gallery (it needs the actual field names in the Variable as they are coming from BCP, not Gallery3 ).
    Try with a collection, that way you can have a look what is in it after the code is run. I will go back a step in efficiency here as I cannot currently test why they are not there - I generally avoid using "one-use" Collections or Variables.
    I am also wondering why you have those two actions inside your Patch at the start - they are separate functions.
    Clear(colDups);
    ForAll(
       Filter(
          Gallery3.AllItems,
          Checkbox1.Value
       ) As _Data,
       Collect(
          colDups,
          LookUp(
             BCP,
             UniqueID = _Data.TextInput2_1.Text && DateClosed = _Data.DataCardValue13_1.SelectedDate
          )
       )
    );
    If(
       CountRows(colDups) > 0,
       Notify(
          "Entry Already Exists for " & Concat(
             colDups,
             UniqueID & " - " & Text(
                DateClosed,
                "dd/mm/yyyy"
             ),
             ", "
          ),
          NotificationType.Error
       ), UpdateContext({varShow:true}); Select(Button9); Patch( BCP, ForAll( Filter( Gallery3.AllItems, Checkbox1.Value ) As _Data, { Title: _Data.TextInputMarket.Text, Address: _Data.TextInputAddress.Text, City: _Data.TextInputCity.Text, State: _Data.TextInputState.Text, LocationConcat: _Data.DataCardValue17_1.Text, Time: _Data.TimeDD.Selected, DateClosed: _Data.DataCardValue13_1.SelectedDate, Status: _Data.StatusCardValue16_2.Selected, Reason: _Data.DataCardValue26_1.Text, UniqueID: _Data.TextInput2_1.Text, ISE: _Data.TextInputISE.Text, Latitude: _Data.TextInputLatitude.Text, Longitude: _Data.TextInputLongitude.Text, Dealer: _Data.TextInputDealer.Text, AD: _Data.CBAD.Selected, DIR: _Data.CBDIR.Selected, SrSpecialistSalesProgramExecution: _Data.CBSPE.Selected } ) ); Select(BTNLoadData); Notify( "Success! To see your entries click on My BCP Entries button", NotificationType.Success ) )
     
    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    LinkedIn    Buy me a coffee
  • BB-31072007-0 Profile Picture
    76 on at
    @WarrenBelz  Thank you for your latest reply.  The latest code doesn't have any errors.  But some reason it doesn't detect the duplicates like the previous code did.  I'm sorry!      
  • WarrenBelz Profile Picture
    153,781 Most Valuable Professional on at
    Is there anything in colDups after you have run it ?
    If the first code worked, the second one should as I am simply creating a collection instead of storing the Table in a With() Variable. So if you just run this with some duplicates selected
    Clear(colDups);
    ForAll(
       Filter(
          Gallery3.AllItems,
          Checkbox1.Value
       ) As _Data,
       Collect(
          colDups,
          LookUp(
             BCP,
             UniqueID = _Data.TextInput2_1.Text && DateClosed = _Data.DataCardValue13_1.SelectedDate
          )
       )
    );
    do you have anything in the Collection ?
  • BB-31072007-0 Profile Picture
    76 on at
    @WarrenBelz   It says "We didn't find any data". I only put that code in the OnSelect of the Submit button.   That code where I removed the Concat part-I ran it again and the popup with "Duplicate Exists" pops up even when there are no duplicates. It pops up every time.    I'm sorry man, but that wasn't working like I thought.  
  • WarrenBelz Profile Picture
    153,781 Most Valuable Professional on at
    I have done some testing when I had time to build a model - please see amended item in blue. It works here as expected.
     
    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    LinkedIn   

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!

Leaderboard > Power Apps

#1
Haque Profile Picture

Haque 88

#2
WarrenBelz Profile Picture

WarrenBelz 85 Most Valuable Professional

#3
Valantis Profile Picture

Valantis 45

Last 30 days Overall leaderboard