Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

ServiceClient not working with CrmServiceContext from Early Bound Generated Class #606

Closed
lcbotha-braintree opened this issue Nov 30, 2023 · 2 comments
Labels

Comments

@lcbotha-braintree
Copy link

lcbotha-braintree commented Nov 30, 2023

Describe the bug
I'm trying to use FakeXrmEasy to mock the context and create an IOrganizationService which is then used by the rest of the C# .net core web API functions to query data. The existing calls uses the CrmServiceContext to query the data from dataverse while it seems like if we pass in the service client created by FakeXrmEasy, it doesn't work and we get an error querying the data.

To Reproduce

public class GetPositionSeatsQueryHandlerTest : FakeXrmEasyTestBase
	{
		private readonly bthr_PositionSeat _seat;
		private readonly bthr_Employee _employee;

		public GetPositionSeatsQueryHandlerTest( )
		{
			_employee = new bthr_Employee()
			{
				Id = Guid.NewGuid(),
				bthr_FirstName = "Peter",
				bt_FullName = "Peter Rabbit"
			};

			_seat = new bthr_PositionSeat()
			{
				Id = Guid.NewGuid(),
				bthr_PositionSeatId = Guid.NewGuid(),
				bthr_SeatName = "123",
				bthr_Description = "CEO",
				bt_HasChildren = false,
				bt_SeatStatus = bt_positionseat.Filled,
				bt_Manager = false,
				cr1f2_Department = new EntityReference()
				{
					Id = Guid.NewGuid(),
					Name = "Department"
				},
				cr1f2_Division = new EntityReference()
				{
					Id = Guid.NewGuid(),
					Name = "Division"
				},
				cr1f2_LegalEntity = new EntityReference()
				{
					Id = Guid.NewGuid(),
					Name = "LegalEntity"
				},
				bthr_Employee = _employee.ToEntityReference()
			};
		}

		[Fact]
		public async Task Should_Not_Throw_Exception( )
		{
			_context.Initialize(new List<Entity>() {
				_employee,
				_seat
			});

			var handler = new GetPositionSeatsQueryHandler();
			var @event = new GetPositionSeatsQuery
			{
				TenantServiceClient = _service
			};

			var data1 = _context.CreateQuery<bthr_PositionSeat>().FirstOrDefault();
			// Works. Using the IOrganizationService then for the CrmServiceContext seems to then fail next.

			var crmServiceContext = new CrmServiceContext(_service);
			var data2 = crmServiceContext.CreateQuery<bthr_PositionSeat>().FirstOrDefault();
			// Fails with error: System.ArgumentNullException : Value cannot be null. (Parameter 'key')
		}
	}

Expected behavior
The IOrganizationService from FakeXrmEasy does not work when using it within the CrmServiceContext generated by Early Bound Generators.

** FakeXrmEasy and Dynamics 365 / CRM version **
FakeXrmEasy.v9 (3.3.3)

@lcbotha-braintree
Copy link
Author

I've managed to resolve it. If anyone else experience the issue its because you need to use the ToEntityRefernce function instead and not manually assign the EntityReference like I did.

@jordimontana82
Copy link
Owner

@lcbotha-braintree Glad you got it working. I believe it must be the same issue as DynamicsValue/fake-xrm-easy#107.

Btw this repo is for legacy version v1, the new versions (2.x and 3x) live in several repos under the DynamicsValue organisation: https://github.com/DynamicsValue/fake-xrm-easy

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants