I have a requirement to validate on change of state, so if a user deactivates a record and the end date is null an error should be thrown, i have registered the plugin at the pre-stage of setstate dynamic entity
But I keep getting a null exception error
The code
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Plugins.Event
{
public class SetStatePreStage : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
PluginHelper helper = new PluginHelper();
IPluginExecutionContext context = helper.PluginContext;
const string entityname = "new_event";
const string status = "statecode";
const string inactivedate = "new_enddate";
// Initialize execution contexts
helper.Init(this, serviceProvider);
helper.Trace("Validating Primary entity context");
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is EntityReference
&& context.PrimaryEntityName == entityname)
{
// Obtain the target business entity from the input parameters.
helper.Trace("Obtaining the target business entity from input parameters");
EntityReference new_event = (EntityReference)context.InputParameters["Target"];
int statecode = ((OptionSetValue)context.InputParameters[status]).Value;
DateTime enddate = ((DateTime)context.InputParameters[inactivedate]);
// int statecode = ((OptionSetValue)new_event[status]).Value;
// DateTime enddate = ((DateTime)new_event[inactivedate]);
//active is 0 inactive is 1
if (statecode == 1)
{
if ( enddate == null)
{
throw new InvalidPluginExecutionException("You Cannot deactivate without setting end date time");
}
else {
throw new InvalidPluginExecutionException("This did not work");
}
}
}
}
}
}
I initially used entity instead of entity reference but the code did nothing
but now the code gives and error and does not run either
Also whats the best way to achieve this task, use Pre-Image?
Also was the best way to check for a null field in date
if (enddate== null)
or
(!entity.Contains(enddate))
*This post is locked for comments

Report
All responses (
Answers (