0

I am creating a plugin for Dynamics 365 in C#. I am trying to make request using fetchXML, however I am getting the following error: Object reference not set to an instance of an object.

Here is my code:

    public void Execute(IServiceProvider serviceProvider)
    {
        // Obtain the tracing service
        ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        // Obtain the execution context from the service provider.  
        IPluginExecutionContext context = (IPluginExecutionContext)
            serviceProvider.GetService(typeof(IPluginExecutionContext));

        IOrganizationService svc = (IOrganizationService)serviceProvider.GetService(typeof(IOrganizationService));

        try
        {
            var id = context.InputParameters["leadid"] as string;

            var fetchXml = $@"<?xml version=""1.0"" encoding=""utf-16""?>
                <fetch>
                  <entity name=""lead"">
                    <attribute name=""leadid"" />
                    <link-entity name=""new_sitecorevisit"" from=""new_new_parent_leadid"" to=""leadid"">
                      <attribute name=""new_visitduration"" />
                      <filter>
                        <condition attribute=""new_new_parent_leadid"" operator=""eq"" value=""{id}"" />
                      </filter>
                    </link-entity>
                  </entity>
                </fetch>";

            EntityCollection result = svc.RetrieveMultiple(new FetchExpression(fetchXml));

            context.OutputParameters["data"] = result;
        }

        catch (Exception ex)
        {
            tracingService.Trace("Error: {0}", ex.ToString());
            throw;
        }
    }

I believe that it is being thrown when this line is reached: EntityCollection result = svc.RetrieveMultiple(new FetchExpression(fetchXml)); as removing this fixes the issue. However, I do not know why it is throwing this error. I have checked to see if it is an error with the fetchXml variable or the id variable, however these are passing as expected.

I have tested retrieving the data using JavaScript and the data is returned as expected, however, this is not useful for creating a .NET plugin.

Is there a mistake that I am making?

Michał T
  • 267
  • 5
  • 16
dwewers
  • 67
  • 6
  • 1
    What is the *actual* value of `svc`? – Hans Kesting Aug 30 '22 at 08:53
  • svc is the `IOrganizationService` which is equal too `(IOrganizationService)serviceProvider.GetService(typeof(IOrganizationService))` – dwewers Aug 30 '22 at 08:55
  • 1
    What Hans is asking is to set a breakpoint somewhere before the exception, and ensure that `svc` is a non null value. – Joost00719 Aug 30 '22 at 08:56
  • And to elaborate further: that Getservice call *should* return a value, but *does* it really? We cannot say from here – Hans Kesting Aug 30 '22 at 08:58
  • What happens if, instead, you use `IOrganizationService svc = serviceProvider.GetRequiredService();` – DavidG Aug 30 '22 at 09:04
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Stefan Aug 30 '22 at 09:05
  • @DavidG Using that throws the error `Cannot resolve symbol "GetRequiredService"` – dwewers Aug 30 '22 at 09:10
  • Do you have a `using Microsoft.Extensions.DependencyInjection;` line? – DavidG Aug 30 '22 at 09:22
  • @DavidG That fixed that error, but now it returns `"Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"` – dwewers Aug 30 '22 at 09:30

0 Answers0