XConnect.Operations.FacetOperationException: AlreadyExists

Had an issue with contact interactions not saving as expected and looked in Xconnect instance log file and saw this error message.

Sitecore.XConnect.Operations.FacetOperationException: Operation #0, AlreadyExists, Contact <contactId>, Classification

We have some custom code that saves and updates contacts so I started digging to find the culprit.

I stumbled upon this know issue in Sitecore 9.x, which I was using, that is fixed in Siteore 10.
https://support.sitecore.com/kb?id=kb_article_view&amp;sysparm_article=KB0397292

But before I installed the package from the Known Issue, I wanted to make sure my custom code was solid and that I wasn’t making any errors there myself. Spoiler, it was, so take a look in your custom code before (if any).

My issue was that I misunderstood the process of how contacts are retrieved with the xConnect API works. I thought I could just get a contact by some Facet, like an email, and then I would get that contact with all it’s Facets and values.

In my case I wanted to set the “Personal” Facet if it was null and then save it.
But I got the contact without the ExpandOptions{PersonalInformation.DefaultFacetKey… which meant that the Facet “Personal” was always null.
So when I submitted it, it threw an error because I was trying to add a Facet that already existed on the contact. With the updated ExpandOptions like below, I was now getting the correct Facets on the contact and it worked like a charm.

var identifier = new IdentifiedContactReference(Constants.EmailSource, email);
var contact = context.Get(identifier, new ExpandOptions(PersonalInformation.DefaultFacetKey, EmailAddressList.DefaultFacetKey));

if (contact.GetFacet<PersonalInformation>(PersonalInformation.DefaultFacetKey) == null)
{
    PersonalInformation personalInfoFacet = new PersonalInformation() {FirstName = firstname, LastName = lastname};
    context.SetFacet(existingContact, PersonalInformation.DefaultFacetKey, personalInfoFacet);
}

context.Submit();

Hope this helps bring some clarity as it did for me! 🙂

Sitecore xConnect error GetEntityOperation

This error message caught my eye on xConnect in Sitecore 9.3, which looked like this.

"[Error] Sitecore.XConnect.Operations.GetEntityOperation`1[Sitecore.XConnect.DeviceProfile]: Sitecore.Xdb.Collection.Failures.DataProviderException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception: The system cannot find the file specified"

This issue was because of Xdb had wrong ServerName in the [*.Collection.ShardMapManager] database.


The database and table “[*.Collection.ShardMapManager].[__ShardManagement].[ShardsGlobal]” that contains [ServerName] and [DatabaseName] columns. Replace with your ServerName and hopefully this will be the issue in your case.

Hope this helps if you get a similar error.