locked
pass field value to plugin that uses Entity Reference RRS feed

  • Question

  • Thanks this forum I have advanced my knowledge in crm by leaps and bounds. I with the help of Rockstar I have successfully managed to fire a plugin from a record being deactivated to throw an error ( code below ) . This made me think, can a value from the deactivated record be passed to the plugin. in this instance, I would like to pass the field value of new_accountkey which is a text field.

    Namespace OAccountsDelete
        Public Class cls_OAccounts_delete
            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("EntityMoniker") AndAlso TypeOf context.InputParameters("EntityMoniker") Is EntityReference Then
     
    
                    Throw New InvalidPluginExecutionException("You are not authorized to set this record as inactive ")
    " I want to add the value of new_accountkey to the error message"
    
                End If
    
            End Sub
        End Class
    End Namespace

     

    Dont ask me .. i dont know

    Saturday, January 4, 2014 9:23 PM

Answers

  • You need to do a Retrieve using the Id of the EntityReference, but before you need to instantiate the organizationservice after you get the context

    something like this:

    Dim serviceFactory As IOrganizationServiceFactory = DirectCast(serviceProvider.GetService(GetType(IOrganizationServiceFactory)), IOrganizationServiceFactory)
    Dim service As IOrganizationService = serviceFactory.CreateOrganizationService(context.UserId)

    and after you got the EntityMoniker do the retrieve:

    Dim entity As EntityReference = DirectCast(context.InputParameters("EntityMoniker"), EntityReference)
    Dim attributes As New ColumnSet(New String() { "new_accountkey" })
    Dim retrievedEntity as Entity = service.Retrieve(entity.LogicalName, entity.Id, attributes)
    Dim key as String = retrievedEntity("new_accountkey")
    

    I hope the syntax is ok because normally i use C#


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Proposed as answer by Guido PreiteMVP Saturday, January 4, 2014 9:38 PM
    • Marked as answer by Pete Newman Saturday, January 4, 2014 9:47 PM
    Saturday, January 4, 2014 9:38 PM

All replies

  • You need to do a Retrieve using the Id of the EntityReference, but before you need to instantiate the organizationservice after you get the context

    something like this:

    Dim serviceFactory As IOrganizationServiceFactory = DirectCast(serviceProvider.GetService(GetType(IOrganizationServiceFactory)), IOrganizationServiceFactory)
    Dim service As IOrganizationService = serviceFactory.CreateOrganizationService(context.UserId)

    and after you got the EntityMoniker do the retrieve:

    Dim entity As EntityReference = DirectCast(context.InputParameters("EntityMoniker"), EntityReference)
    Dim attributes As New ColumnSet(New String() { "new_accountkey" })
    Dim retrievedEntity as Entity = service.Retrieve(entity.LogicalName, entity.Id, attributes)
    Dim key as String = retrievedEntity("new_accountkey")
    

    I hope the syntax is ok because normally i use C#


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Proposed as answer by Guido PreiteMVP Saturday, January 4, 2014 9:38 PM
    • Marked as answer by Pete Newman Saturday, January 4, 2014 9:47 PM
    Saturday, January 4, 2014 9:38 PM
  • Thank you so much. The help you have given will be a great help in my project

    thank you


    Dont ask me .. i dont know

    Saturday, January 4, 2014 9:47 PM