How to Retrieve Data related to diff datatypes from Dynamics 365 Target Entity Using a Plugin
D365 CRM Plugin
In this blog post, we’ll walk through how to retrieve different types of data from a Microsoft Dynamics 365 entity using a plugin. We'll explore different field types and their corresponding retrieval methods, including:
Single-line and multiple-line text fields
Numeric fields (Integer, Decimal)
Date fields
Entity reference fields (Lookup)
Currency fields
Option Set and Multiselect Option Set fields
Let’s dive right in!
Retrieving Various Data Types in a Plugin
Single-line / Multiple-line Text (String)
string accountName = targetEntity.Contains("name") && targetEntity["name"] != null ? targetEntity.GetAttributeValue<string>("name") : null;
Numeric Fields (Integer and Decimal)
Integer Field:
int? quantity = targetEntity.Contains("quantity") && targetEntity["quantity"] != null ? targetEntity.GetAttributeValue<int?>("quantity") : null;
Decimal Field:
decimal? price = targetEntity.Contains("price") && targetEntity["price"] != null ? targetEntity.GetAttributeValue<decimal?>("price") : null;
Date Time Fields (Date and Time / Date Only)
DateTime? createdOn = targetEntity.Contains("createdon") ? targetEntity.GetAttributeValue<DateTime?>("createdon") : null;
Entity Reference (Lookup) Fields
Retrieve Entity Reference:
EntityReference owner = targetEntity.Contains("owner") && targetEntity["owner"] != null ? targetEntity.GetAttributeValue<EntityReference>("owner") : null;
Retrieve Entity Reference Guid:
Guid? ownerId = targetEntity.Contains("owner") && targetEntity["owner"] != null ? targetEntity.GetAttributeValue<EntityReference>("owner")?.Id : null;
Retrieve Entity Reference Logical Name:
string owner_entityname = targetEntity.Contains("owner") && targetEntity["owner"] != null ? targetEntity.GetAttributeValue<EntityReference>("owner")?.LogicalName : null;
Currency Fields
Retrieve Currency:Money revenue = targetEntity.Contains("revenue") && targetEntity["revenue"] != null ? targetEntity.GetAttributeValue<Money>("revenue") : null;
Retrieve Currency Value:
decimal? revenueValue = targetEntity.Contains("revenue") && targetEntity["revenue"] != null ? targetEntity.GetAttributeValue<Money>("revenue")?.Value : null;
Retrieve Currency Entity Reference:
EntityReference? currencyE = targetEntity.Contains("revenue") && targetEntity["revenue"] != null ? targetEntity.GetAttributeValue<Money>("revenue")?.Currency : null;
Retrieve Currency Name:
string? currencyCode = targetEntity.Contains("revenue") && targetEntity["revenue"] != null ? targetEntity.GetAttributeValue<Money>("revenue")?.Currency?.Name : null;
Option Set (Single Select)
Retrieve Option Set Value:
OptionSetValue status = targetEntity.Contains("statuscode") && targetEntity["statuscode"] != null ? targetEntity.GetAttributeValue<OptionSetValue>("statuscode") : null;
Retrieve OptionSet Value and Label:
int? statusValue = targetEntity.Contains("statuscode") && targetEntity["statuscode"] != null ? targetEntity.GetAttributeValue<OptionSetValue>("statuscode")?.Value : null; // Option Set & Two Optionset -> Option Set: If you want optionset formatted value/label string statusFormattedValue = targetEntity.FormattedValues.Contains("statuscode") ? targetEntity.FormattedValues["statuscode"] : null;
Multiselect Option Set
Retrieve Multiselect Option Set Values:
OptionSetValueCollection interests = targetEntity.Contains("interests") && targetEntity["interests"] != null ? targetEntity.GetAttributeValue<OptionSetValueCollection>("interests") : null; // Multi Select Optionset -> If you want optionset values int[] selectedValues = targetEntity.Contains("interests") && targetEntity["interests"] != null ? targetEntity.GetAttributeValue<OptionSetValueCollection>("interests").Value : null;
Retrieve MultiSelect OptionSet Labels:
List<string> selectedLabels = new List<string>(); if (targetEntity.FormattedValues.Contains("interests")) { string formattedValues = targetEntity.FormattedValues["interests"]; selectedLabels = formattedValues.Split(',').ToList(); }
Plugin Code
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
public class FollowupPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity targetEntity = (Entity)context.InputParameters["Target"];
// Obtain the IOrganizationService instance which you will need for
// web service calls.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
// Retrieve values from the Target entity
//String -> Single line / Multiple lines of text
string accountName = targetEntity.Contains("name") && targetEntity["name"] != nul ?
entity.GetAttributeValue<string>("name") : null;
// Number -> Integer
int? quantity = targetEntity.Contains("quantity") && targetEntity["quantity"] != null ?
targetEntity.GetAttributeValue<int?>("quantity") : null;
// Number -> Decimal/float
decimal? price = targetEntity.Contains("price") && targetEntity["price"] != null ?
targetEntity.GetAttributeValue<decimal?>("price") : null;
// Datetime -> Date and time || Date only
// Question mark (?) represents nullable value also
// If datatype is Date only in crm then the time value will be 00:00:00
DateTime? createdOn = targetEntity.Contains("createdon") ?
targetEntity.GetAttributeValue<DateTime?>("createdon") : null;
// EntityReference -> Lookup
EntityReference owner = targetEntity.Contains("owner") && targetEntity["owner"] != null ?
targetEntity.GetAttributeValue<EntityReference>("owner") : null;
// EntityReference -> Lookup: If you want related entity Guid
Guid? ownerId = targetEntity.Contains("owner") && targetEntity["owner"] != null ?
targetEntity.GetAttributeValue<EntityReference>("owner")?.Id : null;
// EntityReference -> Lookup: If you want related entity logical name
String? owner_entityname = targetEntity.Contains("owner") && targetEntity["owner"] != null ?
targetEntity.GetAttributeValue<EntityReference>("owner")?.LogicalName : null;
// Number -> Currency
Money revenue = targetEntity.Contains("revenue") && targetEntity["revenue"] != null ?
targetEntity.GetAttributeValue<Money>("revenue") : null;
// Number -> Curreny: If you want only value
decimal? revenueValue = targetEntity.Contains("revenue") && targetEntity["revenue"] != null ?
targetEntity.GetAttributeValue<Money>("revenue")?.Value : null;
// Number -> Curreny: If you want only curreny entity reference
EntityReference? currencyE = targetEntity.Contains("revenue") && targetEntity["revenue"] != null ?
targetEntity.GetAttributeValue<Money>("revenue")?.Currency : null;
// Number -> Curreny: If you want only currency name
string? currencyCode = targetEntity.Contains("revenue") && targetEntity["revenue"] != null ?
targetEntity.GetAttributeValue<Money>("revenue")?.Currency?.Name : null;
// Option Set & Two Optionset -> Option Set
OptionSetValue status = targetEntity.Contains("statuscode") && targetEntity["statuscode"] != null ?
targetEntity.GetAttributeValue<OptionSetValue>("statuscode") : null;
// Option Set & Two Optionset -> Option Set: If you want optionset value
int? statusValue = targetEntity.Contains("statuscode") && targetEntity["statuscode"] != null ?
targetEntity.GetAttributeValue<OptionSetValue>("statuscode")?.Value : null;
// Option Set & Two Optionset -> Option Set: If you want optionset formatted value/label
string statusFormattedValue = targetEntity.FormattedValues.Contains("statuscode") ?
targetEntity.FormattedValues["statuscode"] : null;
// Multi Select Optionset
OptionSetValueCollection interests = targetEntity.Contains("interests") && targetEntity["interests"] != null
? targetEntity.GetAttributeValue<OptionSetValueCollection>("interests")
: null;
// Multi Select Optionset -> If you want optionset values
int[] selectedValues = targetEntity.Contains("interests") && targetEntity["interests"] != null
? targetEntity.GetAttributeValue<OptionSetValueCollection>("interests").Value
: null;
// Multi Select Optionset -> If you want optionset labels/ formatted values
List<string> selectedLabels = new List<string>();
if (targetEntity.FormattedValues.Contains("interests"))
{
string formattedValues = targetEntity.FormattedValues["interests"];
selectedLabels = formattedValues.Split(',').ToList();
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
}
catch (Exception ex)
{
tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
throw;
}
}
}
}