Sitecore Update xDB Contact

Problem

We had a solution where we had to process any contacts that changed their details in the past 24 hours, for example changed their name, birthday, custom preferences, etc.

I expected I could get a list of all updated contacts (i.e. contacts where one of their facets had changed), but unfortunately, adding or updating facets does not change the contact’s Last Updated property?

According to the Sitecore support & documentation, this is not a bug, but the desired behavior?

Solution

The Contacts Last Updated property is updated, only if an identifier is added or removed.

The following code is a nasty work around that adds/removes an identifier; to force an update of the Last Updated property.

  private void TempWorkAroundToGetSitecoreToUpdateLastmodified(Contact contact, XConnectClient client)
	  {
      //when a contact facet is updated the contacts last modified is not updated?
      // see documentation WTF https://doc.sitecore.net/developers/xp/xconnect/xconnect-client-api/contacts/update-contacts.html

      // the only way to set the last modified for a contact, is to add or remove an identifier, so we have to toggle that
	    var identifierToRemove = contact.Identifiers.FirstOrDefault(x => x.Source == Constants.Source.FakeSource);
	    if (identifierToRemove != null)
	    {
	      client.RemoveContactIdentifier(contact, identifierToRemove);
      }
	    else
	    {
	      client.AddContactIdentifier(contact, new ContactIdentifier(Constants.Source.FakeSource, contact.Id.HasValue ? contact.Id.ToString() : Guid.NewGuid().ToString(), ContactIdentifierType.Known));
	    }
	  }

Each time a contact was updated, we called this code, to ensure it is possible to get a list of all contacts that have changed, within a given time span.

Hope this helps and enjoy the summer, Alan

4 thoughts on “Sitecore Update xDB Contact

  1. Peter Nazarov

    Hi Alan

    Thank you for a heads-up and the solution proposal. It is very useful!

    Some thoughts:

    Would it be feasible to remove “else” statement, in the code example above? Would there be any implication if you remove it, for example, performance penalty, etc?

    I’m coming from the perspective of the data integrity. The aim is to change the “Last Updated” property of the contact. Probably, there should not be other modifications made to the state of contact’s data record(s), however, the code above always change the state of “Identifiers”. It might be a good idea to avoid inconsistent modifications of the data record(s) and preserve data record(s) integrity.

    Reply
    1. Alan Coates Post author

      Unfortunately u have to add or remove to force the update, the if else is the toggle as if it all ready exists the last changed is not updated

      Reply
      1. Peter Nazarov

        Clarification: I meant to say remove “else” part from “if” statement but keep the inner statement from “else”.

        Then, also swap inner statements from “if” and “else” round, and inverse the condition. The idea just to trigger “Last Updated” value change but keep all other data fields/records state intact.

        if (identifierToRemove == null)
        {
        client.AddContactIdentifier(contact, new ContactIdentifier(Constants.Source.FakeSource, contact.Id.HasValue ? contact.Id.ToString() : Guid.NewGuid().ToString(), ContactIdentifierType.Known));
        }
        client.RemoveContactIdentifier(contact, identifierToRemove);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s