Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Calling the Close Opportunity Dialog Box via JavaScript

(0) ShareShare
ReportReport
Posted on by 125

Hi,

I had a JavaScript that would bring up the Close Opportunity dialog box when a field value was changed to "No" in that Opportunity. This JavaScript was functioning in Dynamics CRM 2013, but since upgrading to CRM 2015 I get the error "Object doesn't support property or method 'OpportunityClose'"

Below is the JavaScript I'm using

function opportunityCloseDevelop(won) {
var decision = Xrm.Page.getAttribute("pacer_preparefordevelopstage").getValue();
if(decision == 590470001){
Mscrm.CommandBarActions.OpportunityClose(won);
}
}

The JavaScript is being called on the event OnChange for that field, where I'm passing the false parameter, since I want to close the Opportunity as Lost whenever the field value changes to No. How can I accomplish this?

Thanks

*This post is locked for comments

  • Syed_Faisal Profile Picture
    815 on at
    RE: Calling the Close Opportunity Dialog Box via JavaScript

    Hi,

    Could you please help me on this ?

  • 00f137054c6340ccb3ea9447e34ad682 Profile Picture
    225 on at
    RE: Calling the Close Opportunity Dialog Box via JavaScript

    I know is late but i got working.

    //JavaScript

    //won

    Mscrm.OpportunityCommandActions.opportunityClose(1);

    //lost

    Mscrm.OpportunityCommandActions.opportunityClose(0);

    thanks in advance,

    JP

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Calling the Close Opportunity Dialog Box via JavaScript

    The question's already got a verified answer, but I had a similar issue where Mscrm.CommandBarActions.OpportunityClose didn't work anymore in 2015. I believe the command simply changed.

    Try using

    Mscrm.OpportunityCommandActions.opportunityClose

    instead with boolean values true/false

    Library is also different: $webresource:Opportunity_main_system_library.js

  • Avneet Profile Picture
    125 on at
    RE: Calling the Close Opportunity Dialog Box via JavaScript

    Thanks! I decided to close the opportunity using a workflow, rather than using the javascript.

  • flavia.takushi Profile Picture
    on at
    RE: Calling the Close Opportunity Dialog Box via JavaScript

    Hi Avneet,

    I think Custom Code Validation 2015 tool will help you identifying if there is any unsupported function on your organization:

    www.microsoft.com/.../details.aspx

    You can also use F12 developer tools in the browser to debug your javascript code and check which is the exactly point you are having issues.

    For more information, please check:

    msdn.microsoft.com/.../hh771584.aspx

  • Verified answer
    Royal King Profile Picture
    27,686 on at
    RE: Calling the Close Opportunity Dialog Box via JavaScript

    You are using the crm Internal method that's why it is failing.  Use the below script that should close opportunity as won

    function closeOpportunityAsWon() {

       var opportunityId=Xrm.Page.data.entity.getId();

       var xml = "" +

       "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">" +

           "<s:Body>" +

               "<Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">" +

                   "<request i:type=\"b:WinOpportunityRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">" +

                       "<a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">" +

                       "<a:KeyValuePairOfstringanyType>" +

                           "<c:key>OpportunityClose</c:key>" +

                           "<c:value i:type=\"a:Entity\">" +

                               "<a:Attributes>" +

                                   "<a:KeyValuePairOfstringanyType>" +

                                       "<c:key>opportunityid</c:key>" +

                                       "<c:value i:type=\"a:EntityReference\">" +

                                           "<a:Id>" + opportunityId + "</a:Id>" +

                                           "<a:LogicalName>opportunity</a:LogicalName>" +

                                           "<a:Name i:nil=\"true\"/>" +

                                       "</c:value>" +

                                   "</a:KeyValuePairOfstringanyType>" +

                                   "<a:KeyValuePairOfstringanyType>" +

                                       "<c:key>subject</c:key>" +

                                       "<c:value i:type=\"d:string\" xmlns:d=\"www.w3.org/.../XMLSchema\">Won the Opportunity!</c:value>" +

                                    "</a:KeyValuePairOfstringanyType>" +

                                       "</a:Attributes>" +

                                       "<a:EntityState i:nil=\"true\"/>" +

                                           "<a:FormattedValues/>" +

                                           "<a:Id>00000000-0000-0000-0000-000000000000</a:Id>" +

                                           "<a:LogicalName>opportunityclose</a:LogicalName>" +

                                           "<a:RelatedEntities/>" +

                                           "</c:value>" +

                                    "</a:KeyValuePairOfstringanyType>" +

                                    "<a:KeyValuePairOfstringanyType>" +

                                       "<c:key>Status</c:key>" +

                                       "<c:value i:type=\"a:OptionSetValue\">" +

                                           "<a:Value>3</a:Value>" +

                                       "</c:value>" +

                                   "</a:KeyValuePairOfstringanyType>" +

                                   "</a:Parameters>" +

                                   "<a:RequestId i:nil=\"true\"/>" +

                                   "<a:RequestName>WinOpportunity</a:RequestName>" +

                     "</request>" +

                 "</Execute>" +

            "</s:Body>" +

       "</s:Envelope>";

       var xmlHttpRequest = new XMLHttpRequest();

       xmlHttpRequest.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", false);

       xmlHttpRequest.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

       xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

       xmlHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");

       xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

       xmlHttpRequest.send(xml);

       var resultXml = xmlHttpRequest.responseText;

       alert(resultXml);

    Xrm.Utility.openentityform(Xrm.Page.data.entity.getEntityName(),opportunityId)

    }

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,099 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans