Dataverse : How to make the right choices for Plugins Development
Plugins is an important part of development in Dynamics 365 and I would like to share a basic knowledge about choosing plugin in various case.
Which stage should we choose?
Here I have examples of scenario:
When a Contact is created, perform the following actions (using plugins)
- The name of a Contact should not contain special characters, so we have to remove all special characters from FirstName and LastName of this contact.
- It is not allowed to create a duplicated Contact (with the same email). So if this Contact is duplicated, we have to show error to the user and cancel the creation of this contact.
- Create a task for the current user to validate the contact.
To adapt this requirement we could use various approaches such as: Microsoft Flow, Workflow, Plugins. However, plugins is most effective solution because we could reduce the number of execution request to the server.
Let's have look at what Microsoft said:
- Line in green is when we should use it.
- Line in red is when we shouldn't use it.
Note: there are some special cases that I have met when working with plugins
- Field Owner can not be changed in Pre-Operation stage.
Solution:- Move to pre-validation stage
- Use assign method in post-operation stage
- Full name of Contact can not be set in Pre-Operation stage.
Solution: move to pre-validate stage
So back to my example:
- The name of a Contact should not contain special characters, so we have to remove all special characters from FirstName and LastName of this contact. -> Pre-Operation
- It is not allowed to create a duplicated Contact (with the same email). So if this Contact is duplicated, we have to show error to the user and cancel the creation of this contact. -> Pre-Validation
- Create a task for the current user to validate the contact. -> Post-Operation
How to handle update in plugin?
- In Pre-Operation plugin: we only update attributes values of the entity included in the message
- In Post-Operation plugin: we are not recommended update attributes value in this plugin. However, we must do it in some special business logic and in this case we must call localContext.OrganizationService.Update to execute update value.
Summary
Base on requirements, we need to choose the stage of the plugin correctly.
Add new comment