title: CRM 2011 Custom Workflow
body: <blockquote>
    <p><strong>I GO THE PLUGIN WORKING, AND I HAVE UPDATED THE WORKING CODE HERE. HOPE IT HELPS!!</strong></p>
  </blockquote>
  
  <p>I'm creating a workflow which has a plugin to retrieve a contact entity from a "FROM" field of an Email record. I'm trying to see if that email exists in the contact entity. The input is the "FROM" email and the output will return the Contacts entity. I have not been able to get this code working, I've received different errors or no errors, but I know its not working. Please help! Thanks in advance!</p>
  
  <pre class="lang-csharp prettyprint-override"><code>using System.Activities;
  using Microsoft.Xrm.Sdk;
  using Microsoft.Xrm.Sdk.Workflow;
  using Microsoft.Xrm.Sdk.Query;
  using Microsoft.Xrm.Sdk.Messages;
  using System;
  
  namespace GetSenderPlugin
  {
    public class GetSenderPlugin : CodeActivity
    {
      protected override void Execute(CodeActivityContext executionContext)
      {
          //Create the tracing service
          ITracingService trace = executionContext.GetExtension&lt;ITracingService&gt;();
          trace.Trace("*****Tracing Initiated*****");
  
          //Create the IWorkflowContext and the IOrganizationService for communication with CRM
          IWorkflowContext context = executionContext.GetExtension&lt;IWorkflowContext&gt;();
          IOrganizationServiceFactory serviceFactory = executionContext.GetExtension&lt;IOrganizationServiceFactory&gt;();
          IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
          trace.Trace("*****IOrganizationService created*****");
  
          trace.Trace("*****Entity logical Name: " + Email.Get&lt;EntityReference&gt;(executionContext).LogicalName + "*****");
          trace.Trace("*****Entity ID: " + Email.Get&lt;EntityReference&gt;(executionContext).Id + "*****");
  
          if (Email != null &amp;&amp; string.Compare(Email.Get&lt;EntityReference&gt;(executionContext).LogicalName, "email", false) == 0)
          {
              EntityReference retrieveEmail = new EntityReference();
              retrieveEmail.Id = Email.Get&lt;EntityReference&gt;(executionContext).Id;
              retrieveEmail.LogicalName = Email.Get&lt;EntityReference&gt;(executionContext).LogicalName;
              retrieveEmail.Name = Email.Get&lt;EntityReference&gt;(executionContext).Name;
  
              string[] strArrays = new string[1];
              strArrays[0] = "from";
  
              ColumnSet columnSet = new ColumnSet();
              columnSet.AddColumn(strArrays[0]);
  
              RetrieveRequest retrieveRequest = new RetrieveRequest();
              retrieveRequest.Target = retrieveEmail;
              retrieveRequest.ColumnSet = columnSet;
              trace.Trace("*****Retrieve Request declared*****");
  
              RetrieveResponse retrieveResponse = (RetrieveResponse)service.Execute(retrieveRequest);
              trace.Trace("*****Retrieve Response executed*****");
  
              Email businessEntity = (Email)retrieveResponse.Entity;
              trace.Trace("*****businessEnitity retrieved*****");
  
              //ActivityParty activitypartyArray = (ActivityParty)businessEntity.From.FirstOrDefault;
              foreach (ActivityParty activityParty in businessEntity.From)
              {
                  trace.Trace("*****Activity Party Name: " + activityParty.PartyId.LogicalName + "*****");
                  trace.Trace("*****Activity Party Id: " + activityParty.PartyId.Id + "*****");
                  if (activityParty != null &amp;&amp; activityParty != null &amp;&amp; activityParty.PartyId != null)
                  {
                      string str = activityParty.PartyId.LogicalName;
                      if (str.CompareTo("contact") != 0)
                      {
                          trace.Trace("*****Not Contact*****");
                          if (str.CompareTo("account") != 0)
                          {
                              trace.Trace("*****Not Account*****");
                              if (str.CompareTo("lead") != 0)
                              {
                                  trace.Trace("*****Not Lead*****");
                                  if (str.CompareTo("systemuser") != 0)
                                  {
                                      trace.Trace("*****Not System User*****");
                                      if (str.CompareTo("queue") == 0)
                                      {
                                          Queue.Set(executionContext, activityParty.PartyId);
                                          Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
                                          User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
                                          Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
                                          Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
                                      }
                                      else
                                      {
                                          trace.Trace("*****User not found*****");
                                          Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
                                          Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
                                          User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
                                          Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
                                          Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
                                      }
                                  }
                                  else
                                  {
                                      User.Set(executionContext, activityParty.PartyId);
                                      Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
                                      Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
                                      Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
                                      Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
                                  }
                              }
                              else
                              {
                                  Lead.Set(executionContext, activityParty.PartyId);
                                  Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
                                  Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
                                  User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
                                  Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
                              }
                          }
                          else
                          {
                              Account.Set(executionContext, activityParty.PartyId);
                              Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
                              Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
                              User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
                              Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
                          }
                      }
                      else
                      {
                          trace.Trace("*****Contact assigned*****");
                          Contact.Set(executionContext, activityParty.PartyId);
                          Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
                          User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
                          Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
                          Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
                      }
                  }
                  break;
              }
          }
          else
          {
              trace.Trace("*****Email is null*****");
              Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
              Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
              User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
              Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
              Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
          }
      }
  
      #region Properties
      [Input("E-mail")]
      [ReferenceTarget("email")]
      public InArgument&lt;EntityReference&gt; Email { get; set; }
  
      [Output("Account")]
      [ReferenceTarget("account")]
      public OutArgument&lt;EntityReference&gt; Account { get; set; }
  
      [Output("Contact")]
      [ReferenceTarget("contact")]
      public OutArgument&lt;EntityReference&gt; Contact { get; set; }
  
      [Output("Lead")]
      [ReferenceTarget("lead")]
      public OutArgument&lt;EntityReference&gt; Lead { get; set; }
  
      [Output("Queue")]
      [ReferenceTarget("queue")]
      public OutArgument&lt;EntityReference&gt; Queue { get; set; }
  
      [Output("User")]
      [ReferenceTarget("systemuser")]
      public OutArgument&lt;EntityReference&gt; User { get; set; }
      #endregion        
    }
  }
  </code></pre>
  
  <p>So we are upgrading our internal CRM system from 4.0 to 2011, and this was a plugin that was on a workflow. I didn't have the original source code so I didn't really know what the code was doing. But I decompiled the original .dll file after I exported the solution. I tried to rewrite the code for CRM 2011, and here is what I have. After I tested the workflow, I get error saying: "Expected non-empty Guid." The code goes to the very last line after the loop then gives me the error. Here is the trace:</p>
  
  <pre><code>Workflow paused due to error: Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Expected non-empty Guid.Detail: 
  &lt;OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts"&gt;
    &lt;ErrorCode&gt;-2147220989&lt;/ErrorCode&gt;
    &lt;ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /&gt;
    &lt;Message&gt;Expected non-empty Guid.&lt;/Message&gt;
    &lt;Timestamp&gt;2013-02-21T23:46:37.0376093Z&lt;/Timestamp&gt;
    &lt;InnerFault&gt;
     &lt;ErrorCode&gt;-2147220970&lt;/ErrorCode&gt;
      &lt;ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" /&gt;
      &lt;Message&gt;System.ArgumentException: Expected non-empty Guid.
  Parameter name: id&lt;/Message&gt;
      &lt;Timestamp&gt;2013-02-21T23:46:37.0376093Z&lt;/Timestamp&gt;
      &lt;InnerFault i:nil="true" /&gt;
      &lt;TraceText i:nil="true" /&gt;
   &lt;/InnerFault&gt;
   &lt;TraceText&gt;[Microsoft.Xrm.Sdk.Workflow: Microsoft.Xrm.Sdk.Workflow.Activities.RetrieveEntity]
  [RetrieveEntity]
      *****Tracing Initiated*****
      *****IOrganizationService created*****
      *****Entity logical Name: email*****
      *****Entity ID: c49e4c7c-8724-de11-86ce-000c290f83d7*****
      *****Retrieve Request declared*****
      *****Retrieve Response executed*****
      *****businessEnitity retrieved*****
      *****Activity Party Name: contact*****
      *****Activity Party Id: 79ed3a33-8eb9-dc11-8edd-00c09f226ebb*****
      *****Activity Party not null*****
      *****Contact assigned*****
      *****foreach ended*****&lt;/TraceText&gt;
  &lt;/OrganizationServiceFault&gt;
     at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Retrieve(String entityName, Guid id, ColumnSet columnSet, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
     at Microsoft.Crm.Extensibility.InprocessServiceProxy.RetrieveCore(String entityName, Guid id, ColumnSet columnSet)
     at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Retrieve(String entityName, Guid id, ColumnSet columnSet)
     at Microsoft.Crm.Workflow.Services.RetrieveActivityService.&lt;&gt;c__DisplayClass1.&lt;RetrieveInternal&gt;b__0(IOrganizationService sdkService)
     at Microsoft.Crm.Workflow.Services.ActivityServiceBase.ExecuteInTransactedContext(ActivityDelegate activityDelegate)
     at Microsoft.Crm.Workflow.Services.RetrieveActivityService.ExecuteInternal(ActivityContext executionContext, RetrieveEntity retrieveEntity)
     at Microsoft.Crm.Workflow.Services.RetrieveActivityService.Execute(ActivityContext executionContext, RetrieveEntity retrieveEntity)
     at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
     at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)
  </code></pre>