locked
CRM 2011: Post Create Plugin RRS feed

  • Question

  • I think I might be missing something very simple here.  I have a simple post-create plugin that fires in order to wire up some associations between entities.  When the entity is created, the plugin fires and runs through logic that creates the associations and updates a few fields on the main entity. 

    The first thing I do in the plugin code is capture the id of the entity and call out to a service class.  In that service class I try to retrieve the entity from CRM using the following code. However, I get an error that says the entity does not exists.  This is a "post" create plugin.  Doesn't that mean it exists?  It is after the create, so it should exists, right?

    var hbc = _crmSdkService.Retrieve("new_hbc", hbcId, new ColumnSet("new_name", "new_contact"));

    Here's the plugin boilerplate code I use to capture the entity id and where I call out to the service.  Why would the id that I capture here, not allow me to read the entity from the service class?  Is not created?  It must not be, as I don't see it in the database yet. So I'm guessing that the entity is not actually created until the post-create plugin succeeds.  Is that correct?

    Any suggestions as how I can fire off something like this and update the entity in the post-create plugin?  Firing this logic off from the update works fine, as it already exists.  But I need to fire it off from the create.

     protected void ExecutePostHBCCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                // Obtain the execution context from the service provider.
                var orgService = localContext.OrganizationService;
                var context = localContext.PluginExecutionContext;
                _service = new CrmServices(orgService); // Initialize the service class.
    
                // The InputParameters collection contains all the data passed in the message request.
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parmameters.
                    var entity = (Entity)context.InputParameters["Target"];
    
                    if (entity.LogicalName.Equals("new_hbc"))
                    {
                        var hbcId = new Guid(entity.Attributes["new_hbcid"].ToString());
    
                        try
                        {
                            //orgService.Create(entity);                        
                            _service.UpdateEmpowerContactAgency(hbcId);
                        }
                        catch (Exception ex)
                        {
                            throw new InvalidPluginExecutionException("An error occurred in the plug-in." + ex.Message, ex);
                        }
                    }
                    else
                    {
                        throw new InvalidPluginExecutionException(String.Format("Failed to process logical name type."));
                    }
                }
                else
                {
                    throw new InvalidPluginExecutionException(String.Format("Failed to process target value."));
                }
            }



    Jon Gregory Rothlander

    Friday, March 6, 2015 8:18 PM

All replies

  • please try to register the plugin asynchronous mode and see if it fixes the issue?.

    Jithesh

    Sunday, March 8, 2015 12:41 AM
  • I was able to make a few changes and get it to work using asynchronous mode.  However, now the user has to know to wait a few seconds and then refresh the page.  So it creates a bit of an odd user experience. I was looking for a way to have the user click a button then the plugin logic would fire and load the sub-grids.  I was thinking about adding a JavaScript progress bar or something in case it takes a little while to process.

    Any suggestions in this regards?


    Jon Gregory Rothlander


    Monday, March 9, 2015 2:32 PM
  • hi Jon,

    the step that many be causing issue is the retrieve.

    you are trying to retrieve a record within transaction context of the record.

    have you tried to just add the attributes and issue an update?.

    regards

    Jithesh

    Monday, March 9, 2015 10:31 PM