Tuesday, 8 March 2016

Auto Follow and Unfollow records in Dynamics CRM

It’s been a very long time since I wrote my last blog. Hence, I’m really excited while writing this post.

One of our customers has a requirement to see the POSTS for opportunity records at “What’s New” section of the “Sales Activity Social Dashboard”. This functionality is required only for a leading user, for a CEO of their organisation. Ideally, CRM populates this “What’s New” section for Opportunity entity when an opportunity gets created, on stage change, and opportunity gets closed as lost or won. However, it doesn’t populate it with the comment(s), which are manually inserted by a user on the record wall of the opportunity record. If we would like to see such manual comments, the easiest way is to follow that record. In Dynamics CRM 2016RM, we have a provision to ‘follow’ and ‘unfollow’ records manually from the Record Wall and also we can do bulk ‘follow’ and ‘unfollow’ records from an entity’s list view/landing page OR Advanced Find. But, the customer doesn’t want their CEO to do it manually.

 

To make it automatic, I decided to create a workflow for this.

Auto-Following an Entity via Workflow

Go to Settings -> Processes -> New in order to create a new workflow.

Enter Process name and choose Entity = Opportunity, Category = Workflow.

image

On a next screen, configure the things as displayed in following screen print.

image

Click the “Set Properties” button as highlighted in the above screen print, select the current opportunity record in “Regarding” field and a user in Owner field, who is to be displayed the post comments on his/her personal wall.

image

Once you save and activate the workflow, the new records are now automatically followed by CEO and the posts, manual comments should be displayed on personal wall.

NOTE: Dynamics CRM does not allow a user to follow more than 500 records.

Due to this limitation, I suggested the customer to remove these records when the particular opportunity record gets closed.

The OOB CRM process (workflow) does not allow us to delete such FOLLOW entity record.

Hence, I managed the entire functionality using plug-ins to ‘follow’ and ‘unfollow’ opportunity records. For this, I wrote two plug-ins,

1. Opportunity entity, post create message.

//Unique User ID - for whom auto-follow needs to be enabled
//Instead of hardcoding this, it can be fetched using User Name.
Guid userid = new Guid(“USER GUID”);
 
//Opportunity ID - for which auto-follow needs to be enabled
//It can be referred from the plug-in’s context
Guid opportunityid = new Guid(“RECORD GUID ”);
 
Entity postFollow = new Entity(“postfollow”);
postfollow.Attributes[“regardingobjectid”] = new EntityReference(“opportunity”, opportunityid);
postfollow.Attributes[“ownerid”] = new EntityReference(“systemuser”, userid);
Guid postFollowId = service.Create(postFollow);

2. Opportunity entity, post close message.

//Unique User ID - for whom auto-follow needs to be enabled
//Instead of hardcoding this, it can be fetched using User Name.
Guid userid = new Guid(“USER GUID”);
 
//Opportunity ID - for which auto-follow needs to be enabled
//It can be referred from the plug-in’s context
Guid closedopportunityid = new Guid(“RECORD GUID ”);
                   
QueryExpression queryExpression  = new QueryExpression(“postfollow”);
queryExpression.Criteria.AddCondition(new ConditionExpression(“ownerid”,ConditionOperator.Equal,userid));
queryExpression.Criteria.AddCondition(new ConditionExpression(“regardingobjectid”, ConditionOperator.Equal, closedopportunityid));
 
EntityCollection postFollowCollection = service.RetrieveMultiple(queryExpression);
foreach (Entity postFollow in postFollowCollection.Entities)
{
service.Delete(postFollow.LogicalName,postFollow.Id);
}

With the help of these two plug-ins, the CEO can now see the manual posts of the opportunity records on his personal wall. That’s it.

Thank you for reading this post.

Happy CRMing…

 


A simple and quick way to insert data into Many-to-Many relationship in Dynamics CRM is the Drag and drop listbox.

http://www.inkeysolutions.com/DynamicCRMDragAndDropListBox.html