Part 19 - Understanding Basic Plugin Code Structure


Scenario:

==> You are working in a hospital using Dynamics 365 CRM.

==> Whenever a Patient record is created, you want to check if the patient’s age is less than 18 years.

==> If the patient is under 18, the system should show an error.


Step by Step Process of Writing a Basic Plugin Code:


1. Using Libraries:

Article content

  • using System; ---> This is a standard library for basic operations like displaying messages and handling errors.
  • using Microsoft.Xrm.Sdk; ---> This is a special Dynamics 365 library that allows you to interact with CRM data.


2. Class and Plugin Declaration:

Article content

  • public class ---> A class is a container where the plugin logic is written.
  • PatientAgeCheckPlugin ---> This is the name of your class. You can name it based on its purpose.
  • IPlugin ---> This is a rule (interface) that all plugins must follow. It tells CRM that this is a plugin.


3. Main Execute Method:

Article content

  • Execute ---> This is like the main function. It runs when the plugin is triggered.
  • serviceProvider ---> It provides services like logs, context, and database access.


4. Get Context (To Know What Happened):

Article content

Context ---> It tells you what triggered the plugin

Example:

  • Is it a Create?
  • Is it an Update?
  • Which record is it?


5. Get Tracing Service (For Logs)

Article content

  • Tracing Service → It writes messages to the logs so you can see what happened.
  • Trace() → Think of this like writing notes on paper.

Example:

  • "Plugin Execution Started." → This is the first log entry to confirm that the plugin started.


6. Check if It’s a Create Operation:

Article content

  • context.MessageName ---> It checks if the action is "Create".
  • If it’s not a Create, it stops the plugin.

Example:

  • If a patient’s record is updated instead of created, the plugin won’t run.


7. Get Patient Data:

Article content

  • context.InputParameters["Target"] ---> This contains the record data (e.g., patient name, age, etc.).
  • Entity patient ---> This treats the record as a Patient entity (record).


8. Ensure It’s a Patient Record:

Article content

  • LogicalName → It checks if the record is from the Patient table.
  • If not, the plugin exits.

Example:

  • If a record from the Doctor table is created, the plugin won’t run.


9. Check for Age Field:

Article content

  • Contains("age") ---> Checks if the patient record has an age field.
  • If it doesn’t, it logs a message and exits.


10. Perform the Age Check:

Article content

  • int age = (int)patient["age"] -----> Get the patient’s age.
  • if (age < 18) ----> Check if the age is less than 18 years.
  • throw new InvalidPluginExecutionException ---> This shows an error message if the patient is under 18.

Example:

  • If the patient is 16, it will display as ------> Patient is under 18. Cannot register without parental consent.
  • If the patient is 20, it will display as ------> Patient Age: 20. Registration Successful.


===> By combining all the above steps the final plugin code look like as shown below:

Article content

Final Summary

  • Context → Checks what happened (Create, Update, Delete).
  • Tracing → Logs messages for debugging.
  • Entity → Accesses data like name and age.
  • Validation → Checks the patient’s age.
  • Error → If the patient is under 18 years, it throws an error.

To view or add a comment, sign in

Others also viewed

Explore content categories