Skip to main content

Notifications

Power Apps - Building 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:
  • BB-31072007-0 Profile Picture
    BB-31072007-0 76 on at
    Duplicate Detection
    This is working amazing now-It now works on recent items!  After testing numerous times, it would have to be the delegation, as it fails for records >2000.     I do see a delegation warning in the code, and I'm sorry I did not jump on that sooner.  Thanks again for all your insight and patience.  
  • Verified answer
    WarrenBelz Profile Picture
    WarrenBelz 145,422 on at
    Duplicate Detection
    What is strange about this is that the Filter is Delegable, so I discounted the possibility you may have this issue, however if the records you are after are in the newest 2,000 items (and your Data Row Limit is set to this)
    With(
       {
          _Dups: 
          ForAll(
             Filter(
                Gallery3.AllItems,
                Checkbox1.Value
             ) As _Data,
             LookUp(
                Sort(
                   BCP,
                   ID,
                   SortOrder.Decending
                ),
                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 ) 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
    BB-31072007-0 76 on at
    Duplicate Detection
    @WarrenBelz   I tested again against older records and it seems to be working correctly.  I think the LookUp was not looking past the 2000 record limit.  I didn't think of that since the label looked up the duplicates with no issues.     Anyway going to see how we can purge the records into another source and see if that fixes it.  So marked your solution as answer.   Thanks again for all your help.
  • WarrenBelz Profile Picture
    WarrenBelz 145,422 on at
    Duplicate Detection
    Is this now solved ? I am not sure what else to offer other than an honest opinion going back to the start that this has far more integrity if you check and update each record as it is added.
  • BB-31072007-0 Profile Picture
    BB-31072007-0 76 on at
    Duplicate Detection
    @WarrenBelz   Hi Warren,  I have complete confidence yours is working like expected.   I put the label in and it definitely shows "Duplicate" when I select locations and Date with duplicates.   But I can submit them with no problem-I get the "Success" message and then I check the data source and they are loaded.  I'll keep playing around with it.  It is SharePoint and just a date field with no time. Craziness.   Thank you for all your determination.    
  • WarrenBelz Profile Picture
    WarrenBelz 145,422 on at
    Duplicate Detection
    I have tested both the Collection and the With() statement code on my model (which has exactly the same names, types and structure as yours) and both work completely as expected. I have re-pasted the code directly from the working model in both sets below previously provided (the first one would be the better to use). Is your date field a date only (not date and time) and is your data source SharePoint ? Also put a Label in the Gallery with this - if it does not show duplicates selected, there is an issue with the criteria.
    If(
    CheckBox1.Value &&  
    !IsBlank(
    LookUp(
    BCP,
    UniqueID = TextInput2_1.Text && DateClosed = DataCardValue13_1.SelectedDate
    )
      ),
      "Duplicate"
    )


     
  • BB-31072007-0 Profile Picture
    BB-31072007-0 76 on at
    Duplicate Detection
    @WarrenBelz   It allows me to submit the same location and date over and over, whether I use a different browser tab or not.  So not sure what could be going on.... Pasted exactly like you have it:  
    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
       )
    )
     
  • WarrenBelz Profile Picture
    WarrenBelz 145,422 on at
    Duplicate Detection
    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    Buy me a coffee
  • BB-31072007-0 Profile Picture
    BB-31072007-0 76 on at
    Duplicate Detection
    @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
    WarrenBelz 145,422 on at
    Duplicate Detection
    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 ?

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,422

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,711

Leaderboard