locked
Create a record using ACTION, Javascript, MS CRM 2013 RRS feed

  • Question

  • Hi All,

    I am trying to create a record using Action, Javascript, in MS CRM 2013 Online.

    I went through couple of posts like this and this, which were not clear how to create input parameters and pass input parameter values to the action using Javacsript. I want to pass 3 parameters (string and date fields) to the action. I have tried the below code.

    function CreatReecord() {
        var Name = "Record Created through Action from Javascript";
        var today = new Date();
        var BillingDate = new Date(today.setDate(today.getDate() + 7));
        var Revenue = "100";
        var entityId = Xrm.Page.data.entity.getId();
        var entityName = "opportunity";
        var requestName = "new_test";
        ExecuteActionCreateProject(Name, Revenue, BillingDate, entityId, entityName, requestName);
    }
    function ExecuteActionCreateProject(Name, Revenue, BillingDate, 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>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:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>Name</b:key>";
        requestXML += "            <b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + Name + "</b:value>";
        requestXML += "          </a:KeyValuePairOfstringanyType>";
        requestXML += "          <a:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>Revenue</b:key>";
        requestXML += "            <b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + Revenue + "</b:value>";
        requestXML += "          </a:KeyValuePairOfstringanyType>";
        requestXML += "          <a:KeyValuePairOfstringanyType>";
        requestXML += "            <b:key>BillingDate</b:key>";
        requestXML += "            <b:value i:type=\"c:d:dateTime\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + BillingDate + "</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;
    }

    But it was giving an error message saying

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
    <s:Fault>
    <faultcode xmlns:a="a:ActionNotSupported</faultcode">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>

    Could anyone help me?


    Thanks and Regards. Make sure to &quot;Vote as Helpful&quot; and &quot;Mark As Answer&quot;,if you get answer of your question.


    • Edited by Ravitheja J Tuesday, April 1, 2014 10:30 AM
    Tuesday, April 1, 2014 10:29 AM

All replies

  • HI Ravitheja,

    You can use oDATA/Rest endpoint

    function getCurrentUserFullName()
    {
    var serverUrl;
    var account=Xrm.Page.getAttribute("LookupName").getValue();
    var guid=account[0].id;

    var guidId = guid.replace('{', '').replace('}', '');

    if (Xrm.Page.context.getClientUrl !== undefined)
    {
    serverUrl = Xrm.Page.context.getClientUrl();
    }
    else
    {
    serverUrl = Xrm.Page.context.getServerUrl();
    }
       var oDataURI = serverUrl
            + "/XRMServices/2011/OrganizationData.svc/"
            + "TaskSet"
            + "?$filter="
            + "ActivityId eq (guid'" + guidId+ "')"
            + "&$select=ScheduledEnd";
     

    var userRequest = new XMLHttpRequest();
    userRequest.open("GET", oDataURI, false);
    userRequest.setRequestHeader("Accept", "application/json");
    userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    userRequest.send();
    if (userRequest.status === 200)
    {
    var retrievedUser = JSON.parse(userRequest.responseText).d;
    var userFullName = retrievedUser.results[0].ScheduledEnd;
    var enddate = new Date(parseInt(userFullName.replace("/Date(", "").replace(")/", ""), 10));
    alert(enddate);

    }
    else
    {
    alert("error");
    }
    }

    Tuesday, April 1, 2014 11:55 AM
  • Thanks for the reply Aamir Hijazi.

    I am not looking for oData/REST endpoints. I want to use Action.


    Thanks and Regards. Make sure to &quot;Vote as Helpful&quot; and &quot;Mark As Answer&quot;,if you get answer of your question.

    Tuesday, April 1, 2014 12:12 PM
  • Are you sure the datatype is set correctly for the BillingDate attribute. You've got i:type=\"c:d:dateTime\", which looks wrong. dateTime is defined within the http://www.w3.org/2001/XMLSchema\ namespace, so I'd expect you should have i:type=\"c:dateTime\"

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, April 1, 2014 2:54 PM
    Moderator