delegateas / XrmMockup

Engine which simulates your exact Dynamics 365/CRM instance locally including all of it's logic!
Other
71 stars 28 forks source link

Owning Business Unit not set on Target Entity during Create Post-Operation #92

Open bo-stig-christensen opened 4 years ago

bo-stig-christensen commented 4 years ago

Describe the bug In a Create Post-Operation plugin on an entity with calling user as the owner, Target Entity does not have an 'owningbusinessunit' attribute defined in Mockup Tests. However, in D365 this attribute is present in Target Entity in Post-Operation stage. Assuming same issue with a team as owner regarding 'owningteam' attribute.

No workaround possible, even setting the raw attribute becomes null in Mockup Test.

To Reproduce

[TestMethod]
public void GivenUserInBusinessUnit_WhenCreatingAccount_ExpectOwnerToBeBusinessUnit()
{
    // Arrange
    var userDenmark = new SystemUser
    {
        FirstName = "Luke",
        LastName = "Skywalker",
        InternalEMailAddress = "luke@test.com",
        BusinessUnitId = _businessUnitDenmark.ToEntityReference()
    };
    userDenmark = Crm.CreateUser(OrgAdminService, userDenmark, SecurityRoles.Salesperson) as SystemUser;
    OrgService = Crm.CreateOrganizationService(userDenmark.Id);

    // Act
    var account = new Account
    {
        Name = "Test Account"
    };
    account.Id = OrgService.Create(account);

    // Assert
    var accountFromD365 = Account.Retrieve(OrgAdminService, account.Id);
    accountFromD365.OwnerId.Id.Should().Be(_businessUnitDenmark.Id);
}

A plugin is in place that contains this fragment:

using (var context = new Xrm(localContext.OrganizationAdminService))
{
    var owningBusinessUnitTeam = context.TeamSet.FirstOrDefault(x => x.BusinessUnitId.Id == targetEntity.OwningBusinessUnit.Id);
    if (owningBusinessUnitTeam != null)
        localContext.OrganizationAdminService.Update(new Account(targetEntity.Id) { OwnerId = owningBusinessUnitTeam.ToEntityReference() });

The usage of OwningBusinessUnit.Id throws an exception in Mockup, but not in D365.

Expected behavior OwningBusinessUnit should be populated in Create Post-Operation as it is in D365.

Environment

MattTrinder1 commented 4 years ago

This looks to be related to #101 where other system field values are not set in the post operation plugin - it should be possible to add OwningBusinessUnit into the list of system attributes so that it is included.

104 contains the work to resolve this.