Thursday, February 23, 2012

Use of “Filtering Attributes” in plugin registration

Recently one of my clients asked me to make some changes to one of their existing plugin. The plugin was sending an email to a user when “Estimated Revenue” field is updated. The plugin was comparing  the value of “Estimated Revenue” from the  PreEntityImage with value from PostEntityImage. It made me think that why you would use PreEntityImage and PostEntityImage values to do that when you can achieve that by setting  “Filtering Attributes” during plugin registration.
parentcustomer  
This is very handy parameter of step registration process. It is equivalent to “IsDirty” function in JavaScript. If you use “Filtering Attributes” on post update events, you don’t need to compare the value of fields using PreEntityImage and PostEntityImage. The other biggest advantage is that your plugin does not execute on every update event.
Here is some code. This code creates a note for a contact when parentcustomerid field is updated.
protected void ExecutePostContactUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }             // TODO: Implement your custom Plug-in business logic.
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            
            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];
                
                try
                {
                    Entity task = new Entity("task");
                    task["subject"] = "Account number is missing";
                    task["regardingobjectid"] = new EntityReference("contact", entity.Id);
                    task["description"]= "Parent Customer field has been updated".;

                     // Create the task in Microsoft Dynamics CRM.
                    service.Create(task);
                                        
                }

                 catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                }

             }
        }
To run this code create a plugin using developer's toolkit on contact entity. Use the values shown in the screen shot to register the update step. Then overwrite the  ExecutePostContactUpdate() method with this code. Deploy the plugin and test it.
Here is link to a step by step tutorial to create a plugin using developers toolkit.

4 comments:

  1. Does anyone know if multiple attributes in the Filtering attribute are interpreted as OR or AND?

    ReplyDelete
  2. Does anyone know if multiple attributes in the Filtering attribute are interpreted as OR or AND?

    ReplyDelete