web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Exception: Entity Reference cannot have id and key attributes empty

(0) ShareShare
ReportReport
Posted on by

Hi all,

I'm enough new on Dynamics CRM and I'm working on CRM 2016

I'm trying to do a custom workflow but I have a problem with the entity reference output, I execute the following code and I receive this exception: "Entity Reference cannot have id and key attributes empty".

using System;
using System.Activities;
using System.Collections.ObjectModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System.Linq;
using TCRMPlugins.ProxyClasses;

namespace TCRMPlugins
{
    public partial class CustomerSearch: BaseWorkflow
    {
        [Input("Email")]
        public InArgument<String> Email { get; set; }


        [Output("Individual")]
        [ReferenceTarget("contact")]
        public OutArgument<EntityReference> Individual { get; set; }


        protected override void ExecuteInternal(LocalWorkflowContext context)
        {
            var email = this.Email.Get(context.CodeActivityContext);


            if (email != null)
            {
                var individualQuery = context.CrmContext.CreateQuery("contact")

                .ToList()

                .Where(c => c.GetAttributeValue<string>("emailaddress1") == email)

                .FirstOrDefault();
           
                if (individualQuery != null)
                {
                    var individualFound = new ProxyClasses.Individual(
            context.OrganizationService.Retrieve(
                 ProxyClasses.Individual.GetLogicalName(),
                 individualQuery.Id,
                 new ColumnSet(true)
                 )
             );
                    if(individualFound != null)
                    {
                        var individualReference = new EntityReference(ProxyClasses.Individual.GetLogicalName(), individualQuery.Id);
                        this.Individual.Set(context.CodeActivityContext, individualReference);                      
                    }                  
                }

*This post is locked for comments

I have the same question (0)
  • ashlega Profile Picture
    34,477 on at

    Hi,

     not sure what's causing the error, but you should probably try to simplify the code first:

           protected override void ExecuteInternal(LocalWorkflowContext context)

           {

               var email = this.Email.Get(context.CodeActivityContext);

               if (email != null)

               {

                   var individual =

                            (from c in context.CrmContext.CreateQuery("contact")

                             where (string)c["emailaddress1"] == email

                             select c).FirstOrDefault();

                   if (individual != null)

                   {

                       this.Individual.Set(context.CodeActivityContext, individual.ToEntityReference());                      

                    }

              }

       }

    This way you won't be bringing all contacts to the "client" to filter them after that - you'll only get those which have correct email (and you don't need that extra "retrieve" call)

  • Community Member Profile Picture
    on at

    Hi Alex,

    I tried your code but I still get the same error.

    However thank you because in this way my code has got to be pretty faster than before

    Giacomo Scappaticci

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    from the individual you need to extract the ID and the entitytypekey then you can create an entityreference with those 2 values

  • ashlega Profile Picture
    34,477 on at

    Hi Giacomo,

     Do you know if there is a contact for the email you are testing this code with?

    I'm wondering if it would help if you explicitly set that output parameter to null when nothing is found..

  • Community Member Profile Picture
    on at

    Hi Guillaume,

    how can I extract the entitytypekey?

  • Community Member Profile Picture
    on at

    Yes Alex, I checked and the contact exists.

    Now I try to put entity reference null

  • ashlega Profile Picture
    34,477 on at

    Hi Guillaume,

     toEntityReference is a standard method that creates a perfectly valid entity reference from an entity record:

    msdn.microsoft.com/.../microsoft.xrm.sdk.entity.toentityreference.aspx

  • Community Member Profile Picture
    on at

    Hi Alex,

    I tried to set the output null but I get the same error

  • ashlega Profile Picture
    34,477 on at

    Ok.. Could you try something (if it does not work either then I am out of ideas:) )

    Rewrite your code like this and see if it changes anything (just make a copy of the original code first):

    using System;

    using System.Activities;

    using System.Collections.ObjectModel;

    using Microsoft.Crm.Sdk.Messages;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Crm.Sdk;

    using Microsoft.Xrm.Sdk.Messages;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Xrm.Sdk.Workflow;

    using System.Linq;

    using TCRMPlugins.ProxyClasses;

    namespace TCRMPlugins

    {

       public partial class CustomerSearch: CodeActivity

       {

          [Input("Email")]

           public InArgument<String> Email { get; set; }

           [Output("Individual")]

           [ReferenceTarget("contact")]

           public OutArgument<EntityReference> Individual { get; set; }

           protected override void Execute(CodeActivityContext executionContext)

           {

               IWorkflowContext workflowContext = executionContext.GetExtension<IWorkflowContext>();

               IOrganizationServiceFactory serviceFactory =

                   executionContext.GetExtension<IOrganizationServiceFactory>();

               IOrganizationService service =

                   serviceFactory.CreateOrganizationService(workflowContext.UserId);

               var context = new OrganizationServiceContext(service);

               var email = this.Email.Get(executionContext);

                this.Individual.Set(executionContext, null);  

                if (email != null)

                {

                  var individual =

                           (from c in context.CrmContext.CreateQuery("contact")

                            where (string)c["emailaddress1"] == email

                            select c).FirstOrDefault();

                  if (individual != null)

                  {

                      this.Individual.Set(executionContext, individual.ToEntityReference());                      

                  }

                }

           }

      }

    }

  • RyanCPerry Profile Picture
    105 on at

    If this is registered as a custom action called in a workflow, it is likely that you have specified an output entityreference arguement as required, but your workflow is returning null. Check to see if any of your action output variables are required. If so, set them to optional, save, publish, update plugins and try it then. This fixed the error for me. The problem wasn't the code, it was the action expecting something to be returned.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Varsha deshpande Profile Picture

Varsha deshpande 5

#2
NeerajPawar Profile Picture

NeerajPawar 2

#2
jlattimer Profile Picture

jlattimer 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans