locked
Plug-In Question RRS feed

  • Question

  • Hi All,

    I have an entity called "application".  It has two fields "x_ActiveCandidate" and "x_PotentialCandidate".  On an update of the applicaiton entity, I want to execute a validation logic on the potential candidate field.  If the validation passes, the potential candidate becomes the active candidate and I will do more processing on the active candidate.

    I have a pre-update plugin on the application entity that is doing the validationon on a pre-image.  I also have a post-update plugin that does further processing on the application.

    if(preImage != null && preImage.Attributes.Contains("x_potentialcandidate")

    {

    //Run validation logic

    }

    Now, if validation passes, can I do the following assignment inisde of this pre-update plugin?  If so, how can I access this new value in the post-update plugin?

    preImage.Attributes["x_activecandidate"] = value of "x_potentialcandidate"

    Thursday, May 17, 2012 8:21 PM

Answers

  • HI fifo123,

                    You cannot update the values of the images, the values in the images are read-only, however you can update the values on the target entity you can obtain from the context, so you could do something as follows in the pre-update plugin.

    Entity myentity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
    
    myentity["x_activecandidate"] = value of "x_potentialcandidate"; 
    


    Regards,
    Damian Sinay

    • Marked as answer by fifo123 Friday, May 18, 2012 5:10 AM
    Thursday, May 17, 2012 8:55 PM

All replies

  • Hi fifo123,

    To pass in a value from one plugin to another, you will need to use SharedVariables. The value that you entered into the SharedVariables will be persisted only for the duration of the pipeline.

    In your pre-plugin, you will have something like this.

    public class PreEventPlugin : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
    
                Guid potentialCandidate = new Guid("{74882D5C-381A-4863-A5B9-B8604615C2D0}");
    
                // Pass the data to the post event plug-in in an execution context shared
                // variable named PrimaryContact.
                context.SharedVariables.Add("PotentialCandidate", (Object)potentialCandidate.ToString());
            }
        }

    And to get this value out from the PostPlugin, you will do something like this.

    public class PostEventPlugin : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                // Obtain the execution context from the service provider.
                Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
    
                // Obtain the contact from the execution context shared variables.
                if (context.SharedVariables.Contains("PotentialCandidate"))
                {
                    Guid potentialCandidate =
                        new Guid((string)context.SharedVariables["PotentialCandidate"]);
    
                    // Do something with the Potential Candidate.
                }
            }
        }

    For further information, please have a look at this link. http://msdn.microsoft.com/en-us/library/gg328579.aspx

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits

    Thursday, May 17, 2012 8:52 PM
  • HI fifo123,

                    You cannot update the values of the images, the values in the images are read-only, however you can update the values on the target entity you can obtain from the context, so you could do something as follows in the pre-update plugin.

    Entity myentity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
    
    myentity["x_activecandidate"] = value of "x_potentialcandidate"; 
    


    Regards,
    Damian Sinay

    • Marked as answer by fifo123 Friday, May 18, 2012 5:10 AM
    Thursday, May 17, 2012 8:55 PM
  • fifo,

    If you assign the Potential Candidate value to the Active Candidate attribute of the Target entity, it will get persisted by CRM automatically during the Update event.

    If this is not the intended behavior, you will need to use the SharedVariables as I mentioned above.

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits



    Friday, May 18, 2012 5:20 AM