Skip to content

Instantly share code, notes, and snippets.

@tswann
Created February 14, 2013 16:40
Show Gist options
  • Save tswann/4954084 to your computer and use it in GitHub Desktop.
Save tswann/4954084 to your computer and use it in GitHub Desktop.
protected void ExecutePreValidateContactDelete(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
_service = localContext.OrganizationService;
EntityReference contact = (EntityReference)localContext.PluginExecutionContext.InputParameters["Target"];
if (HasOpenTasks(contact.Id))
{
throw new InvalidPluginExecutionException("This Contact has open Tasks associated with it.\n\nPlease resolve all outstanding tasks before deleting.");
}
}
private bool HasOpenTasks(Guid contactId)
{
var tasks = GetRelatedTasks(contactId);
return tasks.Any();
}
private IEnumerable<Entity> GetRelatedTasks(Guid contactId)
{
QueryExpression query = new QueryExpression()
{
EntityName = "task",
ColumnSet = new ColumnSet(true),
};
const int STATE_OPEN = 0;
query.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, contactId));
query.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, STATE_OPEN));
return _service.RetrieveMultiple(query).Entities.ToList<Entity>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment