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

Notifications

Announcements

No record found.

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

I have the same question (0)
  • Verified answer
    Royal King Profile Picture
    27,686 on at

    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)

    }

  • flavia.takushi Profile Picture
    on at

    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

  • Avneet Profile Picture
    125 on at

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

  • Suggested answer
    Community Member Profile Picture
    on at

    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

  • 00f137054c6340ccb3ea9447e34ad682 Profile Picture
    225 on at

    I know is late but i got working.

    //JavaScript

    //won

    Mscrm.OpportunityCommandActions.opportunityClose(1);

    //lost

    Mscrm.OpportunityCommandActions.opportunityClose(0);

    thanks in advance,

    JP

  • Syed_Faisal Profile Picture
    815 on at

    Hi,

    Could you please help me on this ?

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans