SC9 modifying same facet twice during the same visit

Hi.

 

I have some troubles with Sitecore 9.

 

On form submit i write to FacetA.PropertyA.

 

Later during the same visit, i write to FacetA.PropertyB.

 

Each time I get this error:

"Operation #0, AlreadyExists, Contact {b8ae4c31-f187-0000-0000-0548ebbca8e0}, FacetA"

 

I use same method as stated here, and each XConnectClient has it's own Submit after i modified the data.

 

 

I tried a lot of different stuff, but no success so far. 

 

It works fine if i just submit FacetA.PropertyA. But it seems like there is a conflict, because i modify the same facet (although different property) twice, during the same visit.

  • In reply to Kasper Kristensen:

    Ok, so the error message implies that you are trying to add a facet to a contact, but the facet already exists for the contact. For that to happen, "personal != null" is false. So it seems your code is not finding the facet for the contact. There are a few reasons that might happen, but I would suspect it has something to do with the way you are getting your contact from xConnect.

    Here's an example using ContactExpandOptions. Can you try it and see it works for you?

    if (!Tracker.Current.Contact.IsNew)
    {
        using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
        {
            var contactIdentifier = Tracker.Current.Contact.Identifiers.FirstOrDefault();
            var existingContact = client.Get<Contact>(new IdentifiedContactReference(contactIdentifier.Source, contactIdentifier.Identifier), new ContactExpandOptions(PersonalInformation.DefaultFacetKey));
    
            if (existingContact != null)
            {
                var personalFacet = existingContact.GetFacet<PersonalInformation>() ?? new PersonalInformation();
                personalFacet.FirstName = model.FirstName;
                personalFacet.LastName = model.LastName;
                client.SetFacet(existingContact, PersonalInformation.DefaultFacetKey, personalFacet);
                client.Submit();
            }
        }
    }

  • In reply to Kasper Kristensen:

    Did you try this method for getting the current contact?

    var contactIdentifier = Tracker.Current.Contact.Identifiers.FirstOrDefault();
    var existingContact = client.Get<Contact>(new IdentifiedContactReference(contactIdentifier.Source, contactIdentifier.Identifier), new ContactExpandOptions(PersonalInformation.DefaultFacetKey));