Hi All,
I am new to plugins development. I am trying to retrieve 3 attributes from an entity called 'sample entity', then subtract 2 attributes and update the third one.
example: attributes are
field 1, field 2, field 3.
field 3 = field 1 - field 2. These attributes are decimal numbers in CRM 2011
I want the plugin to fire when the form is updated. So I have made the execution on post-operation.
The code is below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
namespace samplePlugin
{
public class Plugin : IPlugin
{
public void Execute(IServiceProvider serviceprovider)
{
// 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 parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
{
// Retrieve the days used and subtract it from Total Allowance
decimal oDaysUsed = entity.GetAttributeValue<decimal>("tcrmb_daysused");
decimal oTotalAllowance = entity.GetAttributeValue<decimal>("tcrmb_totalallowance");
// decimal oDaysAvailable = entity.GetAttributeValue<decimal>("tcrmb_daysavailable");
decimal oAvailable = oTotalAllowance - oDaysUsed;
entity.Attributes.Add("tcrmb_daysavailable", oAvailable);
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
}
}
Kindly have a look and let me know please
Regards,
Paul