plugins and prameters RRS feed

  • Question

  • I just don't seem to be able to get this code to work. im trying to pass the value of account.name to a variable. the plugin is registered for create on account

          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)
                    Dim strLicence As String = entity.Attributes("name").ToString
                    Dim SQL_Connection As New SqlClient.SqlConnection
                    Dim SQL_Command As New SqlClient.SqlCommand
                    SQL_Connection = New SqlClient.SqlConnection("Data Source=xxxx;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;password=xxxxx")
                        SQL_Command.CommandType = CommandType.Text
                        SQL_Command.CommandText = "INSERT INTO ClientAdmin (Licence) Values ('" & strLicence & "')"
                        SQL_Command.Connection = SQL_Connection
                    Catch ex As Exception
                        Throw New InvalidPluginExecutionException("Error inserting new customer - " & ex.Message.ToString)
                    End Try
                End If
            End Sub

    from what I have read, I understood that when I click save on a new account, it would fire the pluggin, but I have also read that a couple of the erros I have been getting was due to 'name' being empty ?

    Dont ask me .. i dont know

    • Changed type Pete Newman Sunday, November 3, 2013 5:46 PM
    Saturday, November 2, 2013 10:35 PM


  • Just put a condition like below :

    If entity.Attributes.Contains("name") Then

    // your logic go here

     End If

    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    • Marked as answer by Pete Newman Sunday, November 3, 2013 5:46 PM
    Sunday, November 3, 2013 4:40 AM