2
votes

Error: 0x80040203 Invalid Argument

I'm new to Dynamics. In preparation for getting lookup values from external data source into Dynamics via a plugin, I wanted to test first with hard coded values with this code. But after I registered the assembly, data provider and data source, I created a virtual entity in dynamics which I linked up to a field (lookup type) on a form. After publishing, clicking on the field throws error - Invalid Argument

using System;
using Microsoft.Xrm.Sdk;

namespace Hardcoded.Names.Plugin
{
public class NamesLookupPlugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        serviceFactory.CreateOrganizationService(context.UserId);
        var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        try
        {
            var results = new EntityCollection();
            var itemOne = new Entity
            {
                ["ID"] = Guid.NewGuid(),
                ["Name"] = "First Item"
            };

            var itemTwo = new Entity()
            {
                ["ID"] = Guid.NewGuid(),
                ["Name"] = "Second Item"
            };
            results.Entities.Add(itemOne);
            results.Entities.Add(itemTwo);

            context.OutputParameters["BusinessEntityCollection"] = results;
        }
        catch (Exception e)
        {
            tracingService.Trace($"{e.Message} {e.StackTrace}");
            if (e.InnerException != null)
                tracingService.Trace($"{e.InnerException.Message} {e.InnerException.StackTrace}");

            throw new InvalidPluginExecutionException(e.Message);
        }
     }
   }
}

I expect to see "First Item" and "Second Item" selectable in the lookup field.

1
Are you trying to add data into virtual Entity and your data has Lookup?AnkUser
That's correct. Eventually, the data will come from external source but for now, it's hard coded in the plug-in.Abiy
If I understand correctly, You cannot directly create Lookup when creating a Virtual Entity Record. I mean what does lookup means, It means a Record from some other table and then linking it. So I believe first this Lookup record should be available in System and Then you can show that as lookup.You probably already know but still here is a reference link how lookup is referenced blogs.technet.microsoft.com/lystavlen/2017/11/10/…AnkUser
basically, this is what I wanted to do: docs.microsoft.com/en-us/dynamics365/customer-engagement/… except, hard coded values for now instead of from DropBoxAbiy

1 Answers

0
votes

You’re hooking on RetrieveMultiple message in your plug-in. There is also Retrieve message (get single record by EntityReference) - you need to hook on that message as well for your entity.