Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Microsoft Dynamics CRM set field value of related entity

Posted on by Microsoft Employee

I am using Microsoft Dynamics CRM Online. 

I have a working plug-in starting on the Create Message (post-operate) of an entity called (let's simplify) "entity1". One of the features of this plug-in is that it determines a certain value. Let's call this one "importantValue". The plug-in also creates a relation between "entity1" and another entity (let's simplify again) "entity2", and populates the corresponding lookup field in "entity1".

All of this is working just fine. However, I also want the plug-in to set a field of "entity2" (called "samplefield") to the value of "importantValue". I know how to retrieve the GUID of the related record of entity2, but I can't get the plug-in to update this (already existing) record.

This is the part of the code making problems. I already retrieved the GUID "entity2Guid" and filled importantValue (it's a string). My IOrganizationService is called "service".

Entity entity2 = new Entity("new_entity2");
entity2.Id = new Guid (entity2Guid);
entity2["new_samplefield"] = importantValue;
service.Update(entity2);

What am I doing wrong? Thanks in advance!

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Microsoft Dynamics CRM set field value of related entity

    Please give string values in quotes:

    entity2["new_samplefield"] = "importantValue";

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Microsoft Dynamics CRM set field value of related entity

    @Alok Srivastav

    1. this is the exception message I receive:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred in the test plug-in.Detail: 
    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
    <ErrorCode>-2147220891</ErrorCode>
    <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic">
    <KeyValuePairOfstringanyType>
    <d2p1:key>OperationStatus</d2p1:key>
    <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:int">0</d2p1:value>
    </KeyValuePairOfstringanyType>
    <KeyValuePairOfstringanyType>
    <d2p1:key>SubErrorCode</d2p1:key>
    <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:int">-2146233088</d2p1:value>
    </KeyValuePairOfstringanyType>
    </ErrorDetails>
    <Message>An error occurred in the test plug-in.</Message>
    <Timestamp>2017-03-08T11:25:38.4333423Z</Timestamp>
    <ExceptionSource>SdkClient</ExceptionSource>
    <InnerFault>
    <ErrorCode>-2147220989</ErrorCode>
    <ErrorDetails xmlns:d3p1="schemas.datacontract.org/.../System.Collections.Generic" />
    <Message>Incorrect attribute value type System.String</Message>
    <Timestamp>2017-03-08T11:25:38.2429219Z</Timestamp>
    <ExceptionSource>SdkClient</ExceptionSource>
    <InnerFault>
    <ErrorCode>-2147220989</ErrorCode>
    <ErrorDetails xmlns:d4p1="schemas.datacontract.org/.../System.Collections.Generic" />
    <Message>Incorrect attribute value type System.String</Message>
    <Timestamp>2017-03-08T11:25:38.2429219Z</Timestamp>
    <ExceptionSource i:nil="true" />
    <InnerFault>
    <ErrorCode>-2147220970</ErrorCode>
    <ErrorDetails xmlns:d5p1="schemas.datacontract.org/.../System.Collections.Generic" />
    <Message>System.ArgumentException: Incorrect attribute value type System.String
    Parameter name: new_samplefield </Message>
    <Timestamp>2017-03-08T11:25:38.2429219Z</Timestamp>
    <ExceptionSource i:nil="true" />
    <InnerFault i:nil="true" />
    <OriginalException i:nil="true" />
    <TraceText i:nil="true" />
    </InnerFault>
    <OriginalException i:nil="true" />
    <TraceText i:nil="true" />
    </InnerFault>
    <OriginalException>System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Incorrect attribute value type System.String (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).
    at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, ExecutionContext executionContext)
    at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
    at Microsoft.Crm.Extensibility.InprocessServiceProxy.ExecuteCore(OrganizationRequest request)
    at Microsoft.Crm.Sandbox.SandboxSdkListener.ExecuteInternal(SandboxCallInfo callInfo, SandboxSdkContext requestContext, String operation, Byte[] serializedRequest, IExecutionContext context, String&amp; primaryEntityName)
    at Microsoft.Crm.Sandbox.SandboxSdkListener.Execute(SandboxCallInfo callInfo, SandboxSdkContext requestContext, String operation, Byte[] serializedRequest) 
    Original SdkErrors: 
    </OriginalException>
    <TraceText>
    [Microsoft.Crm.ObjectModel: Microsoft.Crm.ObjectModel.TargetAttributeValidationPlugin]
    [fc743e7d-fbea-4695-bdb9-7d78334c8474: TargetAttributeValidationPlugin]
    
    </TraceText>
    </InnerFault>
    <OriginalException>System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Incorrect attribute value type System.String (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).
    at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, ExecutionContext executionContext)
    at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
    at Microsoft.Crm.Extensibility.InprocessServiceProxy.ExecuteCore(OrganizationRequest request)
    at Microsoft.Crm.Sandbox.SandboxSdkListener.ExecuteInternal(SandboxCallInfo callInfo, SandboxSdkContext requestContext, String operation, Byte[] serializedRequest, IExecutionContext context, String&amp; primaryEntityName)
    at Microsoft.Crm.Sandbox.SandboxSdkListener.Execute(SandboxCallInfo callInfo, SandboxSdkContext requestContext, String operation, Byte[] serializedRequest) 
    Original SdkErrors: 
    </OriginalException>
    <TraceText>
    </TraceText>
    </OrganizationServiceFault>
    
    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.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
    
    Exception rethrown at [0]: 
    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.ISandboxHost.ExecuteAndReturnTraceInfo(SandboxCallInfo callInfo, SandboxPluginExecutionContext requestContext, Guid pluginAssemblyId, Int32 sourceHash, String assemblyName, Guid pluginTypeId, String pluginTypeName, String pluginConfiguration, String pluginSecureConfig, String assemblyContents, Boolean returnTraceInfo)
    at Microsoft.Crm.Sandbox.SandboxPlugin.Execute(SandboxClient client, SandboxCallTracker callTracker, IExecutionContext requestContext, String assemblyContents, Boolean returnTraceInfo)
    at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)

    2. How do I check the Depth of Plugin execution?

    3. Entity2Guid is a string variable which stores the Guid. (although not in Guid-format as it is a string, of course)

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Microsoft Dynamics CRM set field value of related entity

    Refer this:

    social.microsoft.com/.../setting-value-for-lookups-and-option-set-through-c-code-in-crm-2011

  • Suggested answer
    Dynamics_Alok Profile Picture
    Dynamics_Alok 1,746 on at
    RE: Microsoft Dynamics CRM set field value of related entity

    Can you share exception messages if you are getting any.

    I think issue may be  recursive call of the same plugin .

    Please make sure that you are checking Depth of Plugin execution and 

    entity2Guid is proper.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans