Validation form using plugin d365
Plugin Code:
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Indegene.Omnipresence.Common;
namespace Indegene.Omnipresence.Plugins
{
public class OrderClose_PreOperation :
PluginBase
{
/// <summary>
/// Initializes a new instance of the <see cref="Order_PreOperation"/> class.
/// </summary>
/// <param name="unsecure">Contains public (unsecured) configuration
information.</param>
/// <param name="secure">Contains non-public (secured) configuration
information.
/// When using Microsoft Dynamics 365
for Outlook with Offline Access,
/// the secure string is not passed to
a plug-in that executes while the client is offline.</param>
public OrderClose_PreOperation(string unsecure, string secure)
: base(typeof(OrderClose_PreOperation))
{
// TODO: Implement your custom configuration handling.
}
/// <summary>
/// Main entry point for he business
logic that the plug-in is to execute.
/// </summary>
/// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
/// <see cref="IPluginExecutionContext"/>,
/// <see cref="IOrganizationService"/>
/// and <see cref="ITracingService"/>
/// </param>
/// <remarks>
/// For improved performance, Microsoft
Dynamics 365 caches plug-in instances.
/// The plug-in's Execute method should
be written to be stateless as the constructor
/// is not called for every invocation
of the plug-in. Also, multiple system threads
/// could execute the plug-in at the
same time. All per invocation state information
/// is stored in the context. This
means that you should not use global variables in plug-ins.
/// </remarks>
protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new InvalidPluginExecutionException("localContext");
}
//Get the target entity record
localContext.Trace("Starting Plugin: " + this.GetType().Name);
string pluginMessage =
localContext.PluginExecutionContext.MessageName.ToLower();
localContext.Trace("Message: " + pluginMessage);
//Call the respective handlers of the message
switch (pluginMessage)
{
case PluginMessages.Create:
{
HandleMessage_Create(localContext);
break;
}
}
}
#region Message
Handlers
private void HandleMessage_Create(LocalPluginContext localContext)
{
localContext.Trace("OrderClose_PreOperation plugin
called");
try
{
if
(localContext.PluginExecutionContext.InputParameters.Contains(PluginParameters.Target)
&&
localContext.PluginExecutionContext.InputParameters[PluginParameters.Target]
is Entity)
{
localContext.Trace("Getting Target");
Entity targetEntity =
(Entity)localContext.PluginExecutionContext.InputParameters[PluginParameters.Target];
if (targetEntity.Attributes.Contains("salesorderid")
&& targetEntity["salesorderid"] != null)
{
EntityReference
salesOrder = targetEntity.GetAttributeValue<EntityReference>("salesorderid");
EntityCollection
entityCollection = new EntityCollection();
string fetchConfigXML = string.Format(@"<fetch version='1.0' output-format='xml-platform'
mapping='logical' distinct='false'>
<entity name='salesorder'>
<attribute name='name' />
<attribute name='customerid' />
<attribute name='statuscode' />
<attribute name='totalamount' />
<attribute name='salesorderid' />
<attribute name='indskr_reasonforcancellation' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='salesorderid' operator='eq' value='{0}' />
</filter>
</entity>
</fetch>",
salesOrder.Id);
entityCollection =
localContext.OrganizationService.RetrieveMultiple(new FetchExpression(fetchConfigXML));
if (entityCollection != null && entityCollection.Entities != null && entityCollection.Entities.Count > 0)
{
if (!entityCollection.Entities[0].Contains("indskr_reasonforcancellation"))
{
throw new InvalidPluginExecutionException("Business validation Failed, Please
Select the Reason for Cancellation field value!");
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
Steps:

Comments
Post a Comment