locked
CRM 2013: Calling actions from javascript RRS feed

  • Question

  • Hi all,

    I was trying to call action from javascript using the below code:

    function Approve()
    {
        debugger;
        var reason = "aaaaaaaaaaaaaaaaaa";
        var entityId = Xrm.Page.data.entity.getId();
        var entityName = "serviceappointment";
        var requestName = "new_SetRejectionReason";
        ExecuteActionCreateProject(reason, entityId, entityName, requestName);
    }
    
    function ExecuteActionCreateProject(reason, entityId, entityName, requestName)
    {
        // Creating the request XML for calling the Action
        var requestXML = ""
        requestXML += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        requestXML += "  <s:Body>";
        requestXML += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
        requestXML += "      <request xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
        requestXML += "        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
        requestXML += "          <a:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>Reason</b:key>";
        requestXML += "            <b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">"+reason+"</b:value>";
        requestXML += "          </a:KeyValuePairOfstringanyType>";
        requestXML += "          <a:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>Target</b:key>";
        requestXML += "            <b:value i:type=\"a:EntityReference\">";
        requestXML += "              <a:Id>"+entityId+"</a:Id>";
        requestXML += "              <a:LogicalName>"+entityName+"</a:LogicalName>";
        requestXML += "              <a:Name i:nil=\"true\" />";
        requestXML += "            </b:value>";
        requestXML += "          </a:KeyValuePairOfstringanyType>";
        requestXML += "        </a:Parameters>";
        requestXML += "        <a:RequestId i:nil=\"true\" />";
        requestXML += "        <a:RequestName>"+requestName+"</a:RequestName>";
        requestXML += "      </request>";
        requestXML += "    </Execute>";
        requestXML += "  </s:Body>";
        requestXML += "</s:Envelope>";
        var req = new XMLHttpRequest();
        req.open("POST", GetClientUrl(), false)
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationServic/Execute");
        req.send(requestXML);
        //Get the Resonse from the CRM Execute method
        var response = req.responseXML.xml;
    }
    function GetClientUrl() {
        if (typeof Xrm.Page.context == "object") {
            clientUrl = Xrm.Page.context.getClientUrl();
        }
        var ServicePath = "/XRMServices/2011/Organization.svc/web";
        return clientUrl + ServicePath;
    }

    And this the response that I get:

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    	<s:Body>
    		<s:Fault>
    		<faultcode xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:ActionNotSupported</faultcode>
    		<faultstring xml:lang="en-US">The message with Action 'http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationServic/Execute' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
    		</faultstring>
    		</s:Fault>
    	</s:Body>
    </s:Envelope>

    Kindly advise


    Friday, December 13, 2013 10:40 PM

Answers

All replies

  • Hello Mostafa,

    I have a question - in which way you've build Soap Xml you used in your code? Recheck my article that describes step-by-step usage of Actions for .Net and JS developers - http://a33ik.blogspot.com/2013/10/custom-actions-walkthrough-for-net-and.html


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Saturday, December 14, 2013 4:23 PM
    Moderator
  • I have created it using the SoapLogger, however I was able to execute it using .Net but for JavaScritpt it returns status 500

    Have you ever faced this issue before ?

    Saturday, December 14, 2013 4:45 PM
  • No, I have not. Try to use framework I've developed to work with Actions. I developed it once and now use it for every project I need to work with Actions.

    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Saturday, December 14, 2013 5:41 PM
    Moderator
  • Would please help me reuse it as I am newb in javascript

    My understanding that I need to replace the requestXml with a string that contains my request xml, i.e:

    CustomActionExecutor.Execute(
        {
            requestXml: myRequestXML,
            async: true,
            successCallback: function(result){
            },
            errorCallback: function (e) {
            }
        });

    And then this will call the Execute function in your library to execute the request

    Please let me if this wrong

    Saturday, December 14, 2013 6:32 PM
  • Yes, to requestXml you should put your generated Soap Xml.

    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Saturday, December 14, 2013 6:44 PM
    Moderator
  • Thanks alot Andrii, finally it works :)
    Saturday, December 14, 2013 7:12 PM