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)

How do you retrieve an entity's lookup field value using a C# Plugin?

(0) ShareShare
ReportReport
Posted on by 360

I have written the plugin, registered it and it can update fields with values fed directly into the C# code. But I want to retrieve the id of a product value in a lookup field on the entity. 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at
    RE: How do you retrieve an entity's lookup field value using a C# Plugin?

    this might help :

    santoshmscrm.wordpress.com/.../how-to-retrieve-lookup-name-and-id-through-plugin

  • Saif Ehsan Profile Picture
    360 on at
    RE: How do you retrieve an entity's lookup field value using a C# Plugin?

    And here is the error the plugin throws:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=9.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXX]: An error occurred in the FollowupPlugin plug-in.Detail:
    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts">
    <ActivityId>b003e025-a220-408a-8fbf-ff23073525c3</ActivityId>
    <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:string">0</d2p1:value>
    </KeyValuePairOfstringanyType>
    <KeyValuePairOfstringanyType>
    <d2p1:key>SubErrorCode</d2p1:key>
    <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema" i:type="d4p1:string">-2146233088</d2p1:value>
    </KeyValuePairOfstringanyType>
    </ErrorDetails>
    <Message>An error occurred in the FollowupPlugin plug-in.</Message>
    <Timestamp>2018-02-22T08:27:39.293314Z</Timestamp>
    <ExceptionRetriable>false</ExceptionRetriable>
    <ExceptionSource i:nil="true" />
    <InnerFault i:nil="true" />
    <OriginalException i:nil="true" />
    <TraceText>

    [SamplePlugins: Microsoft.Crm.Sdk.Samples.FollowupPlugin]
    [5afe1466-9817-e811-a84c-000d3ad1181d: Microsoft.Crm.Sdk.Samples.FollowupPlugin: Create of salesorderdetail]
    Entered Microsoft.Dynamics.Sales.Plugins.PreOperationSalesOrderLineCreate.Execute(), Correlation Id: XXXXXXXXXXXXXXXXXXXXX, Initiating User: XXXXXXXXXXXXXXXXXXXX
    Exiting Microsoft.Dynamics.Sales.Plugins.PreOperationSalesOrderLineCreate.Execute(), Correlation Id: XXXXXXXXXXXXX, Initiating User: XXXXXXXXXXXXXXXX
    Starting sync workflow 'Tax field', Id: XXXXXXXXXXXXXXXXXXX
    Entering ConditionStep1_step:
    Entering SetAttributeValueStep3_step:
    Entering ConditionBranchStep3_step:
    Sync workflow 'Tax field' completed successfully

    FollowupPlugin: Error.


    </TraceText>
    </OrganizationServiceFault>

    I hid some of the id's above for security if relevant and added XXXX instead lol

  • Saif Ehsan Profile Picture
    360 on at
    RE: How do you retrieve an entity's lookup field value using a C# Plugin?

    MY CODE IS AS BELOW

    using System;

    using System.ServiceModel;

    using Microsoft.Xrm.Sdk;

    namespace Microsoft.Crm.Sdk.Samples

    {

       public class FollowupPlugin : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

               ITracingService tracingService =

                   (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               IPluginExecutionContext context = (IPluginExecutionContext)

                   serviceProvider.GetService(typeof(IPluginExecutionContext));

               if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

               {

                   Entity entity = (Entity)context.InputParameters["Target"];

                   if (entity.LogicalName != "salesorderdetail")

                       return;

                   try

                   {

                       Entity OrderLine = entity;

                       string strSalesOrderDetail_Productid = string.Empty;

                       if (OrderLine.Attributes.Contains("productid"))

                       {

                           strSalesOrderDetail_Productid = OrderLine.GetAttributeValue<string>("productid").ToString();

                       }

                       

                       OrderLine["shipto_name"] = strSalesOrderDetail_Productid;

                     

                      

                       IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                       IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                       tracingService.Trace("FollowupPlugin: Upadting the SalesOrder for you :-)");

                       

                       service.Update(OrderLine);

                   }

                   catch (FaultException<OrganizationServiceFault> ex)

                   {

                       throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);

                   }

                   catch (Exception ex)

                   {

                       tracingService.Trace("FollowupPlugin: {0}", ex.ToString());

                       throw;

                   }

               }

           }

       }

    }

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans