title: How to set the Opportunity Status - Dynamics CRM?
body: <p>I'm trying to update the status of <strong>open opportunity</strong> by using the <em>WinOpportunityRequest &amp; LoseOpportunityRequest</em> API provided in the MSDN. 
  I've followed the code which was given in the MSDN &amp; I even referred to stackoverflow's <a href="https://stackoverflow.com/questions/39620048/dynamics-crm-online-2016-set-opportunity-statecode">Set Opportunity Status</a></p>
  
  <p>But, When I run this following code for the <strong>open opportunity</strong> it throws error stating that </p>
  
  <pre><code>LoseOpportunityRequest req = new LoseOpportunityRequest();
  Entity opportunityClose = new Entity("opportunityclose");
  opportunityClose.Attributes.Add("opportunityid", new EntityReference(OptyEntityName, new Guid("xxxx-xxx")));
  opportunityClose.Attributes.Add("subject", "Lost the Opportunity!");
  req.OpportunityClose = opportunityClose;
  // 4 = Cancelled and 5 = Out-Sold
  req.Status = new OptionSetValue(4);
  LoseOpportunityResponse resp = (LoseOpportunityResponse)_serviceProxy.Execute(req);
  </code></pre>
  
  <p>Error -</p>
  
  <blockquote>
    <p>4 is not a valid status code on opportunity with Id(Guid)</p>
  </blockquote>
  
  <p>When I tried to change the status of the <strong>closed opportunity</strong> it says that <strong>opportunity is already closed</strong>.</p>
  
  <p>One more thing to consider is this status in my CRM has a padlock icon that means it is locked.</p>
  
  <p>So is it possible to change the status or not and is it based on the role?</p>