CRM2013
I am trying to write a plugin in that is fired on 'create' on a custom entity. The only problem I am having is with the lookup. The custom entity has a lookup 'new_licence' to accounts, and I need to get the value from the 'name' field in accounts.
I have tried so many ways and just cannot get the value of account.name into this plugin.
Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports Microsoft.Xrm.Sdk Imports System.ServiceModel Imports Microsoft.SqlServer Imports System.IO Imports CRM2013_Plugins.clsGlobals Imports Microsoft.Xrm.Sdk.Client Imports Microsoft.Xrm.Sdk.Messages Imports Microsoft.Xrm.Sdk.Query Namespace clsTest Public Class clsTest Implements IPlugin Public Sub Execute(serviceProvider As System.IServiceProvider) Implements Microsoft.Xrm.Sdk.IPlugin.Execute Dim context As IPluginExecutionContext = DirectCast(serviceProvider.GetService(GetType(IPluginExecutionContext)), IPluginExecutionContext) If context.InputParameters.Contains("Target") AndAlso TypeOf context.InputParameters("Target") Is Entity Then Dim entity As Entity = DirectCast(context.InputParameters("Target"), Entity) ' Get Old SQL ID Dim SQL_Connection As New SqlClient.SqlConnection Dim SQL_Command As New SqlClient.SqlCommand Dim SQL_Return As String = String.Empty SQL_Connection = New SqlClient.SqlConnection(SQLConnectionString) Try SQL_Command.CommandType = CommandType.Text SQL_Command.CommandText = "SELECT CAST(NEWID() as VARCHAR(50))" SQL_Command.Connection = SQL_Connection SQL_Connection.Open() SQL_Return = SQL_Command.ExecuteScalar Catch ex As Exception Throw New InvalidPluginExecutionException("Error generating SQL GUID - " & ex.Message.ToString) Finally SQL_Connection.Close() SQL_Connection.Dispose() End Try ' test Dim serviceFactory As IOrganizationServiceFactory = DirectCast(serviceProvider.GetService(GetType(IOrganizationServiceFactory)), IOrganizationServiceFactory) Dim service As IOrganizationService = Nothing service = serviceFactory.CreateOrganizationService(context.UserId) Dim req As New RetrieveRequest() req.ColumnSet = New ColumnSet(New String() {"name"}) req.Target = New EntityReference("account", DirectCast(entity.Attributes("new_licence"), EntityReference).Id) service.Execute(req) Dim TestRtn As String = req.ColumnSet.Columns(0).ToString Throw New InvalidPluginExecutionException("TestRtn = " & TestRtn) ' end test End If End Sub End Class End Namespace
Dont ask me .. i dont know