Retrieve the status of an Entity Record through plugin
For writing any plugin you should add following references
Microsoft.Crm.Sdk.Proxy
Microsoft.Xrm.Sdk
System.Runtime.Serialization
After that write plugin steps and Register Image like
if (context.PreEntityImages.Contains("PreImage"))
{
Entity myPreImage = (Entity)context.PreEntityImages["PreImage"];
}
Then write a logic like this
var status = myPreImage.GetAttributeValue<OptionSetValue>("statecode").Value;
Here the Various States and Status reasons I mentioned here.
So If Record is giving value "0" Means it is active or inactive.
So Final Code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
namespace ActiveEntity
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider ServiceProvider)
{
try
{
IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory));
IExecutionContext context = (IExecutionContext)ServiceProvider.GetService(typeof(IExecutionContext));
IOrganizationService Service = ServiceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
if (context.PreEntityImages.Contains("PreImage"))
{
Entity entity = (Entity)context.InputParameters["Target"];
Entity myPreImage = (Entity)context.PreEntityImages["PreImage"];
if (myPreImage.Attributes.Contains("statecode"))
{
var status = myPreImage.GetAttributeValue<OptionSetValue>("statecode").Value;
if (status == 0)
{
//Your Custom Logic
}
}
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
After Build this plugin make sure Signing the plugin then Register Assembly and Step.
After that Register an Image.
Then you will get a result.
Hope it helps you,
Thank you,
Shiva Ram
No comments:
Post a Comment