Skip to content

Related entity not found with RetrieveRequest #271

Description

@SvenRahnSIT

Hi,

I expected the following test to work, but somehow the related parent account is never returned:

using FakeXrmEasy.Abstractions;
using FakeXrmEasy.Abstractions.Enums;
using FakeXrmEasy.Abstractions.Integrity;
using FakeXrmEasy.Integrity;
using FakeXrmEasy.Middleware;
using FakeXrmEasy.Middleware.Crud;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using StrategicIT.CRM.Tools.Entities;
using System;

namespace Plugins.TestsFakeXrmEasy2.Tests
{
    [TestClass]
    public class TestRelatedAccounts
    {
        [TestMethod]
        public void Test()
        {
            // Arrange
            var context = MiddlewareBuilder
                .New()
                .SetLicense(FakeXrmEasyLicense.Commercial)
                .AddCrud()
                .UseCrud()
                .Build();
            context.EnableProxyTypes(typeof(Account).Assembly);
            context.SetProperty<IIntegrityOptions>(new IntegrityOptions() { ValidateEntityReferences = true });
            context.AddRelationship(Account.Fields.Referencingaccount_parent_account, new XrmFakedRelationship()
            {
                RelationshipType = XrmFakedRelationship.FakeRelationshipType.OneToMany,
                Entity1LogicalName = Account.EntityLogicalName,
                Entity1Attribute = Account.Fields.ParentAccountId,
                Entity2LogicalName = Account.EntityLogicalName,
                Entity2Attribute = Account.Fields.Id,
            });

            var service = context.GetOrganizationService();
            var parentAccountId = service.Create(new Account
            {
                Name = "Parent Account"
            });
            var subAccountId = service.Create(new Account
            {
                Name = "Sub Account",
                ParentAccountId = new EntityReference(Account.EntityLogicalName, parentAccountId)
            });
            Console.WriteLine($"Created parent account: {parentAccountId}");
            Console.WriteLine($"Created sub account: {subAccountId}");

            // Act
            var retrieveRequest = new RetrieveRequest
            {
                Target = new EntityReference(Account.EntityLogicalName, subAccountId),
                ColumnSet = new ColumnSet(true),
                RelatedEntitiesQuery = new RelationshipQueryCollection
                {
                    {
                        new Relationship()
                        {
                            SchemaName = Account.Fields.Referencingaccount_parent_account,
                            PrimaryEntityRole = EntityRole.Referencing
                        },
                        new QueryExpression()
                        {
                            EntityName = Account.EntityLogicalName,
                            ColumnSet = new ColumnSet(true),
                        }
                    }
                }
            };
            var retrieveResponse = (RetrieveResponse)service.Execute(retrieveRequest);
            var retrievedAccount = retrieveResponse.Entity.ToEntity<Account>();

            // Assert
            var retrievedAccountId = retrievedAccount.Id;
            Console.WriteLine($"Retrieved sub account: {retrievedAccountId}");
            var retrievedParentAccountId = retrievedAccount.Referencingaccount_parent_account?.Id;
            Console.WriteLine($"Retrieved parent account: {retrievedParentAccountId}");

            Assert.AreEqual(subAccountId, retrievedAccountId);
            Assert.AreEqual(parentAccountId, retrievedParentAccountId);
        }
    }
}

I guess something is wrong here https://github.com/DynamicsValue/fake-xrm-easy-core/blob/f251481a2e07a3ae40ce2357033c9dd751f309b3/src/FakeXrmEasy.Core/Middleware/Crud/FakeMessageExecutors/RetrieveRequestExecutor.cs#L159
I don't know why the constructed LinkEntity in the true-branch of the if-block is never really used.

Console output:

Created parent account: 642670f8-7fc8-4b1e-98a9-6808ad70b269
Created sub account: 259d12d9-fd49-47c2-8f64-790d522ac165
Retrieved sub account: 259d12d9-fd49-47c2-8f64-790d522ac165
Retrieved parent account: 

Activity

  1. jordimontana82 commented on Aug 17, 2026

    @jordimontana82
    Contributor

    Thx @SvenRahnSIT , I'll look into this.

  2. added a commit that references this issue on Sep 10, 2026
    bcfcb60
  3. jordimontana82 commented on Sep 11, 2026

    @jordimontana82
    Contributor

    This made it to 2.9.5 too @SvenRahnSIT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions