locked
CRM PreCaseDelete plugin RRS feed

  • Question

  • I am trying to update a field on all associated accounts when a custom case entity record gets deleted (many to many relationship with accounts). I am querying the middle entity that links the accounts with the cases to find all of the accounts that are related to the case going to be deleted. The problem is that my linq query will return all records except the ones associated with the case about to be deleted. My plugin is running in pre-operation so I am not understanding why this occurring. When I query the related records using LinqPad the same query returns the related accounts but when the plugin fires, it won't return the related records. I am using tracing to debug and I have proved that I am pulling the id for the case to be deleted. I changed my query from == to != to prove that query is pulling records from the correct table (the query pulls all records except the related as you would expect when the plugin fires). Any insight will be most appreciated.

           protected void ExecutePreCaseDelete(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }

                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
                ITracingService tracingService = localContext.TracingService;
                tracingService.Trace("Implemented tracing service succesfully!");
                Entity entity = null;
                new_matter_case deletedCase = null;
                int totalCases = 0;

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    entity = context.InputParameters["Target"] as Entity;

                }
                else
                {
                    if (context.MessageName == "Delete")
                    {
                        EntityReference entityRef = context.InputParameters["Target"] as EntityReference;
                        deletedCase = service.Retrieve(new_matter_case.EntityLogicalName, entityRef.Id, new ColumnSet(true)).ToEntity<new_matter_case>();
                    }
                    else
                    {
                        return;
                    }
                }
                if (deletedCase.new_AccountLink != null && deletedCase.new_AccountLink.LogicalName == Account.EntityLogicalName)
                {
                    using (CRMPluginContext orgContext = new CRMPluginContext(service))
                    {
                       
                        var accounts = from c in orgContext.new_new_matter_case_accountSet
                                       where c.new_matter_caseid == deletedCase.new_matter_caseId
                                       select c.accountid;

                        foreach (var account in accounts)
                            {
                            var cases = from c in orgContext.new_new_matter_case_accountSet
                                            join d in orgContext.new_matter_caseSet on c.new_matter_caseid equals d.new_matter_caseId
                                            where c.accountid == account
                                            && c.new_matter_caseid != deletedCase.new_matter_caseId
                                            select new new_new_matter_case_account
                                            {
                                                Id = c.Id
                                            };

                            totalCases = cases.ToList().Count;

                            if (totalCases > 0)
                                {
                                Account updateAccount = service.Retrieve(Account.EntityLogicalName, account.Value, new ColumnSet(true)).ToEntity<Account>();
                                updateAccount.new_CaseLitigation = true;
                                service.Update(updateAccount);
                                }
                                else
                                {
                                Account updateAccount = service.Retrieve(Account.EntityLogicalName, account.Value, new ColumnSet(new string[] { "new_Caselitigation" })).ToEntity<Account>();
                                updateAccount.new_CaseLitigation = false;
                                service.Update(updateAccount);
                                };

                            }

                    }
                }
            }

        }
    }

    Thursday, July 20, 2017 3:25 PM

All replies

  • Maybe try registering the plugin step in the pre-validation stage.
    Thursday, July 20, 2017 9:14 PM
  • Something else to try is to use a EntityImage (pre image) to get the data from the case to be deleted, rather than using a Retrieve. The EntityImage should contain the data before any changes happen, whereas the Retrieve runs within the transaction, so may pick up changes

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Friday, July 21, 2017 9:29 AM
    Moderator