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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Assign Entity from Code Activity- Entity Reference cannot have Id and Key Attributes empty

(0) ShareShare
ReportReport
Posted on by 311

Hi All,

I'm in CRM online, 9.0. When I try to assign entity(opportunity) to some user, I got the exception bellow. I got the exception regardless if I use Update or AssignEntity Message (which is now deprecated). I'm quite sure there is no empty entity reference because I logged Id and logicalName of the EntityReference.  Also if I run the code from my desktop application there is no error. There is no error if I assign entity manually to the same user through UI. The workflow is run under System Administrator authority. The user to who I assign is System Administrator.

This is the code:

     QueryExpression query = new QueryExpression();
            query.EntityName = "opportunity";
            query.ColumnSet = new ColumnSet(new string[] {
                "ownerid" //, "owningteam",  "owninguser" I tried also fetching these two attributes and adding them to opportunity before the update
            }
            );
             FilterExpression criteria = new FilterExpression(LogicalOperator.And);
         //....some criteria modification
            query.Criteria = criteria;
            query.Orders.Add(new OrderExpression("createdon", OrderType.Descending));
            query.TopCount = 1;

            var ec = OrgService.RetrieveMultiple(query);
            if (ec?.Entities?.Count != 1) return false;
            try
            {
                Entity lastOpportunity = ec.Entities[0];

// I retrieve all fields of the opportunity because so it is made in Microsoft sample for assigning entity. But it doesn't work also if I create entity and add only OwnerId to the Entity(with or without owningteam and owninguser.
                Entity e1 =  OrgService.Retrieve("opportunity", opportunityId, new ColumnSet(true));
                e1.Attributes["ownerid"] = new EntityReference(((EntityReference)(e1.Attributes["ownerid"])).LogicalName, ((EntityReference)(e1.Attributes["ownerid"])).Id);
                // tried also this:
                 // e1.Attributes["ownerid"] = lastOpportunity.Attributes["ownerid"];
                 // and this
                // e1.Attributes["ownerid"] = new EntityReference( ((EntityReference)(e1.Attributes["ownerid"])).LogicalName,  ((EntityReference)(e1.Attributes["ownerid"])).Id);
                OrgService.Update(e1);
                 return true;
            }
            catch(Exception ex)
            {
                Logger.Trace(ex);
            }

Exception thrown by the Update is this:

 Entity Reference cannot have Id and Key Attributes empty. at
Server stack trace:
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
   at Microsoft.Crm.Sandbox.SandboxOrganizationService.Execute(String operation, Byte[] serializedRequest, Object sandboxTraceSettingsObj)
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

Exception rethrown at [1]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Microsoft.Crm.Sandbox.ISandboxOrganizationService.Execute(String operation, Byte[] serializedRequest, Object traceSettings)
   at Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper.ExecuteInternal(OrganizationRequest request)
   at myFunction(myFunctionParameters

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Nikica Profile Picture
    311 on at
    RE: Assign Entity from Code Activity- Entity Reference cannot have Id and Key Attributes empty

    It looks like it is another plugin/workflow that fails.  Probabyl I didn't log innerException.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
UllrSki Profile Picture

UllrSki 2

#3
SC-08081331-0 Profile Picture

SC-08081331-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans