Get count of Attachments for Email in Dynamics CRM without code
Recently we had a requirement from our customer to show the number of attachments for each email in a view. The number needs to be reflected whenever the user refreshes the view.
Simple requirement right? And what is coming to your mind? Plugins? Workflows?
Well if you are thinking of the Plugins and workflows, I am sure you are not alone. In fact the first thing that came to my mind was the same. But we are in CRM 2016 right? And is there no other way to achieve this?
I brainstormed for sometime and then came up with the below solution. Believe me it is a simple solution without any code and I am pretty sure you would like it. No it is not rollup field. It is using the calculated field feature of Dynamics CRM.
Let us first look at the email entity in CRM. A quick glance at the email entity and you could see there is field already in the email entity called ‘Attachment Count’. Wow! Can’t we use this field. Unfortunately the answer is ‘No’. If you open this field, it is a non-editable field and it won’t appear in your view and advanced find as well. Nor you can use it in roll-up fields or calculated fields editor. Sad isn’t it.
I was pretty sure that this field could give us the count of attachments for an email. But how to get around the limitation. Well you have CRM 2016 after all which is just awesome. All we consultants need to do is to explore it a bit to make it work for all situations
To start with I created a solution with the email entity in it. And then I created a dummy field called ‘Attachment Count Dummy’. Don’t worry I am just creating a dummy field here. I will delete it later when done. As of now just make sure you make the field as ‘whole number’ type.
Great. So we are done with our first step. Now we create our real field to store the attachments. I name it the same ‘Attachment Count’ of type whole number. But this I make it a calculated field.
I set it to be equal to ‘Attachment count dummy’ field. You may be asking why not set this field to the OOBattachmentcount field. Because as I mentioned before, the OOB attachmentcount field cannot be used in calculated field editor.
Save and close the editor. Include this calculated field in whichever views you want.
All set and done. Now comes the real trick. Publish everything and export the solution. Once the solution is exported, unzip it and you would see the contents. It should have a Formulas folder where CRM stores all the calculated field formulas.
Open the folder and then open the formula of the ‘Attachment Count’ calculated field .xaml file in a xml editor of your choice. Replace the dummy field name with the name ‘attachmentcount’. Note this is the most important step.
Save and close the file. Now zip the contents of the solution back and import the solution again in your CRM environment. Once the publish is completed, open advanced find and then check for the views
I open my email, attach one more attachment and as soon as I refresh the view, I could see the count being incremented.
Are we missing something here? Yes. You remember we created an extra dummy field. Go ahead and delete it. It won’t do any harm if you delete it. But you would be left with an unnecessary field.
Wonderful isn’t it? I hope you liked the idea