I'm creating a plugin to, when the user set the status of a task in the crm, verify all tasks that are associated with a incident. If, there's no tasks opened, the incident should be closed.
When I use a profile to debug the plugin it works fine, but otherwise nothing happens.
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
EntityReference entity = (EntityReference)context.InputParameters["EntityMoniker"];
ColumnSet cols = new ColumnSet();
cols.AllColumns = true;
Entity entityComplete = service.Retrieve("task", entity.Id, cols);
if (((OptionSetValue)entityComplete.Attributes["statecode"]).Value == 0) //se o status for cancelado ou concluído
{
if (entityComplete.Attributes.Keys.Contains("regardingobjectid") && ((EntityReference)entityComplete.Attributes["regardingobjectid"]).LogicalName == "incident")
{
QueryExpression query = new QueryExpression();
query.EntityName = "task";
query.ColumnSet = cols;
query.LinkEntities.Add(new LinkEntity("task", "incident", "regardingobjectid", "incidentid", JoinOperator.Inner));
query.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));
query.Criteria.AddCondition(new ConditionExpression("activityid", ConditionOperator.NotEqual, entityComplete.Id));
query.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, ((EntityReference)entityComplete.Attributes["regardingobjectid"]).Id));
EntityCollection collection = service.RetrieveMultiple(query);
if (collection.Entities.Count == 0)
{
Entity incident = service.Retrieve("incident", ((EntityReference)entityComplete.Attributes["regardingobjectid"]).Id, cols);
SetStateRequest setState = new SetStateRequest();
setState.EntityMoniker = new EntityReference();
setState.EntityMoniker.Id = incident.Id;
setState.EntityMoniker.LogicalName = incident.LogicalName;
setState.State = new OptionSetValue(1);
SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);
}
}
}
Somebody can help me? Thanks.
Try registering your plugin also for the SetStateDynamicEntity
message, in addition to doing the same for SetState
. From my experience, entities need to be registered for both in order to work, although I'm not 100% clear on whether it's necessary, I know for a fact that it works. Several searches did not give me a definitive answer. Check out this popular CRM blog with the same suggestion. http://nishantrana.wordpress.com/2010/01/29/plug-in-for-setstate-and-setstatedynamicentity-messages/
I know with entities that I've worked with, failing to register for SetStateDynamic will cause the plugin not to trigger.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.