Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 82002

Exception: Entity Reference cannot have id and key attributes empty

$
0
0

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);                      
                    }                  
                }


Viewing all articles
Browse latest Browse all 82002

Latest Images

Trending Articles



Latest Images