Thursday 31 March 2016

Intercept Column/Field Value in CRM View using RetrieveMultiple Plugin C#

In the previous post, I explained about how to modify the lookup field display using Plugin in CRM.

Now, I just want to show you how to modify also other column and I give a use case here is to display the column as Calculated Field.

Well, in CRM 2015 and above Microsoft introduced 2 new types of field: Calculated and Roll-up field, but what if your organization still using earlier version and you already created a field without using that type (do you care to re-create again?) or you want user not to always refresh it.

So, here I want to give you use case in simple scenario, that is to display “Age” field dynamically based on Birthdate of Contact versus current date.

Use Case

Now you have a custom field: Age, as Integer or Whole Number, but all blank.

You have choice: Always update this using batch job that will run everyday or you show as a report dynamically.

So, this is the combination of those choices, you do not need to create report, but you just need to intercept the Plugin to show the data that you want.

So here is before you apply the plugin:

*Age is blank
image

The Code

And here is the code:

 public void Execute(IServiceProvider serviceProvider)
        {
            #region must to have

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            // Create service with context of current user
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            //create tracing service
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            #endregion

            //this is to modify the Age column

            if (context.OutputParameters.Contains("BusinessEntityCollection"))
            {
                var retrievedResult = (EntityCollection)context.OutputParameters["BusinessEntityCollection"];
                foreach (Entity entity in retrievedResult.Entities)
                {                 
                    if (entity.Contains("birthdate"))
                    {
                        var birthday = entity.GetAttributeValue<DateTime>("birthdate");
                        int age = CalculateAgeYear(birthday);
                        //modify the Age field
                    entity.Attributes["pa_age"] = age;
                    }
                    
                }
            }
        }

You can modify any field so long you put the correct type of object as the value based on the each field type

Result

And here is the result

image

Yes, you can display this in the view.

What if you want to display in the form?
You need to tweak the plugin and use the “Retrieve” message, instead.

Or you can use this as well
http://stackoverflow.com/questions/15048688/dynamics-crm-2011-plugin-retrieve-and-retrieve-multiple

*Thanks Pedro, my friend.

Which Retrieve can also be used for form and also RetrieveMultiple also.

Well, this is not only for one field, is can be for many fields..Including lookup that I explained in my other previous post.

And the good thing also you can display in any view so long the field is exist.

But the disadvantage is you cannot use this as keyword to search or filter, because it is actually not stored in the database.

Example:

Active Contacts View

image

So by using this capability, you can extend this for example to display the combination of 1 to N records!

For example: Total Number of related cases, total dollar of Revenue (from Opportunity), total child Contacts, total number of days of open cases, any aging fields, any rollup fiel as well, and so on..

And you do not even need plugin or workflow to always update this field everytine got any changes in the source for calculated field or any new or removed Associated records changes as well, or creating batch job or recreating your field to be using CRM 2015 rollup and calculated fields..Amazing right..

Hope this helps!
Thanks

4 comments:

  1. I see that you are using the RetrieveMultiple plugin message, but in terms of Performance, how bad is the 'RetrieveMultiple' plugin message, since you are retrieving all the items in the Output Parameters and modifying a value in each of them.

    ReplyDelete
  2. Suppose user has not added the "Age" field on the columns (view columns), then there is no need to run this logic. But with your current code it always triggers. How to avoid that?

    How to calculate the age when the same record is accessed from another record as child (related record)

    ReplyDelete

My Name is..