1

I have this piece of code in my Dynamics 365 plugin,

 private int? RetrieveCurrentUsersSettings(IOrganizationService service)
    {
        var currentUserSettings = service.RetrieveMultiple(
            new QueryExpression("usersettings")
            {
                ColumnSet = new ColumnSet("timezonecode"),
                Criteria = new FilterExpression
                {
                    Conditions =
                    {
                        new ConditionExpression("systemuserid", ConditionOperator.EqualUserId)
                    }
                }
            }).Entities[0].ToEntity<Entity>();

        return (int?)currentUserSettings.Attributes["timezonecode"];
    }

and I am writing a UnitTest for it with FakeXrm, and while debugging I am getting an error, in this query expression.

Here is my fake Xrm code.

  Entity systemUser = new Entity("systemuser");
        systemUser.Id = Guid.NewGuid();

        Entity userSettings = new Entity("usersettings");
        userSettings.Id = Guid.NewGuid();
        userSettings["timezonecode"] = 71;
        userSettings["systemuserid"] = systemUser.ToEntityReference();

        fakedContext.Initialize(new List<Entity>()
        {
            workOrder, owner, invoiceNote, userSettings
        });

The question is, how to create usersettings entity in FakeXrm and provide proper attributes to it.

VolcanoAsh
  • 11
  • 3

2 Answers2

1

I am glad that you followed my suggestion to ask the question on StackOverflow.

Anyway you should have pointed out that you asked the same question on GitHub on the FakeXrmEasy repository and that the solution was provided there with this answer.

Long story short, the problem with your UnitTest was that you were missing fakedContext.CallerId = systemUser.ToEntityReference(); and that was needed because you were using ConditionOperator.EqualUserIdin your query.

Best Regards, Betim Beja.

Betim Beja
  • 41
  • 1
  • 5
0

I have found a solution

           Entity systemUser = new Entity("systemuser");
            systemUser.Id = Guid.NewGuid();
            fakedContext.CallerId = systemUser.ToEntityReference();
            IOrganizationService service = fakedContext.GetOrganizationService();
            Entity userSettings = new Entity("usersettings");
            userSettings.Id = Guid.NewGuid();
            userSettings["timezonecode"] = 71;
            userSettings["systemuserid"] = systemUser.ToEntityReference();
VolcanoAsh
  • 11
  • 3