web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Close Opportunity as Close as WON or Close as LOST via API

(0) ShareShare
ReportReport
Posted on by 10

Hi,

I would like to change Opportunity status to Close as WON or Close as LOST via API:

I use PATCH:
...api/data/v9.1/opportunities(f58cca41-f506-ea11-a811-000d3a2c59aa)

with JSON:
{"statecode": 1, "statuscode": 3, "actualvalue": 0.0, "salesstage": 3, "actualvalue_base": 0.0, "actualclosedate": "2019-11-15"}

But still get:
{"error":{"code":"0x80040216","message":"This message can not be used to set the state of opportunity to won. In order to set state of opportunity to won, use the won message instead.","innererror":{"message":"This message can not be used to set the state of opportunity to won. In order to set state of opportunity to won, use the won message instead."


I have tried to find a solution on Internet but without any success.

Please, help :)

I have the same question (0)
  • Verified answer
    Thangamani Profile Picture
    507 on at
    RE: Close Opportunity as Close as WON or Close as LOST via API

    Hi,

    Can you pls try to send only Statecode and statuscode for WON message along with opportunity ID and ignore ,Actualclosedate,salestage and actualvalue_base.

    Pls try to to use the below.

    POST http://[Organization URI]/api/data/v8.2/WinOpportunity HTTP/1.1
    OData-MaxVersion: 4.0
    OData-Version: 4.0
    Content-Type: application/json; charset=utf-8

    {
    "Status":3,
    "OpportunityClose":{
    "subject":"Won Opportunity",
    "opportunityid@odata.bind":"http://[Organization URI]/api/data/v8.2/opportunities(47920da5-fb4a-e611-80d5-00155da84802)"
    }
    }

    Regards,

    Thangamani

  • Rozpalacz Profile Picture
    10 on at
    RE: Close Opportunity as Close as WON or Close as LOST via API

    When I send

    {"statecode": 1, "statuscode": 3}

    still get the same message.

    I noticed that when opportunity has status WON I can use API to OPEN opportunity by sending

    {"statecode": 0, "statuscode": 1}

    but I can not close it :(

  • Verified answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at
    RE: Close Opportunity as Close as WON or Close as LOST via API

    Hi,

    Please see the below link where "Andrew Butenko" showed, "How to close Opportunity as Won using WebApi":

    butenko.pro/.../  

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Close Opportunity as Close as WON or Close as LOST via API

    Hey,

    I am not fully sure whether this will work with the Opportunity entity and which version you are on.

    But a couple of days ago I wrote a short custom JS-command for a custom ribbon on a custom entity.

    When clicked a confirmation dialog pops up and if confimed via "OK", I set the statecode and statuscode successfully on that record. Keeping in my that both statecode and statuscode have to be "related". First I needed to set the statecode and then the related statuscode.

    function Close_Record(PrimaryControl){
    	var formContext = PrimaryControl;
    
    	var confirmStrings = { text:"You are about to close the record as won?", title:"Record closure" };
    	var confirmOptions = { height: 200, width: 450 };
    	
    	Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
    	function (success) {    
    	    if (success.confirmed){
    	    	formContext.getAttribute("statecode").setValue(1);
    			formContext.getAttribute("statuscode").setValue(2); 
    		    formContext.data.refresh(true).then(null, null);	      			    		    
    		}
    	    else{
    	        console.log("Dialog closed using Cancel button or X.");
    	    }
    	});
    }

    Lines 12 to 14 in my code are ones that do the trick.
    I applied the code to an instance in version 9.1 where UI is the new UCI.

    I've checked for the Opportunity closure codes and it would be this code in my instance for setting it to "WON":

    function Won_Opportunity(executionContext){
    	var formContext = executionContext.getFormContext()
    
    	formContext.getAttribute("statecode").setValue(1);
    	formContext.getAttribute("statuscode").setValue(3);
    	formContext.data.refresh(true).then(null, null);
    }

    P.S.: I also have both fields on the form but as hidden fields in order to access them via the getAttribute.

    Please mark the answer as verified if it helped you on your issue.
    Happy developing

  • Suggested answer
    Vipin J Profile Picture
    1,603 on at
    RE: Close Opportunity as Close as WON or Close as LOST via API

    How to Won, Lost or Reopen opportunity in Dynamic CRM using Web API

    Refer here

    https://vjcity.blogspot.com/2020/06/how-to-won-lost-or-reopen-opportunity.html

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 179 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 110

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 61 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans