Let’s assume we need to send an email or push notification as part of an engagement with contact. Once EXM for Sitecore 9 is released, i expect there will be an option to send triggered email. For now, we can create a custom action type “Send Promo Email”.
As mentioned before, Sitecore has great article that covers creating custom activity implementation. Documentation suggests we start by inheriting from IActivity interface that resides in Sitecore.Xdb.MarketingAutomation.Core.dll. Next, we need to implement Invoke() method that has IContactProcessingContext object as a parameter.
public override ActivityResult Invoke(IContactProcessingContext context)
IContactProcessingContext is very helpful to get contact and interaction data from activity context, i.e. the contact being evaluated by activity and interaction that resulted to activity or plan enrollment.
We will need contact’s preferred email address in order to send email and so we can make use of context.Contact facets in the following way.
EmailAddressList facet = context.Contact.GetFacet<EmailAddressList>(); if (facet == null || facet.PreferredEmail == null) return (ActivityResult)new Failure(Resources.TheEmailAddressListFacetHasNotBeenSetSuccessfully);
Failure activity result will cause the engine to retry our custom activity on the next “event/trigger”, hence the enrollment for the context contact will get stuck on current activity. Such activity result should only be used for catastrophic situations, and if you still want contact to move down the path, you can create additional activity path and move contact there by returning SuccessMove(“no-email”) activity result where “no-email” is alternative activity path.
Let’s leave the logic of SendEmail service out of scope and focus on Activity results where we use default path by returning SuccessMove() action result.
string email = facet.PreferredEmail.SmtpAddress; //instantiating email service without DI for simplicity var emailService = new EmailService(); if (!emailService.SendPromoEmail(email)) return (ActivityResult)new Failure("Failed to send promo email"); return (ActivityResult)new SuccessMove();
Note: You can make perfect use of Dependency Injection for your services as suggested by this article.
Note: Your contact may have custom facets associated. Be aware that only a certain list of facets is loaded by Sitecore automation engine to the context contact. Following the link, you can find additional information on loading contact facets. And you can always load additional data on demand.
Now we can create activity definition item. We create a new item from Activity Type template under /sitecore/system/Settings/Analytics/Marketing Automation/Activity Types
In the activity definition item we can set Implementation Type field to refer to our c# class. We will also need to classify our activity as Marketing Action so that it appears in the corresponding category of Marketing Automation Plan Editor Toolbox. Note that we have 0 parameters created and only one Default path. Basically, it means we don't have any parameters to be managed in the Action Type editor UI and there is only one exit possible from activity.
You can find full source code on GitHub
Browse to part 3 to learn how to create Activity Type UI and Editor.