title: Get related entity statecode value to use as a check
body: <p>I need to be able to only execute my code upon the condition that the related opportunity has a <code>statecode</code> of <code>1</code></p>
  
  <p>In my code I am able to use the <a href="https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.generatesalesorderfromopportunityrequest.aspx" rel="nofollow">GenerateSalesOrderFromOpportunityRequest</a> class provided by the Microsoft Dynamics SDK to create a new sales order when a opportunityclose activity is created.</p>
  
  <p>The drawback of this approach is that an <code>opportunityclose</code> activity is created by the system when an opportunity is closed as won(<code>1</code>) or lost(<code>2</code>). Also, there are no attributes on the <code>opportunityclose</code> activity that say if it was won or lost. So the only way to find it out is to get that attribute from the related opportunity.</p>
  
  <p>In my code I'm able to get other attributes from the related opportunity, like <code>name</code>, but I have not been able to get any other value for <code>statecode</code> other that <code>0</code>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>Entity postImageEntity = (context.PostEntityImages != null &amp;&amp; context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
  
              if (postImageEntity.LogicalName == "opportunityclose" &amp;&amp; postImageEntity.Attributes.Contains("opportunityid") &amp;&amp; postImageEntity.Attributes["opportunityid"] != null)
              {
                  // Create an entity reference for the related opportunity to get the id for the GenerateSalesOrderFromOpportunityRequest class
                  EntityReference entityRef = (EntityReference)postImageEntity.Attributes["opportunityid"];
  
                  // Retrieve the opportunity that the closed opportunity activity was created for.
                  Entity RelatedEntityRef = service.Retrieve("opportunity", entityRef.Id, new ColumnSet( new String[] {"statecode","statuscode", "name"}));
  
                  OptionSetValue StateCode = (OptionSetValue)RelatedEntityRef.Attributes["statecode"];
  
                  OptionSetValue StatusCode = (OptionSetValue)RelatedEntityRef.Attributes["statuscode"];
  
                  string OppName = (string)RelatedEntityRef.Attributes["name"];
  
                  if (entityRef.LogicalName == "opportunity" &amp;&amp; StateCode.Value == 1)
                  {
                      try
                      {
                          GenerateSalesOrderFromOpportunityRequest req = new GenerateSalesOrderFromOpportunityRequest();
                          req.OpportunityId = entityRef.Id;
                          req.ColumnSet = new ColumnSet(true);
                          GenerateSalesOrderFromOpportunityResponse resp = (GenerateSalesOrderFromOpportunityResponse)service.Execute(req);
                      }
                      catch (FaultException ex)
                      {
                          throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                      }
                  }
              }
  </code></pre>
  
  <p>Recap: For this to work I just need to be able to get the actual <code>statecode</code> value of the <code>opportunity</code> related to the <code>opportunityclose</code>. Currently I have only been able to get <code>0</code> even if I know that the state code of the opportunity is <code>1</code>.</p>
  
  <p>Other Info:</p>
  
  <ul>
  <li>This is for Microsoft Dynamics Online 2013/2015(works with both)</li>
  <li>Using SKD v6.1.1</li>
  <li>Plugin works, but fires whether the opportunity is won or lost. (not intended)</li>
  </ul>