使用Javascript在Microsoft Dynamics CRM 2016中运行操作

时间:2016-06-16 09:25:40

标签: javascript dynamics-crm-2016

我在Action下为自定义实体Process创建了new_enrollment。我没有为该操作创建任何I / O参数。现在,通过使用以下代码片段,我想运行该自定义操作,以便在执行操作时触发插件并创建电话呼叫。

但似乎action没有被执行。任何建议或帮助,以便我可以action执行。

function emailOrderDetails(){

    var entityId = Xrm.Page.data.entity.getId();
    var entityName = "new_enrollment";
    var requestName = "new_sendemail";
    RunAction(entityId, entityName, requestName);
    window.location.reload(true);
}

function RunAction(entityId, entityName, requestName) {
    try{

    // 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: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/IOrganizationService/Execute");
    req.send(requestXML); 
    //Get the Response from the CRM Execute method
    //var response = req.responseXML.xml;
    }
    catch(e){
        alert(e.Message);
    }
}

function GetClientUrl() {
    if (typeof Xrm.Page.context == "object") {
        clientUrl = Xrm.Page.context.getClientUrl();
    }
    var ServicePath = "/XRMServices/2011/Organization.svc/web";
    return clientUrl + ServicePath;
}

1 个答案:

答案 0 :(得分:0)

您正在使用支持WebAPI的2016,并且使用WebAPI非常容易。下面是使用JS调用自定义操作的工作示例。我有2个输出变量,一个由自定义操作设置,另一个由插件“ptext”我在此Action上注册。 希望它能解决你的问题。

function CallCAction(context) {       
 var Credit_Limit = Xrm.Page.getAttribute("creditlimit").getValue();
    var Credit_hold = Xrm.Page.getAttribute("creditonhold").getValue();
    if(Credit_hold !=null && Credit_Limit!=null){
        var actionName = "new_Preferred_Check";
        var inputParam = {
            "Credit_Limit": Credit_Limit,
            "Credit_hold": Credit_hold
        };
        Xrm.Page.ui.setFormNotification("Processing...","INFO","processingFId");
        var actionResponse = callPreferredCust(actionName, inputParam);

        if (actionResponse != null) {
            Xrm.Page.getAttribute("new_preferredcust").setValue(actionResponse.Preferred_Cust);
            alert(actionResponse.ptext);
            Xrm.Page.ui.clearFormNotification("processingFId");
        }        
    }    
}
function callPreferredCust(actionName, inputParam) {
    var result = null;
    var req = new XMLHttpRequest();
    var uri = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/";
    try {
        req.open("POST",encodeURI(uri+actionName),false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.onreadystatechange = function () {
            if(this.readyState==4){
                req.onreadystatechange = null;
                if(this.status==200){
                    result = JSON.parse(this.response);
                }else{
                    var err = JSON.parse(this.response).error;
                    alert(err.message);
                }
            }
        };
        req.send(JSON.stringify(inputParam));
        return result;

    }catch(err){
        alert(err.message);
    }
}