Answered by:
Still struggling trying to build plugin

Question
-
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
Thursday, December 5, 2013 9:28 PM
Answers
-
This code looks correct:
Dim req As New RetrieveRequest() req.ColumnSet = New ColumnSet(New String() {"name"}) req.Target = New EntityReference("account", DirectCast(entity.Attributes("new_licence"), EntityReference).Id)
But to handle the response, you'll need something like this:
Dim resp as RetrieveResponse resp = service.Execute(req) Dim name as String name = DirectCast(resp.Entity.Attributes("name"), String)
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
- Marked as answer by Pete Newman Thursday, December 12, 2013 3:32 PM
Friday, December 6, 2013 11:35 AMModerator
All replies
-
Hello,
Recheck following article - http://mileyja.blogspot.com/2012/03/how-to-retrieve-entity-instance-in.html
Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
My blogThursday, December 5, 2013 10:06 PMModerator -
Sorry, Im just not getting this. The plugin is firing on new_oAccount entity and this entity has a lookup field called new_licence. The lookup is to account entity and i am trying to get back the value of then 'name' field from account. in your example you have the GUID, where do you get that guid from.
I am struggling to understand how to apply ths and get the result back. if more explanation is needed , please let me know
Dont ask me .. i dont know
Friday, December 6, 2013 9:55 AM -
This code looks correct:
Dim req As New RetrieveRequest() req.ColumnSet = New ColumnSet(New String() {"name"}) req.Target = New EntityReference("account", DirectCast(entity.Attributes("new_licence"), EntityReference).Id)
But to handle the response, you'll need something like this:
Dim resp as RetrieveResponse resp = service.Execute(req) Dim name as String name = DirectCast(resp.Entity.Attributes("name"), String)
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
- Marked as answer by Pete Newman Thursday, December 12, 2013 3:32 PM
Friday, December 6, 2013 11:35 AMModerator