Following code can be used to close Opportunity as Won using JavaScript and WebApi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
var opportunityclose = { "opportunityid@odata.bind": "/opportunities(628CF01A-AED1-E411-80EF-C4346BAC7BE8)",//replace with id of opportunity "actualrevenue": 100, "actualend": new Date(), "description": "Your description here" }; var parameters = { "OpportunityClose": opportunityclose, "Status": -1 }; var context; if (typeof GetGlobalContext === "function") { context = GetGlobalContext(); } else { context = Xrm.Page.context; } var req = new XMLHttpRequest(); req.open("POST", context.getClientUrl() + "/api/data/v8.2/WinOpportunity", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 204) { //Success - No Return Data - Do Something } else { var errorText = this.responseText; //Error and errorText variable contains an error - do something with it } } }; req.send(JSON.stringify(parameters)); |
Can You Please Give Rest Api Code for update opportunity status to won or lost
Hello,
It’s almost the same. Just replace WinOpportunity with LoseOpportunity.
Thanks,
Andrew