Skip to content

RetrieveRequest doesn't throw RecordNotFoundByEntityKey error #280

Description

@SvenRahnSIT

Hi,

I expect this to work because this is what the CRM does but FXE returns InvalidEntityKeyOperation instead.

[TestClass]
public class RetrieveByAlternateKeyTests
{
    [TestMethod]
    public void Retrieve_UnknownAlternateKey_ThrowsErrorCode()
    {
        // Arrange
        var context = MiddlewareBuilder
            .New()
            .AddCrud()
            .AddFakeMessageExecutors()
            .AddGenericFakeMessageExecutors()
            .AddPipelineSimulation()
            .UsePipelineSimulation()
            .UseMessages()
            .SetLicense(FakeXrmEasyLicense.Commercial)
            .Build();

        context.EnableProxyTypes(typeof(Account).Assembly);
        context.InitializeMetadata(typeof(Account).Assembly);

        void AddAlternateKey(string entityLogicalName, params string[] keyAttributes)
        {
            var metadata = context.GetEntityMetadataByName(entityLogicalName);
            metadata.SetSealedPropertyValue(nameof(metadata.Keys), new EntityKeyMetadata[]
            {
                new() { KeyAttributes = keyAttributes }
            });
            context.SetEntityMetadata(metadata);
        }

        AddAlternateKey(Account.EntityLogicalName, Account.Fields.AccountNumber);

        var service = context.GetOrganizationService();

        // Act + Assert
        var exception = Assert.Throws<FaultException<OrganizationServiceFault>>(() =>
        {
            service.Execute(new RetrieveRequest()
            {
                Target = new EntityReference(Account.EntityLogicalName, Account.Fields.AccountNumber, "something"),
                ColumnSet = new ColumnSet(false),
            });
        });
        Assert.AreEqual(ErrorCodes.RecordNotFoundByEntityKey, (ErrorCodes)exception.Detail.ErrorCode);
    }
}

Activity

  1. jordimontana82 commented on Sep 18, 2026

    @jordimontana82
    Contributor

    Thanks Sven, I'll investigate for the next minor release.

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