Hi,
I am trying to write a plugin on my own. I am fairly new to crm.
This plugin is to update a lookup field before the record status reason changes. So I registered it on Update message Pre Operation event. But I don't think it is doing anything it has to do! Here is my code, please let me know the mistakes I've done and
I will be happy to learn from my mistakes. Thanks
protected void ExecutePreTaskUpdate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
//create entity context
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "task")
return;
try
{
if (entity.Contains("new_taskissuephase"))
{
OptionSetValue taskStateCode = entity.GetAttributeValue<OptionSetValue>("statecode");
QueryExpression query = new QueryExpression
{
EntityName = "new_taskissuephase",
Criteria =
{
Conditions =
{
new ConditionExpression("new_taskissuephase",ConditionOperator.Equal,"Tech Work Complete")
}
}
};
EntityCollection resultsCollection = localContext.OrganizationService.RetrieveMultiple(query);
foreach (Entity result in resultsCollection.Entities)
{
result.Attributes["activityid"] = new EntityReference(entity.LogicalName, entity.Id);
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}