Where did my custom facets go in Sitecore?


Hello good people, I hope you are well out there and enjoying the beautiful spring and all the cool stuff that Sitecore has to offer us 🙂

This will be another short post about some issues I had and hope it will help you next time you are in a similar situation.

Recently, I encountered a strange behavior where SOME of my custom facets simply disappeared. It happened after a merge(identifying contact).

I thought it had to do with the merge and somehow the custom facets were not copied to the “SurvivingContact”. So I hijacked the MergeContactProcessor pipeline (it’s the one being called when a contact is being identified)

public class MergeContactFacets : MergeContactProcessor
{

	public override void Process(MergeContactArgs args)
	{
		Assert.ArgumentNotNull((object)args, "args");

		//The facet/s from the old one, check if it exist...
		IFacet source = args.DyingContact.Facets["SomeFacetName"];

		//This is the new contact that the facets will be copied to  
		//args.SurvivingContact		
	}

}

To my surprise, some of my facets where not in the DyingContact. Somehow they disappeared before the merge, that was indeed weird.

After tons and tons of hair pulling, I finally found the issue.

During the request(controller action) I did several saves/updates and after each save/update I called:
SaveAndReleaseContact(contact);
FlushContactToXdb(contact);
*From the ContactManager

They where all saved to the contact in mongodb, I could even view them in Robo 3T.

What happened was that the first time the methods (above) were called, the facet/s remained. After that nothing was “registered”(or what we should call it).
So when it was time to do a merge(identifying the contact), the mergeprocessor only had the custom facets(in the “DyingContact”) from the first method call. The remaining facets were gone (even if they where in the mongodb).

Lessons learned, be careful with SaveAndReleaseContact and FlushContactToXdb.
Only call them once and preferably at the end of the request.

(This happened in Sitecore 8.2)

That’s all for now folks 🙂

Advertisements

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