Skip to content

Commit

Permalink
Search on all previous names (#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Jan 5, 2024
1 parent 51bfeab commit 4901662
Showing 1 changed file with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ public class GetContactsByLastNameAndDateOfBirthHandler : ICrmQueryHandler<GetCo
{
public async Task<Contact[]> 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,
Expand All @@ -25,19 +21,49 @@ public async Task<Contact[]> 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 =
{
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<Contact>()).ToArray();
return response.Entities
.Select(e => e.ToEntity<Contact>())
.GroupBy(c => c.Id)
.Select(g => g.First())
.ToArray();
}
}

0 comments on commit 4901662

Please sign in to comment.