Event handlers trigger actions that run when a certain event happens, such as when a record is created, updated, or deleted. The main options for event handler registration within the Power Platform include plug-ins and JavaScript events, along with Microsoft Flow or Power Automate (known as Business Process Flows).
1. Plug-ins:
Plug-ins in the Power Platform are a server-side way to trigger custom business logic. They are typically written in C# or VB.NET, and are hosted within the Common Data Service (CDS) environment.
One core benefit of plug-ins is their ability to run synchronously (blocking) or asynchronously (non-blocking). Synchronous plug-ins execute immediately and have the ability to rollback an entire transaction if an error occurs. In contrast, asynchronous plug-ins execute within the context of a queue and do not allow for transaction rollbacks.
Registration of plug-ins is done through the Plug-in Registration Tool, a component of the Power Platform SDK.
In the following example, a plug-in is used to trigger custom logic when an account is created:
C#:
public class AccountCreate : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
var account = (Entity)context.InputParameters["Target"];
// Your custom logic here
}
}
}
2. JavaScript Events:
On the client-side, JavaScript event handlers can also be registered on forms in the Power Platform. These event handlers are typically used to enhance the user interface or interactivity of the Power Platform forms.
JavaScript event handlers can be registered for various events, such as ‘onLoad’ (when the form loads), ‘onSave’ (before the form saves), and field-level ‘onChange’ events.
Here is an example of an ‘onSave’ event JavaScript registration:
Xrm.Page.data.entity.addOnSave(mySaveFunction);
function mySaveFunction(executionContext) {
// Your custom logic here
}
3. Business Process Flows (Microsoft Flow and Power Automate):
Business Process Flows are another option for event handler registrations within the Power Platform. These can be used to guide users through a specific business process and perform actions based on various steps throughout the flow.
These Business Process Flows can be triggered in several ways, including when a record is created, updated, based on a schedule, or even a manual trigger.
Generally, these are used for less complex event handling and do not require any coding experience.
The Power Platform offers a range of efficient and effective event handler registration options. It’s essential to understand the triggers and impacts of each, as well as how to apply these tools within your applications. By mastering these aspects, you will find the PL-400 Microsoft Power Platform Developer Exam that much more conquerable.
Practice Test
True or False: Event handler registration options in Microsoft Power Platform allow the developer to execute specific actions automatically.
- True
- False
Answer: True
Explanation: Event handlers allow the execution of scripts or functions during specific instances like form loading or field changing. Developers can register these event handlers to run automatically for those instances.
Which of the following are event handler registration options in Microsoft Power Platform? Select all that apply.
- A. Function
- B. Library
- C. Event
- D. Execution
Answer: A. Function, B. Library, C. Event
Explanation: Functions, libraries, and events are all elements that can be registered as event handlers in Microsoft Power Platform. Execution is not a registration option.
The sequence in which events are triggered in a Microsoft Power Platform is known as what?
- A. Event Flow
- B. Event Sequence
- C. Event Cycle
- D. Event Order
Answer: D. Event Order
Explanation: When multiple event handlers are registered, they are executed in a specific sequence which is called Event Order.
True or False: Event handlers are always executed synchronously.
- True
- False
Answer: False
Explanation: Event handlers can be executed either synchronously or asynchronously. This depends on the needs of the process and how the event handler has been configured.
In Microsoft Power Platform, which of the following styles can be selected for event handler registration?
- A. Asynchronous
- B. Synchronous
- C. Both A and B
- D. None of the above
Answer: C. Both A and B
Explanation: Event handler registration can be either asynchronous (where the process will continue regardless of completion), or synchronous (where the process will wait for completion).
When registering an event handler, what does the execution context refer to?
- A. The environment in which the process is running
- B. Which user triggered the event
- C. The state of the Microsoft Power Platform application
- D. The state of the form or control when the event was triggered
Answer: D. The state of the form or control when the event was triggered
Explanation: Execution context in the case of event handler registration refers to the state of the form or control when the event was triggered.
True or False: Microsoft Power Platform permits multiple handlers for a single event to be registered.
- True
- False
Answer: True
Explanation: Microsoft Power Platform does allow multiple handlers for a single event to be registered. The execution of these handlers follows the specific Event Order.
“Onload”, “OnSave”, “OnChange” are examples of which event handler registration option?
- A. Event
- B. Function
- C. Library
- D. Sequence
Answer: A. Event
Explanation: In Microsoft Power Platform, examples of events for which handlers can be registered are ‘Onload’, ‘OnSave’, and ‘OnChange’.
For which fields in PowerApps can one register an “OnChange” event?
- A. Text
- B. Option Set
- C. Lookup
- D. All of the Above
Answer: D. All of the Above
Explanation: An ‘OnChange’ event can be registered for any field, including text, option set, and lookup.
True or False: Microsoft Power Platform supports the use of anonymous functions as event handlers.
- True
- False
Answer: False
Explanation: Microsoft Power Platform does not support the use of anonymous functions as event handlers. The use of named functions is required.
Interview Questions
What is event handler registration in Microsoft Power Platform?
Event handler registration in Microsoft Power Platform is a process of associating a plugin or a custom workflow activity to an event that occurs on the server so that the plugin or workflow can be triggered when the event occurs.
Which tool is commonly used to register an event handler in Microsoft Dynamics 365?
The Plugin Registration Tool is commonly used to register an event handler in Microsoft Dynamics 365.
What are the three options for the execution mode during event handler registration?
The three options for the execution mode during event handler registration are Synchronous, Asynchronous and Delete Operation.
What is the primary difference between asynchronous and synchronous event handlers?
Asynchronous event handlers are queued and executed in no specific order and do not interfere with user interface operations. Synchronous event handlers are executed immediately and in maximum two minutes and can be designed to modify the UI.
What are Pre-validation and Pre-operation stages in an event pipeline?
The Pre-validation stage is the initial stage in the event pipeline where you validate data and decide whether the operation should be permitted to continue. Pre-operation is when you make changes to data before the core system operation occurs.
How can you stop an operation in the event pipeline?
You can stop an operation in the event pipeline by throwing an InvalidPluginExecutionException in either the pre-validation or pre-operation stage.
What happens when a synchronous plug-in or workflow is registered for the Delete message?
It will not be allowed to cancel the operation. An InvalidPluginExecutionException thrown from a synchronous plug-in registered for a Delete message is ignored.
Can multiple event handlers be registered for the same event?
Yes, multiple event handlers can be registered for the same event.
In which cases are Post-operation registered plugins useful?
Post-operation registered plugins are useful when the operation needs to be completed prior to the execution of the plugin, for example when creating a related record for a new record that was just created.
Is it possible to unregister an event handler and what would be the reason to do so?
Yes, it is possible to unregister an event handler. This might be needed when the event handler is no longer required or if a different event handler is needed to replace it.
Can I use the invalid characters in plug-in and custom workflow names during registration?
No, you cannot use invalid characters (“/”, “\”, “#”, “%”, “&”, “*”, “:”, “<", ">“, “?”, “{“, “|”, “}”) in plug-in and custom workflow names during registration.
Can I register an async workflow to be triggered by the ‘Delete’ operation?
Yes, unlike a sync plug-in, an async workflow can be triggered by the ‘Delete’ operation.
Can a field value be changed in the post-operation stage of a synchronous event?
No, a field value cannot be changed in the post-operation stage of a synchronous event as the database transaction has already been committed.
What happens if a synchronous plug-in or workflow registered in the pre-validation stage throws an exception?
If a synchronous plug-in or workflow registered in the pre-validation stage throws an exception, the base system operation is not performed. The exception is shown in the application that made the request.
Are event handlers registered against system or custom entities?
Event handlers can be registered against both system and custom entities.