locked
Systemuser create plugin, record does not exist when running in sync mode. RRS feed

  • Question

  • Hey everyone.  I'm working on a plugin in a crm2011 onprem environment.  Fairly simple stuff.

    • Lookup the ID for a team named "Non Affiliated"
    • Then create an association between that team and the systemuser that was just created 

    The code to look up the team is working fine, and when I debug I'm even seeing the id for the systemuser that was just created.  However when running in sync mode I am being told that the systemuser does not exist at executiong of the AssociateRequest.

    It works in async mode, but I'd love to get the speed of sync mode rather than the wait time of async.

    Here's the code, thoughts and advice are appreciated

    if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                {
                    try
                    {
                        
                        QueryByAttribute teamQuery = new QueryByAttribute("team");
                        teamQuery.ColumnSet = new ColumnSet(new string[] { "name", "teamid" });
                        teamQuery.AddAttributeValue("name", "Non Affiliated");
                        EntityCollection teamResult = service.RetrieveMultiple(teamQuery);
    
                        Entity newUser = (Entity)context.InputParameters["Target"];
                        EntityReference team = new EntityReference("team", (Guid)teamResult[0].Attributes["teamid"]);
    
                        AssociateRequest usertoteam = new AssociateRequest 
                        {
                            Target =  new EntityReference(newUser.LogicalName, newUser.Id),
                            RelatedEntities = new EntityReferenceCollection
                            {
                                team
                            },
                            Relationship = new Relationship("teammembership_association")
                        };
                    
                        
                        service.Execute(usertoteam);
    
    
        
                    }
                    catch (Exception ex)
                    {
                        tracingService.Trace("Non Affiliated Plugin: {0}", ex.ToString());
                        throw;
                    }
                }

    Tuesday, May 7, 2013 7:16 PM

Answers

  • I try to make simple:
    it works when is registered as asynchronous because will be executed outside the database transaction.
    When the plugin is registered as synchronous is executed you are still inside the database transaction that triggered the plugin.

    your plugin is very similar to the basic plug-in example on MSDN:

    http://msdn.microsoft.com/en-us/library/gg594416.aspx

    Inside the requirements you can read this part:

    Register this plug-in for an account entity, on the Create message, and in asynchronous mode. Alternately, you can register the plug-in on a post-event in the sandbox.

    I suggest to unregister totally the plugin and register again (also change the plugin name if possible, so you can avoid strange caching) inside sandbox & post-event


    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Wednesday, May 8, 2013 7:02 AM
    • Marked as answer by Hoyty Monday, January 6, 2014 8:41 PM
    Tuesday, May 7, 2013 8:06 PM

All replies

  • Hi,
    try to register the plugin in the sandbox, post-operation, synchronous.


    My blog: www.crmanswers.net


    Tuesday, May 7, 2013 7:29 PM
  • That is how I did it.

    I tried

    Sandbox, post, sync
    Sandbox, pre, sync
    not sandbox, post, sync
    not sandbox, pre, sync

    Then I go to sandbox, post, async and it worked, just slowly.  Should satisfy the needs of the project but I am curious as to why it doesn't work.

    Tuesday, May 7, 2013 7:32 PM
  • I try to make simple:
    it works when is registered as asynchronous because will be executed outside the database transaction.
    When the plugin is registered as synchronous is executed you are still inside the database transaction that triggered the plugin.

    your plugin is very similar to the basic plug-in example on MSDN:

    http://msdn.microsoft.com/en-us/library/gg594416.aspx

    Inside the requirements you can read this part:

    Register this plug-in for an account entity, on the Create message, and in asynchronous mode. Alternately, you can register the plug-in on a post-event in the sandbox.

    I suggest to unregister totally the plugin and register again (also change the plugin name if possible, so you can avoid strange caching) inside sandbox & post-event


    My blog: www.crmanswers.net

    • Proposed as answer by Guido PreiteMVP Wednesday, May 8, 2013 7:02 AM
    • Marked as answer by Hoyty Monday, January 6, 2014 8:41 PM
    Tuesday, May 7, 2013 8:06 PM