locked
CRM2011 Lookup Value RRS feed

  • Question

  • Working on a plugin using VB.Net how would I get the actual string that is in a seleted lookup?
    Dim context As Microsoft.Xrm.Sdk.IPluginExecutionContext =
                CType(serviceProvider.GetService(
                        GetType(Microsoft.Xrm.Sdk.IPluginExecutionContext)), 
                    Microsoft.Xrm.Sdk.IPluginExecutionContext)
            Dim sFullAddress As String = ""
            ' The InputParameters collection contains all the data passed in the message request.
            If context.InputParameters.Contains("Target") AndAlso
                TypeOf context.InputParameters("Target") Is Entity Then
    
                ' Obtain the target entity from the input parmameters.
                Dim entity As Entity = CType(context.InputParameters("Target"), Entity)
                'Dim entity As Dynamic.ent
                '</snippetAccountNumberPlugin2>
    
                ' Verify that the target entity represents an account.
                ' If not, this plug-in was not registered correctly.
                If entity.LogicalName.Equals("lead") Then
                    Try
                        Dim sAddress As String = ""
                        Dim sCity As String = ""
                        Dim sState As String = ""
                        Dim sZip As String = ""
                        Dim sCountry As String = ""
                        If entity.Attributes.Contains("address2_line1") Then
                            sAddress = If(Not IsNothing(entity.Attributes("address2_line1")), entity.Attributes("address2_line1"), "")
                        End If
                        If entity.Attributes.Contains("address2_city") Then
                            sCity = If(Not IsNothing(entity.Attributes("address2_city")), entity.Attributes("address2_city"), "")
                        End If
                        If entity.Attributes.Contains("address2_stateorprovince") Then
                            sState = If(Not IsNothing(entity.Attributes("address2_stateorprovince")), entity.Attributes("address2_stateorprovince"), "")
                        End If
                        If entity.Attributes.Contains("address2_postalcode") Then
                            sZip = If(Not IsNothing(entity.Attributes("address2_postalcode")), entity.Attributes("address2_postalcode"), "")
                        End If
    
                        ''''''''''''''''''''''''''''''''''''
                        'Just need the string val of selected lookup
                        'sCountry = entity.Attributes("llb_leadcountry")  ??????
    
                        '''''''''''''''''''''''''''''''''''''
                        entity.Attributes.Add("address2_line2", sCountry)
                                       Catch ex As Exception
                        Throw New InvalidPluginExecutionException("Error: " & ex.Message)
                    End Try
                End If
            End If
    Thanks in Advance!

    WeDoMapping

    Saturday, February 18, 2012 12:54 AM

Answers

  • Hello,

    I develop plugins using C# but I believe you will be able to convert my C# code to your VB:

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(null);
    
                string countryname = string.Empty;
                if (entity.Contains("llb_leadcountry"))
                {
                    Guid countryid = ((EntityReference)entity["llb_leadcountry"]).Id;
    
                    Entity country = service.Retrieve("Country Entity Name Here", countryid, new ColumnSet(new string[] { "Coutry Name Field Here" });
    
                    countryname = (string)country["Country Name Field Here"];
                }


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    • Marked as answer by WeDoMapping Saturday, February 18, 2012 6:53 PM
    Saturday, February 18, 2012 2:29 AM
    Moderator

All replies

  • Hello,

    I develop plugins using C# but I believe you will be able to convert my C# code to your VB:

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(null);
    
                string countryname = string.Empty;
                if (entity.Contains("llb_leadcountry"))
                {
                    Guid countryid = ((EntityReference)entity["llb_leadcountry"]).Id;
    
                    Entity country = service.Retrieve("Country Entity Name Here", countryid, new ColumnSet(new string[] { "Coutry Name Field Here" });
    
                    countryname = (string)country["Country Name Field Here"];
                }


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    • Marked as answer by WeDoMapping Saturday, February 18, 2012 6:53 PM
    Saturday, February 18, 2012 2:29 AM
    Moderator
  • Perfect! Thank you so much, here is the code in VB.NET for someone else:

     Dim serviceFactory As IOrganizationServiceFactory = DirectCast(serviceProvider.GetService(GetType(IOrganizationServiceFactory)), IOrganizationServiceFactory)
                        Dim service As IOrganizationService = serviceFactory.CreateOrganizationService(Nothing)
                        Dim countryname As String = String.Empty
    
                        If entity.Attributes.Contains("llb_leadcountry") Then
                            Dim countryid As Guid = DirectCast(entity("llb_leadcountry"), EntityReference).Id
                            Dim country As Entity = service.Retrieve("llb_country", countryid, New ColumnSet("llb_countryname"))
                            countryname = DirectCast(country("llb_countryname"), String)
                        End If
    Seems like a lot of work to get a selected value from a dropdown.....

    WeDoMapping

    Saturday, February 18, 2012 6:55 PM