Saturday, 20 June 2015

CRM 2011/2013/2015 Mastering Plugin Registration Filtering Attributes

In plugin, we also can filter our logic to do or not to do something. Moreover, for Plugin onUpdate, for example : to prevent user to update AccountCode or Quote Type, or whatever, sometimes our logic only work, for example if Quote type has been changed, then you have to prompt user to say, you cannot change the Quote Type. But, if you only change the Address in Quote, you by default will also hit the plugin, so that you have to filter, like this :

if(entity.Attributes.Contains("tfp_quotetype"))
{
     throw new InvalidPluginExecutionException("Hi you cannot change Quote Type");
}

But, again your plugin also will be executed.

Let’s say in your plugin, you update this entity using this plugin, this also can cause infinite looping for updating.

In this post, I would like to know to filter Attribute that you don’t want to fire the Plugin.

By default, your plugin, after registered will not filter any attributes, meaning that, once your entity updated, no matter which attribute it is updated, it will trigger your plugin.

image

In my code, I did not do any filter :

image

Meaning that, any changes on every field, the plugin will be triggered

We try this


image

Let’s we do filter, I don’t want to trigger this if Ship Method field changed,

image

then I try to uncheck this attribute :

image

Then, I try to update the Shipping Method :

Will it work? Meaning that if I update the Shipping Method, this plugin will not be triggered?

Let me try!

I update the value from null to Airbone,

But, why I still get this plugin executed?

image

Then, you debug, yes it hits your plugin :

image

If you thought like that, it is wrong, and you’re still confused about Plugin Filtering

Actually plugin filtering attributes is used to trigger plugin or execute plugin once the field is changed, then which fields you want to trigger the plugin, you have make it as checked.

When you uncheck, it doesn’t mean that if you change the shippingmethodcode value (only this field you change in UI) so that your plugin will not be executed, since there are many other attributes that your checked before that has changed, it can be CRM system field that is hidden.

Those fields, for example :

ModifiedOn

image

And you realize that ModifiedOn field is in the checked list.

image

So, even though you are only update the Shipping Method, it also affect to other field, even though your target entity as input parameter does not contain shippingmethodcode, your plugin will still be executed.

Then, you try to not include the ModifiedOn and its friends

image

Then you try again, your plugin is still being hit

image

Then, what you do?

Okey, let’s understand this filtering, let’s change your mind to not focus to what you don’t want, but let’s focus to what you want.

You will fill difficult to uncheck every attribute that you don’t want.

For example, you don’t want to execute plugin after you change your shippingmethod only.

Now, let’s think that actually you want to execute your Plugin, IF QUOTE TYPE is changed.

Okay, what will do is :

1. Deselect all your attributes

2. Then check the ‘Quote Type’

image

Until your plugin step will be look like this.

image

Then stay tune in your plugin and trigger your quote, keep your breakpoint, then let’s you see if your plugin is triggered or not.

Wow, your plugin is not executed after you change the shipping method :

image

So, let’s you have very long code for calculation, it is better to filter attribute do you want to trigger and execute your plugin rather than you let any minor change in your field then run your long lines code of plugin.

Remember that QuoteId (uniqueidentifier) will be always be the InputParameter

image

Hope it helps and change your mind!
*copied from my other blog: http://aileengusni.blogspot.sg/2014/05/crm-2011-2013-mastering-plugin.html

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi, Thanks for your blog!
    We just discoverd that keeping the uniqueidentifier checked will result in the plugin still being fired. Disabling the uniqueidentifier fieldd stopped the plugin.
    Dynamics 365 CRM online, custom entity

    ReplyDelete
  3. Great post, you have pointed out some excellent points, I as well believe this is a very superb website.
    PIPEDRIVE

    ReplyDelete
  4. Thanks for sharing this tutorial on CRM software. Thanks for sharing. Definitely bookmarking for more such article.

    ReplyDelete

My Name is..