locked
CRM 2013, C# PlugIn - How To Get Value of A Lookup Field RRS feed

  • Question

  • Hi,

    The title basically says it all

    I have a lookup field called "businessowningtype" and I want to get the value that is displayed in the field

    Thanks, Shaun


    S.Harrison

    Monday, June 9, 2014 8:11 AM

Answers

  • Hi

    Thanks for your reply

    I'm quite new to all of this so please bare with me

    My code at the moment stands as follows;

    owningUnit = ((Microsoft.Xrm.Sdk.EntityReference)(owningUnitImage.Attributes["owningbusinessunit"])).ToString();

    I used a InvalidPluginExecutionExeption message to show what is getting returned, but all I'm getting is Microsoft.Xrm.Sdk.EntityReference

    Thanks, Shaun



    S.Harrison

    Hi,
    if your field is a lookup from C# you will get always an EntityReference, after you need to use this entityreference to retrieve the actual entity, something like:

    var owningUnit = (Microsoft.Xrm.Sdk.EntityReference)(owningUnitImage.Attributes["owningbusinessunit"];
    var actualOwningUnit = service.Retrieve(owningUnit.LogicalName, owningUnit.Id, new ColumnSet(true));
    var nameField = actualOwningUnit["name"].ToString();
    where service is your IOrganizationService variable.


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Monday, June 9, 2014 8:41 AM
  • You need to use currencyname

    var currencyName = actualCurrency["currencyname"].ToString();


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, June 10, 2014 2:22 PM

All replies

  • You can have the GUID associated with that field, when you retrive data in plugin.

    Still having issue please give more details on issue you are facing with some trace/code anything.


    Jiten Patoliya Windows C# developer

    Monday, June 9, 2014 8:15 AM
  • Hi

    Thanks for your reply

    I'm quite new to all of this so please bare with me

    My code at the moment stands as follows;

    owningUnit = ((Microsoft.Xrm.Sdk.EntityReference)(owningUnitImage.Attributes["owningbusinessunit"])).ToString();

    I used a InvalidPluginExecutionExeption message to show what is getting returned, but all I'm getting is Microsoft.Xrm.Sdk.EntityReference

    Thanks, Shaun



    S.Harrison

    Monday, June 9, 2014 8:30 AM
  • Hi

    Thanks for your reply

    I'm quite new to all of this so please bare with me

    My code at the moment stands as follows;

    owningUnit = ((Microsoft.Xrm.Sdk.EntityReference)(owningUnitImage.Attributes["owningbusinessunit"])).ToString();

    I used a InvalidPluginExecutionExeption message to show what is getting returned, but all I'm getting is Microsoft.Xrm.Sdk.EntityReference

    Thanks, Shaun



    S.Harrison

    Hi,
    if your field is a lookup from C# you will get always an EntityReference, after you need to use this entityreference to retrieve the actual entity, something like:

    var owningUnit = (Microsoft.Xrm.Sdk.EntityReference)(owningUnitImage.Attributes["owningbusinessunit"];
    var actualOwningUnit = service.Retrieve(owningUnit.LogicalName, owningUnit.Id, new ColumnSet(true));
    var nameField = actualOwningUnit["name"].ToString();
    where service is your IOrganizationService variable.


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Monday, June 9, 2014 8:41 AM
  • Worked great, thanks for the help and for the quick reply

    Shaun


    S.Harrison

    Monday, June 9, 2014 8:48 AM
  • Hi, I've tried using the same code with changes to try get the value of my lookup field with currency

    The Code is as follows

    //Check the owning unit for savin

                              

    varcurrency = ((Microsoft.Xrm.Sdk.EntityReference)(currencyImage.Attributes["transactioncurrencyid"]));

                               

    varactualCurrency = service.Retrieve(currency.LogicalName, currency.Id, newColumnSet(true));

                               

    varcurrencyName = actualCurrency["name"].ToString();

    But I keep getting the error of

    The given key was not present in the dictionary?


    S.Harrison

    Tuesday, June 10, 2014 1:57 PM
  • You need to use currencyname

    var currencyName = actualCurrency["currencyname"].ToString();


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, June 10, 2014 2:22 PM
  • Shorter version: ((EntityReference)currentRecord["businessowningtype"]).Name;
    • Edited by Kyle Knab Thursday, September 11, 2014 8:50 PM shorter version
    Thursday, September 11, 2014 7:43 PM
  • Shorter version: ((EntityReference)currentRecord["businessowningtype"]).Name;

    In this way you can get null value, especially inside a plugin. From MSDN:

    http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.entityreference.name.aspx

    EntityReference.Name Property

    This property can contain a value or null. This property is not automatically populated unless the EntityReference object has been retrieved from the server. This value is used to provide text to represent the related record in any user interface elements, such as the value in a column of a view.


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Friday, September 12, 2014 3:10 AM
  • Working great thanks!
    Friday, November 14, 2014 4:21 PM