In essence, a plugin is designed to extend the functionality of an application or software without changing the underlying source code. It is a kind of software component that adds a particular feature to an existing computer program. When a program supports plug-ins, it enables customization.
In the context of Dynamics 365 CRM (an integral part of the Microsoft Power Platform), plugins are used to augment business operations and automate their workflows. Plugins are written in either C# or VB, and are often used to add, update, remove, or manipulate data within Dynamics 365.
Developing a Plug-In That Implements Business Logic
In relation to the PL-400 certification exam, you need to understand how to develop a plug-in that implements business logic. Let’s say we need to prevent deletion of an account if it has any associated contacts. Let’s go through the steps to develop this plug-in.
- Setting up the Plugin Development Environment: The first step is to set up the development environment by installing Visual Studio and the .NET Framework required for Dynamics 365.
- Creating a new Project: Now, open the Visual Studio and create a new project. Select the Class Library (.NET Framework) template.
- Adding the References: We need to add two references to our project i.e., Microsoft.Xrm.Sdk and System.Runtime.Serialization.
- Writing the plugin code: Now, start with adding namespaces to your code. Then, inherit IPlugin interface which is defined in Microsoft.Xrm.Sdk. After that, implement the “Execute” method of IPlugin interface to prevent the deletion of an account if it has associated contacts.
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
public class PreventAccountDeletion: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)
serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "account") { return; }
try
{
int numberOfContacts = GetContacts(service, entity.Id);
if (numberOfContacts > 0)
{
throw new InvalidPluginExecutionException("You cannot delete an account that has contacts associated with it.");
}
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
private int GetContacts(IOrganizationService service, Guid accountId)
{
//Code to get number of contacts associated with this account.
}
}
- Registering the Plugin: After writing the plugin, compiling it will create a .dll file. This .dll file needs to be registered in CRM using Plugin Registration Tool.
- Testing the Plugin: Once the plugin is successfully registered, you can now test this in Dynamics 365 CRM.
This is a simple example of a plug-in in CRM implementing business logic. It’s important to understand that the complexity of the plugin will vary based on the development needs.
To sum up, developing a plug-in that implements business logic is really about identifying the dynamic features your business needs and matching that with the potential of the Microsoft Power platforms. It involves hands-on experience with programming (C# or VB) and using SDK libraries correctly. Good luck with your PL-400 preparation!
Practice Test
True or False: Business logic refers to the programming that manages communication between an end-user interface and a database.
- True
- False
Answer: True.
Explanation: Business logic in software development is the programming that manages communication between an end-user interface and a database, including all the rules and processes that function between a user interface and a data model.
Which of these are commonly used to implement business logic in a plug-in?
- a. jQuery
- b. JavaScript
- c. Flows
- d. Power Apps
Answer: c. Flows, d. Power Apps
Explanation: Flows and Power Apps are Microsoft Power Platform services generally used to automate and enhance business processes including the implementation of business logic. Whereas, jQuery and JavaScript are used for client-side scripting, not specifically for business logic in plug-ins.
True or False: A plug-in in Microsoft Dynamics 365 can be written only in C#.
- True
- False
Answer: False.
Explanation: Plug-ins for Microsoft Dynamics 365 can be written in any .NET compatible language, although C# is the most commonly used.
Which Microsoft Power Platform component is used to extend business logic beyond what’s provided by built-in features in Power Apps?
- a. Power BI
- b. Connectors
- c. Power Automate
- d. Power Virtual Agents
Answer: c. Power Automate
Explanation: Power Automate (earlier known as Flow) is used to create automated workflows between applications and services in order to extend the business logic beyond what’s provided by default by Power Apps.
True or False: The primary reason to develop a plug-in that implements business logic is to improve application performance.
- True
- False
Answer: False.
Explanation: The primary reason to develop a plug-in is to implement custom business logic that can’t be achieved using out-of-the-box features in a platform.
The execution context in a plug-in contains:
- a. Information about the user who triggered the event
- b. Details about the event that triggered the plug-in
- c. Information about the environment where the plug-in is running
- d. All of the above
Answer: d. All of the above
Explanation: The execution context in a plug-in contains information about the event that triggered the plug-in, the user who triggered the event, and the environment where the plug-in is running.
True or False: Plug-ins are executed immediately after the event that triggered them.
- True
- False
Answer: True.
Explanation: Plug-ins are part of the event processing pipeline and are therefore executed immediately after the event that triggered them.
Which of these is NOT a stage where a plug-in can be registered?
- a. Pre-validation
- b. Pre-operation
- c. Post-operation
- d. Post-validation
Answer: d. Post-validation
Explanation: Post-validation is not a stage where a plug-in can be registered in Microsoft Dynamics 365 or Power Platform. The three stages are Pre-validation, Pre-operation, and Post-operation.
True or False: You can create a custom plug-in to implement business logic and use it in Power Apps.
- True
- False
Answer: True.
Explanation: Custom plug-ins to implement business logic can indeed be created and used in Power Apps.
Which type of testing would you use to validate a newly implemented plug-in?
- a. Unit testing
- b. Security testing
- c. Performance testing
- d. All of the above
Answer: d. All of the above
Explanation: To validate a newly implemented plug-in, it is essential to conduct unit testing to validate its functions, security testing to ensure its protection against threats, and performance testing to ensure it meets desired performance parameters.
Interview Questions
What is the main purpose of building a plug-in for business logic implementation?
The main purpose is to handle events that occur within the system such as creating, updating, or deleting records so it can apply custom business logic during these operations.
In which language do we write plug-ins for Microsoft Power Platform?
We write plug-ins in C# or VB .NET, as Microsoft Power Platform is built on .NET.
What are the points where your plug-ins can be registered, in context of Microsoft Dynamics 365?
Plug-ins can be registered on the following points in Microsoft Dynamics 365: Pre-validation, Pre-operation, and Post-operation.
Can you explain “Pre-Validation” plug-in stage?
Pre-Validation stage occurs prior to the main system operation where the data provided for the operation is checked to ensure it’s appropriate & accurate. Plug-in registered at this stage runs regardless if the transaction is part of the execute transaction method.
What is the use of a Post-operation plug-in?
Post-operation plugins are used for final validation and modifications of data, or other actions after the core transaction has been completed.
How is business logic implemented in a plug-in?
Business logic is implemented in a plug-in through code written using C# or VB .NET. The code is registered in the system to handle certain events such as updating or creating a record.
What happens when an error occurs in a plug-in?
If an error occurs in a plugin, it throws an exception which will roll back the operation. The user will also get an error notification on their screen.
What is Isolation Mode in a plugin?
Isolation mode determines the trust level of a plug-in assembly. If registered in ‘none’ isolation mode, the plug-in has full trust and can access system resources outside the sandbox. If registered in ‘sandbox’ mode, the plugin can only access resources in its restricted environment.
What are synchronous and asynchronous plug-ins?
Synchronous plugins are those which execute in the same thread as that of the main platform and block the platform from processing further till they finish. Asynchronous plugins are those which run separately without blocking the main platform.
When does Microsoft recommend using asynchronous plug-ins?
Microsoft recommends using asynchronous plugins for operations that are lengthy and do not need to be finished immediately, thus improving performance and reliability, like sending emails, or operating on large amount of data.
In which scenario are “Pre-operation” plugins most useful?
Pre-operation plug-ins are most useful while changing the default behaviour of systems, for instance, changing the values that the user has entered.
Can a plug-in send or receive data over the network?
Yes, but only plug-ins registered in ‘none’ isolation mode can send or receive data over the network, while plugins in sandbox mode can’t.
Can we include references to other assemblies in our plugin code?
Yes, you can include references to other assemblies from your plugin code. However, those assemblies must also be registered on the platform.
What types of triggers are there for triggering a plugin?
Triggers can be certain occurrences such as Create, Update, Delete, Retrieve, Retrieve Multiple, Associate, or Disassociate.
Can we debug plug-ins in the Microsoft Power Platform?
Yes, there are specific steps and requirements for debugging plug-ins, which are detailed in Microsoft’s official developer documentation.