How to Set Data to an Entity with Various Data Types Using a C# Plugin in Dynamics 365
Dynamics36
Introduction
Microsoft Dynamics 365 provides a powerful platform for managing business data through entities. In this blog post, we will create a custom entity using a C# plugin and assign various data types, including:
String
Integer
DateTime
Boolean
Decimal
OptionSet (Single and Two OptionSet)
MultiSelect OptionSet
Lookup
This guide is helpful for Dynamics 365 developers looking to automate entity creation using plugins.
Breakdown of Data Types Used
Field Name | Type | Example Value |
custom_name | String | "Sample Name" |
custom_age | Integer | 25 |
custom_dateofbirth | DateTime | 15-May-1998 |
custom_isactive | Boolean | true (Yes) |
custom_salary | Decimal | 50000.75 |
custom_status | OptionSet | 1 (Active) |
custom_preferences | MultiSelect OptionSet | [101 (Email), 102 (SMS)] |
custom_accountid | Lookup (Reference to Account) | GUID |
String
customEntity["custom_name"] = "Sample Name";
Integer
customEntity["custom_age"] = 25;
DateTime
customEntity["custom_dateofbirth"] = new DateTime(1998, 5, 15);
Boolean (Two OptionSet)
customEntity["custom_isactive"] = true;
Decimal
customEntity["custom_salary"] = new decimal(50000.75);
OptionSet (Single Choice)
customEntity["custom_status"] = new OptionSetValue(1); // 1 = Active, 2 = Inactive
MultiSelect OptionSet
customEntity["custom_preferences"] = new OptionSetValueCollection { new OptionSetValue(101), // Email new OptionSetValue(102) // SMS };
Lookup (Entity Reference)
customEntity["custom_accountid"] = new EntityReference("account", new Guid("C3B5D1F5-4F7B-4C6A-9F01-5A347E25C849"));
Implementing in Plugin logic
using System;
using Microsoft.Xrm.Sdk;
public class CreateCustomEntityPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
// Create an entity record
Entity customEntity = new Entity("custom_entity");
// Assign different data types
customEntity["custom_name"] = "Sample Name"; // String
customEntity["custom_age"] = 25; // Integer
customEntity["custom_dateofbirth"] = new DateTime(1998, 5, 15); // DateTime
customEntity["custom_isactive"] = true; // Boolean
customEntity["custom_salary"] = new decimal(50000.75); // Decimal
customEntity["custom_status"] = new OptionSetValue(1); // Two Optionset or Optionset
// MultiSelect OptionSet (Multiple Choices)
customEntity["custom_preferences"] = new OptionSetValueCollection
{
new OptionSetValue(101), // Email
new OptionSetValue(102) // SMS
};
// Lookup
customEntity["custom_accountid"] = new EntityReference("account", new Guid("C3B5D1F5-4F7B-4C6A-9F01-5A347E25C849"));
// Create the record in CRM
service.Create(customEntity);
}
catch (Exception ex)
{
tracingService.Trace("CreateCustomEntityPlugin Error: {0}", ex.ToString());
throw new InvalidPluginExecutionException("An error occurred in the plugin.", ex);
}
}
}