locked
can a plugin fired 'on create' update the record that activated the plugin RRS feed

  • Question

  • I am looking for some guidance regarding a plugin. The plugin is to update a legacy SQL application, which seems to be straight forward enough. The plugin is fired on new_OAccount create.

    I need to return a value back from the plugin into the same record.

    I.e

    New_Oaccount has a field new_LegacyGiud which is a text field. When a new record is added, the plugin is fired. The plugin executes a SQL stored procedure and returns back a string. I need to be able to update new_LegacyGuid with that string value

    1. Can this be done from the same plugin?
    2. If not, what would be the best way to approach this.

    I have a snippit of my plugin below. The only problem I have is how to update the record that has fired the 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.Query

    Imports Microsoft.Xrm.Sdk.Messages

    Namespace OAccountsAdd

        Public Class cls_OAccounts_add

            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)

                    Dim strLicence As String = String.Empty 'new_licence()

                    ' Get Old SQL ID

                    Dim SQL_Connection As New SqlClient.SqlConnection

                    Dim SQL_Command As New SqlClient.SqlCommand

                    Dim SQL_Return_LegacyGUID 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_LegacyGUID = SQL_Command.ExecuteScalar

                    Catch ex As Exception

                        Throw New InvalidPluginExecutionException("Error Generating GUID - " & ex.Message.ToString)

                    Finally

                        SQL_Connection.Close()

                        SQL_Connection.Dispose()

                    End Try

           THIS IS WHERE I need to return the value from   SQL_Return_LegacyGUID back to the new_LegacyGUID field in CRM     

     

    End Sub

        End Class

    End Namespace


    Dont ask me .. i dont know

    Thursday, December 12, 2013 1:22 PM

Answers

  • Hello,

    You can only use return value from your stored procedure to set in entity attribute if have registered your plugin on pre create so that before entity creation you can add that new field in entity propertybag. but if you have written post create, you need to call update method to update entity record something like below

                     Entity _entity = new Entity("yourentity");
                    _entity.Id = new Guid(context.OutputParameters.Properties["id"].ToString());
                    _entity["textfieldnamethat_youwanttoupdate"] = valuereturnedfromstoredprocedure;
                    _service.Update(_entity);

    HTH


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Proposed as answer by HIMBAPModerator Thursday, December 12, 2013 3:47 PM
    • Marked as answer by Pete Newman Thursday, December 12, 2013 3:50 PM
    Thursday, December 12, 2013 3:47 PM
    Moderator