Hi all,
I am developing a plugin for auto-number on a field of any entity.plugin working fine and done following registration steps:
1.Message=Create
2.PrimaryEntity=none
3.Scoundry Entity=none
4.Stage of execution=PreOperation
5.Execution Mode=Synchronous
6.Development
Problem:
When I am uninstalling solution from then getting following error:
Auto Number Solution cannot be deleted due to dependencies from other components in the system. Remove all dependencies to allow for solution deletion.
The above error is due to sdkmessageprocessingstep are not deleting after deleted record from CRM grid.
CODE
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
//</snippetAssignAutoNumberPlugin2>
string messageName = context.MessageName;
if (entity.LogicalName == "dots_autonumber")
{
if (messageName.ToLower() == "create")
{
SdkMessageProcessingStep step = new SdkMessageProcessingStep
{
AsyncAutoDelete = false,
Mode = new OptionSetValue((int)CrmPluginStepMode.Synchronous),
Name = _pluginTypeName + ": " + entity.Attributes["new_name"].ToString(),
EventHandler = new EntityReference("plugintype", GetPluginTypeId(OrganizationService)),
Rank = 1,
SdkMessageId = new EntityReference("sdkmessage", GetMessageId(OrganizationService)),
Stage = new OptionSetValue((int)CrmPluginStepStage.PreOperation),
//Stage = new OptionSetValue((int)CrmPluginStepStage.PostOperation),
SupportedDeployment = new OptionSetValue((int)CrmPluginStepDeployment.ServerOnly),
SdkMessageFilterId = new EntityReference("sdkmessagefilter", GetSdkMessageFilterId(entity.Attributes["new_targetentitylogicalname"].ToString(), OrganizationService)),
Description = entity.Attributes["new_targetattributelogicalname"].ToString()
};
var SdkMessageProcessingStepId = OrganizationService.Create(step);
entity.Attributes.Add("new_sdkmessageprocessingstepid", SdkMessageProcessingStepId.ToString());
}
}
else
{
var entityDetail = GetEntityByName(entity.LogicalName, OrganizationService);
if (entityDetail != null)
{
if (entity.Attributes.Contains(entityDetail.TargetAttributeLogicalName) == false)
{
var InitalValue = GetNewCode(entityDetail);
var setValue = (InitalValue == null || InitalValue == "") ? "0" : InitalValue;
string[] splitValues;
if (setValue.Contains("#"))
{
splitValues = setValue.Split('#');
entity.Attributes.Add(entityDetail.TargetAttributeLogicalName, splitValues[0] + "" + splitValues[1]);
int incrementValue = Convert.ToInt32(splitValues[1]);
IncreaseCurrentNumber(entity.LogicalName, incrementValue, OrganizationService);
}
else
{
entity.Attributes.Add(entityDetail.TargetAttributeLogicalName, setValue);
int incrementValue = Convert.ToInt32(setValue);
IncreaseCurrentNumber(entity.LogicalName, incrementValue, OrganizationService);
}
}
}
}
}
else if (context.InputParameters["Target"] is EntityReference)
{
if (context.PrimaryEntityName == "dots_autonumber")
{
if (context.MessageName == "Delete")
{
Guid id = ((EntityReference)context.InputParameters["Target"]).Id;
DSAutoNumber entity2 = OrganizationService.Retrieve(context.PrimaryEntityName, id, new ColumnSet("new_sdkmessageprocessingstepid")).ToEntity<DSAutoNumber>();
OrganizationService.Delete("sdkmessageprocessingstep", new Guid(entity2.SdkMessageProcessingStepId.ToString()));
}
}
}
Any suggestion plz.