How to Reopen, Update, and Close an Opportunity in a Plugin

I recently had a situation when I needed to update a field on an Opportunity that had been closed when a related record is created. (It has information that isn’t available at the time of Close.) Since Opportunities that are won (or lost) are deactivated/read only, I needed to re-open the opportunity, update the field and then re-close the Opportunity. I’ll need something like this again in the future, so I decided to put it on the blog.

Below is code that runs after the processing in the plugin is completed. If you need the full code for a plugin, refer to the CRM 2011 SDK.

/* Reopen the Opportunity record */
SetStateRequest openOpp = new SetStateRequest();
openOpp.EntityMoniker = oppRef;
openOpp.State = new OptionSetValue(0);

// Store the status so we can reset it back to the same value later
// We don’t want to lose the reason why we won this opportunity

OptionSetValue status = (OptionSetValue)opportunity[“statuscode”];

// We don’t want to lose the amount either, the other values should save fine

Money actualValue = opportunity[“actualvalue”];

// set the status to Qualification temporarily

openOpp.Status = new OptionSetValue(200000);

/* Now the Opportunity can have fields updated */

// Set the Project Number

opportunity[“new_project”] = projectNo;

// Set the status code to the generic value we use for open opportunities

opportunity[“statuscode”] = new OptionSetValue(200000);

/* Re-Close the Opportunity record */

WinOpportunityRequest winOpp = new WinOpportunityRequest();

// update the status to what it was before the Opportunity was reopened and close as Won

winOpp.OpportunityClose = new Entity(“opportunityclose”);
winOpp.OpportunityClose[“opportunityid”] = oppRef;
winOpp.OpportunityClose[“actualvalue”] = actualValue ;
winOpp.Status = status;


If you needed to do this update for both Won and Lost Opportunities, you would need logic to switch between the WinOpportunityRequest and LostOpportunityRequest.

Have questions about Dynamics CRM customizations or development work? Contact us today

Phone: 312-602-4000
222 W. Adams
Chicago, IL 60606
Show Buttons
Share On Facebook
Share On Twitter
Share on LinkedIn
Hide Buttons