Greetings!
Short Version: We are using PowerApps Portal and in our CRM we have multiple contacts sharing the same email id, however we do have a provision to identify the contacts that are related my application and can ensure that even though the contacts share emails with other contacts, they don't share the emails with any other contact related to our application.
I am looking for suggestions from folks who have encountered this scenario.
Elaborated Version:
Background: There was an application developed and moved to CRM online prod instance a year back. For some reason, this was not released to the user (Go live did not happen). I was roped in two months back to assess the application and enable the go-live.
Application uses D365 Sales module - Leads, Contacts, Accounts, Opportunities along with PowerApps Portals (Power Pages)
I have realized that the production environment is used by other applications, they have loaded millions of contact in the Prod environment. Since customer's other systems allow multiple contacts to have same email id, a lot of duplicate records were brought into our production environment.
On querying Prod DB, I found there are around 41000 email ids that are used by multiple contact, some of these emails are shared by as many as 7000+ contacts
We have a field in Contact "App Rec" that determines if that contact is related to our application or not.
The below contact records share the same email id

Our application should not treat them as duplicate because we have only one Contact record with App Recs = "Our App Record"

Scenario: When a new user registers in our PowerApps portal using an email id that does not have any entry in Azure AD B2C and there is no contact in CRM with that email id - the following thing happens
- An entry for that email id is created in Azure AD B2C
- A Contact is created in CRM with that email id.
- An external identity is mapped to this newly created contact and has the object id of the Azure entry.

Question: Do we have any control to customize the code that performs Step 3 [associates external identity to contact]?
Intention: If we can customize this code then I would like the code to perform
- When there is no CRM Contact with the same email id then – Create a new contact with App Recs = "Our App Record" and associate the external identity with this contact
- When there are multiple CRM Contacts with the same email id but none of them has App Recs = "Our App Record" then – Create a new contact with App Recs = "Our App Record" and associate the external identity with this contact
- When there are multiple CRM Contacts with the same email id but one of the Contact’s App Recs = "Our App Record" then – Associate the external identity with the contact having App Recs = “Our App Record”
What other challenges should I anticipate even if the above issue is fixed?
Thanks
Shyam