From 49016621cb7b90ea7852f4e9295ce3bba9cd690e Mon Sep 17 00:00:00 2001 From: James Gunn Date: Fri, 5 Jan 2024 09:24:01 +0000 Subject: [PATCH] Search on all previous names (#1054) --- ...ContactsByLastNameAndDateOfBirthHandler.cs | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetContactsByLastNameAndDateOfBirthHandler.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetContactsByLastNameAndDateOfBirthHandler.cs index d2ee4a233..48c837beb 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetContactsByLastNameAndDateOfBirthHandler.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetContactsByLastNameAndDateOfBirthHandler.cs @@ -8,10 +8,6 @@ public class GetContactsByLastNameAndDateOfBirthHandler : ICrmQueryHandler Execute(GetContactsByLastNameAndDateOfBirthQuery query, IOrganizationServiceAsync organizationService) { - var lastNameFilter = new FilterExpression(LogicalOperator.Or); - lastNameFilter.AddCondition(Contact.Fields.LastName, ConditionOperator.Equal, query.LastName); - lastNameFilter.AddCondition(Contact.Fields.dfeta_PreviousLastName, ConditionOperator.Equal, query.LastName); - var queryExpression = new QueryExpression(Contact.EntityLogicalName) { ColumnSet = query.ColumnSet, @@ -25,7 +21,14 @@ public async Task Execute(GetContactsByLastNameAndDateOfBirthQuery qu }, Filters = { - lastNameFilter + new FilterExpression(LogicalOperator.Or) + { + Conditions = + { + new ConditionExpression(Contact.Fields.LastName, ConditionOperator.Equal, query.LastName), + new ConditionExpression(dfeta_previousname.EntityLogicalName, dfeta_previousname.Fields.dfeta_name, ConditionOperator.Equal, query.LastName) + } + } } }, Orders = @@ -33,11 +36,34 @@ public async Task Execute(GetContactsByLastNameAndDateOfBirthQuery qu new OrderExpression() { AttributeName = Contact.Fields.LastName }, new OrderExpression() { AttributeName = Contact.Fields.FirstName }, new OrderExpression() { AttributeName = Contact.Fields.dfeta_TRN }, + }, + LinkEntities = + { + new LinkEntity( + Contact.EntityLogicalName, + dfeta_previousname.EntityLogicalName, + Contact.PrimaryIdAttribute, + dfeta_previousname.Fields.dfeta_PersonId, + JoinOperator.LeftOuter) + { + LinkCriteria = new FilterExpression() + { + Conditions = + { + new ConditionExpression(dfeta_previousname.Fields.dfeta_Type, ConditionOperator.Equal, (int)dfeta_NameType.LastName), + new ConditionExpression(dfeta_previousname.Fields.StateCode, ConditionOperator.Equal, (int)dfeta_previousnameState.Active) + } + } + } } }; var response = await organizationService.RetrieveMultipleAsync(queryExpression); - return response.Entities.Select(e => e.ToEntity()).ToArray(); + return response.Entities + .Select(e => e.ToEntity()) + .GroupBy(c => c.Id) + .Select(g => g.First()) + .ToArray(); } }