Sunday, March 13, 2016

Custom Auto-Post rule on Activity Feeds : Be notified on records you follow

In Microsoft Dynamics CRM 2013, Microsoft introduced the Out-Of-The-Box (OOB) Social Pane, with Notes, Activities and Posts, based on Activity Feeds. The behavior of Activity Feeds is just like Twitter, where users can follow or unfollow some records, mention them on post, and see all posts based on records they follow on their personal wall.

Unlike Twitter, Activity Feeds introduced some "Auto Post Rules" on a few of OOB entities ; on a particular event (let's say when a opportunity is won), a post is automatically created on the record. This feature is great because all the followers are automatically aware, by having a look at their wall, that this opportunity is closed.

Nevertheless, it has some drawbacks (I wouldn't write about it otherwise :) ). First, you cannot create an auto post rule manually (by going to Settings / Auto Post Rules / New for instance). You can only enable or disable existing ones. Second, Activity Feeds does not provide a way to notify (by email for instance) the followers of a record when a post is posted on it (like Yammer does).

As I had to provide this kind of functionality on a CRM 2013 project, I will introduce today a way of having the 2 features that are not included in the OOB Activity Feeds : the Custom Auto Post Rules (using no code, always great to fulfill requirements without code) and the Email Notifications to followers (with a little code, always good to fulfill requirements with a little of coding :) ).

So, we are going to enhance the Activity Feeds with the following example : a post is created each time an opportunity has been updated, and all the followers of the opportunity have to be notified by email. The post must contain the user that updated the opportunity.

This article is divided in two parts : Custom Auto Post Rule, where we are going to create the automatic creation of the post and Followers Email Notification, where we are going to notify, by email, all the followers of a record, each time a post (either auto or manual) is created.

Custom Auto Post Rule

To create an auto post, we are going to use the powerful functionality of workflows. First, we have to notice that each post used by the Activity Feeds engine is carried by the entity 'post'. So, if you create a record on this entity, using the Create step of the workflow with right values, well, you just created your automatic post. Let's see how it goes on our example :

  • Create a workflow on the Opportunity entity, named "Opportunity - Auto Post on update" :
This workflow is triggered automatically, when the field "Modified On" is modified. By using these settings, the workflow is triggered each time the opportunity is updated, that was our original goal.

The sequence is in fact really simple, it is just using a single step, that is the creation of a record on the Post entity. The properties of this step are as below :
Make sure that the Regarding field is set on the Opportunity record. This way, it will appear on the social pane of the record. Notice that if you want to enhance this rule, for example by creating a post on the account related to this opportunity, you can add another workflow step, and set the Regarding field to the account.
  • Let's have a look about what we have done so far. Activate the workflow and update an existing opportunity (modifying the Budget Amount) :
Before the update of Budget Amount

After the update of Budget Amount
You can see here that our post has been correctly created. Using this workflow, every opportunity will have a new post each time it is updated. The post text also indicate the user that modified the record, that was our (indeed quite simple) goal.

Let us discuss a little about this method. You can see that the creation of the post is in fact really simple, because it is basically only a "Create on Post entity" step in a workflow. If you have more complex rules to implement, the key point is to know if you can reach the event by using standard workflow.

For instance, if you want to create a post on a contact every time this contact has been added as a stakeholder to an opportunity, you can do this by creating a workflow on the Connection entity, triggered on Creation, that checks if the Connected From entity is Opportunity and Connected To Contact, and set the Regarding field of the post on Connected To field of the Connection record :

Workflow on the Connection Creation

Properties of the Creation Post step
And when a contact is added as a stakeholder on an opportunity, a post is created on the contact record :
Social Pane of the contact, after being added as a stakeholder
Finally, this kind of method is interesting because really quick to implement and using all the possibilities provided by the workflow engine, to reach a special event, with some conditions.

However, it still has 2 main drawbacks :
  • First, even if it is quite powerful, the workflows do not allow to reach all the available events provided by Dynamics CRM. For example, you cannot create a post (using this method) every time a contact is added as a member of a marketing list. If you want to create automatic rules for this kind of event, you must develop a custom plugin.
  • Second, you can see some posts contain hyperlinks, directing to the main form of a record. You can actually create custom posts with these kind of hyperlinks but here again, you have to write some code. See this post How to do Mentions with Activity Feeds, to go further on mentioning with Activity Feeds.

Followers Email Notification

If you are using or ever used Yammer, you might like the possibility of being notified on a post/group/subject you follow. Well, if you like it, you cannot do this with Activity Feeds, out-of-the-box. In fact, you cannot even do this using workflows, as workflows does not allow to perform an action on a 1-N relationship (sadly). To do this, you must add a custom plugin (using C#).

The key point here is to add a custom plugin on the Create message of the Post entity. This plugin will first retrieve the regarding record of the post, then retrieve all the followers of the regarding record, and finally send the post message in an email, to all of the followers.
Note : The following demonstration will use some C# code, based on the Dynamics CRM SDK. You should be comfortable with classes and methods provided by the SDK, and how to write a CRM plugin, and more generally on how to work with the SDK.

  1. Retrieve the regarding record 
  2. In this step, we are doing nothing more than retrieving the EntityReference associated with the post, with just a few basic plugin controls.
  3. Retrieving all the followers of the record
  4. At this stage, we need to retrieve records of the System User entity, so we need to use Query Expression, on the entity 'systemuser'. The key point is the construction of the LinkEntity, based on the entity PostFollow. In fact, the entity 'postfollow' is used by the Activity Feeds to link a user and a record (for example a contact), showing that this user is currently following the record. So, we are here just adding the link between SystemUser and PostFollow, on the field OwningUser. And to retrieve only users that follow the regarding record retrieved in step 1, we just add a condition (on the field RegardingObjectId of the PostFollow entity), to filter PostFollows of this record.
  5. Sending a basic email to the followers
This final step is divided in 3 little steps. First, we create here an activity party per follower, all stored in a List of Entity (all on the entity ActivityParty). It will be used as the recipients of the email notification. Second, we create the email record. In this example, I am just using the strictly necessary inputs : the recipients, constructed at the previous step, the subject and the description, that is the content of the post. You could add here more custom content in the email body (in fact custom HTML, with hyperlinks, colors, fonts, images, sender, adapted subject etc.) but this is out of this article scope. And the final step is, once it's created, to send the email, via the message SendEmailRequest provided by the SDK.

Our plugin is now done, we just have to build and register it. And of course, creating the step, in PostOperation of Create of a Post, as below :

And you are done ! Let's see as it works, by using the following scenario :
  • User1 and Boris T. are following the contact John Donahue.
  • User2 sets John Donahue as a stakeholder of an opportunity.
So basically, a post is automatically created (because of our Custom Auto Post Rule see above) and User1 and Boris T. are notified, by email, that John Donahue as been set as a stakeholder :

User2 add John Donahue as a stakeholder on an opportunity
An automatic post is added, on John Donahue
Automated notification, by email
From CRM, to all the followers

As mentioned before, the content of the email is not optimized, but you can, by enhancing the subject and the description of the email record, set an elaborate notifications engine.

Finally, combined with some strong auto-post rules, the method of Email Notification allows you to keep your users aware of data they care about.