public override void Execute() { var record = _crmContext.RecordCache.Get(_alias); var md = GlobalTestingContext.Metadata.GetEntityMetadata(record.LogicalName, EntityFilters.Relationships); var relationship = md.ManyToManyRelationships.FirstOrDefault(r => (r.Entity1LogicalName == record.LogicalName && r.Entity2LogicalName == _relatedEntityName) || (r.Entity1LogicalName == _relatedEntityName && r.Entity2LogicalName == record.LogicalName)); if (relationship == null) { throw new TestExecutionException(Constants.ErrorCodes.N_N_RELATIONSHIP_NOT_FOUND, record.LogicalName, _relatedEntityName); } Logger.WriteLine($"Using relationship {relationship.SchemaName}"); EntityReferenceCollection records = new EntityReferenceCollection(); foreach (var row in _expectedRecords.Rows) { var lookupValue = ObjectConverter.GetLookupValue(_crmContext, row[Constants.SpecFlow.TABLE_VALUE], _relatedEntityName); if (lookupValue == null) { throw new TestExecutionException(Constants.ErrorCodes.RECORD_NOT_FOUND, row[Constants.SpecFlow.TABLE_VALUE], _relatedEntityName); } records.Add(lookupValue); } var currentRecordFieldName = relationship.Entity1LogicalName == record.LogicalName ? relationship.Entity1IntersectAttribute : relationship.Entity2IntersectAttribute; var relatedFieldName = relationship.Entity1LogicalName == _relatedEntityName ? relationship.Entity1IntersectAttribute : relationship.Entity2IntersectAttribute; var query = new QueryExpression(relationship.IntersectEntityName); query.ColumnSet.AddColumn(relatedFieldName); query.Criteria.AddCondition(currentRecordFieldName, ConditionOperator.Equal, record.Id); query.Criteria.AddCondition(relatedFieldName, ConditionOperator.In, records.Select(r => (object)r.Id).ToArray()); var result = GlobalTestingContext.ConnectionManager.CurrentConnection.RetrieveMultiple(query); Assert.AreEqual(records.Count, result.Entities.Count, $"Different records: {string.Join(", ", records.Where(r => !result.Entities.Select(e => e.GetAttributeValue<Guid>(relatedFieldName)).Contains(r.Id)).Select(r => r.Name))}"); }