Created
February 14, 2013 16:40
-
-
Save tswann/4954084 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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