如何使用 Javascript change/update Dynamics crm 实体记录数据和状态从活动状态变为非活动状态

How to change/update Dynamics crm entity record data and state from Active to Inactive with Javascript

我在更改实体记录 (Dynamics CRM 2015) 的状态、状态和附加数据时遇到问题。 .我正在尝试更新刚刚创建的实体记录中的数据。 但是我总是收到错误的请求。请帮助我!?

这是我的代码。

  • Entity name: "Send Email Campaign"
  • Entity Schema Name: "concep_emailcampaign"
  • Record test name: 19/05/17-2

function CreateInactiveEmailCampaign(data) { 

    var emailcampaignId, EmailCampaignName;
    var context = Xrm.Page.context; 
    var serverUrl = Xrm.Page.context.getClientUrl(); 
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc"; 
    var CRMObject = new Object(); 
    ///////////////////////////////////////////////////////////// 
    // Specify the ODATA entity collection 
    var ODATA_EntityCollection = "/concep_emailcampaignSet"; 
    ///////////////////////////////////////////////////////////// 

    CRMObject.concep_name               = data.Name; 
    EmailCampaignName                   = data.Name;
    CRMObject.concep_Subject            = data.Subject;
    CRMObject.concep_SentDateTime   = data.sentdatetime;  
    CRMObject.concep_Recipients   = data.recipientsCount; 
    CRMObject.concep_CampaignID   = data.CampaignID;  
    CRMObject.concep_CampaignCode   = data.CampaignCode;     
    
    //Parse the entity object into JSON 
    var jsonEntity = window.JSON.stringify(CRMObject); 
    //Asynchronous AJAX function to Create a CRM record using OData 
    $.ajax({ type: "POST", 
        contentType: "application/json; charset=utf-8", 
        datatype: "json", 
        url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection, 
        data: jsonEntity, 
        beforeSend: function (XMLHttpRequest) { 
            //Specifying this header ensures that the results will be returned as JSON. 
            XMLHttpRequest.setRequestHeader("Accept", "application/json"); 
        }, 
        success: function (data, textStatus, XmlHttpRequest) { 
            //alert("success"); 
            debugger;
            var NewCRMRecordCreated = data["d"]; 
            ChangeRecordStatus(NewCRMRecordCreated, 1, 2, serverUrl); 
        }, 
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("failure"); 
        } 
    }); 
}
function ChangeRecordStatus(data, stateCode, statusCode, serverUrl) {
 // I need to update the record with
    //CRMObject.statecode    = {}
    //CRMObject.statecode.Value   = 1;    //Inactive
    //CRMObject.statuscode    = {Value:2}
    //CRMObject.statuscode.Value  = 2;    //Inactive
    //CRMObject.concep_Campaign   = {}
    //CRMObject.concep_Campaign.Id   = data.Campaign;  
    //CRMObject.concep_SendAccount  = {}
    //CRMObject.concep_SendAccount.Id  = data.SendAccount;  
    //CRMObject.concep_SendAccount.LogicalName  = data.SendAccount;
    //CRMObject.concep_SendAccount.Name  = data.SendAccount;
    //CRMObject.concep_ResponsesTotal = ''
    //CRMObject.concep_ResponsesToRecipientsRate = ''
    //CRMObject.concep_concep_emailcampaign_list = ''
    //CRMObject.concep_emailcampaign_campaign. = ''
    //CRMObject.concep_emailcampaign_sendaccount. = ''
    //CRMObject.concep_surveyresponse_emailcampaign. = ''
    //CRMObject.user_concep_emailcampaign. = ''
 var serverUrl = serverUrl;
 var RECORD_ID = data.concep_emailcampaignId
 var EntityLogicalName = data.concep_name;
   var request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    request += "<s:Body>";
    request += "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    request += "<request i:type=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    request += "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>EntityMoniker</c:key>";
    request += "<c:value i:type=\"a:EntityReference\">";
    request += "<a:Id>" + RECORD_ID + "</a:Id>";
    //request += "<a:LogicalName>" + EntityLogicalName + "</a:LogicalName>";
    request += "<a:Name i:nil=\"true\" />";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>State</c:key>";
    request += "<c:value i:type=\"a:OptionSetValue\">";
    request += "<a:Value>" + stateCode + "</a:Value>";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "<a:KeyValuePairOfstringanyType>";
    request += "<c:key>Status</c:key>";
    request += "<c:value i:type=\"a:OptionSetValue\">";
    request += "<a:Value>" + statusCode + "</a:Value>";
    request += "</c:value>";
    request += "</a:KeyValuePairOfstringanyType>";
    request += "</a:Parameters>";
    request += "<a:RequestId i:nil=\"true\" />";
    request += "<a:RequestName>SetState</a:RequestName>";
    request += "</request>";
    request += "</Execute>";
    request += "</s:Body>";
    request += "</s:Envelope>";
    //send set state request
    $.ajax({
        type: "POST",
        contentType: "text/xml; charset=utf-8",
        datatype: "xml",
        url:  serverUrl + "/XRMServices/2011/Organization.svc/web",
        data: request,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
            XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            alert("Success");
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
    alert(XMLHttpRequest);
             alert(errorThrown);
        }
    });
}

这可能有帮助:Active/Deactive Record using JS in CRM

我可以知道您为什么使用这种方法 - 有什么具体原因吗? Bcoz,这可以在 UI 工作流程中轻松实现,无需代码。

您现在正在使用 JS 进行的另一种方法。但是你从哪里调用这个方法 - CreateInactiveEmailCampaign?在表单加载时?或功能区点击?

您必须检查更新模式的表单类型(参考:https://msdynamicscrmblog.wordpress.com/2013/12/11/get-form-types-and-modes-in-dynamics-crm-2013/)。否则创建模式不会有 RecordId。

调试并查看您在哪一行出现错误。

问题是我发送给动态的错误值。在 Microsoft 支持页面中设置了错误的值。

  var request = "" +
    "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>" +
    "    <s:Header>" +
    "        <SdkClientVersion xmlns='http://schemas.microsoft.com/xrm/2011/Contracts'></SdkClientVersion>" +
    "    </s:Header>" +
    "    <s:Body>" +
    "        <Execute xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
    "            <request i:type='b:SetStateRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>" +
    "                <a:Parameters xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>" +
    "                    <a:KeyValuePairOfstringanyType>" +
    "                        <b:key>EntityMoniker</b:key>" +
    "                        <b:value i:type='a:EntityReference'>" +
    "                            <a:Id>" + RECORD_ID + "</a:Id>" +
    "                            <a:LogicalName>concep_emailcampaign</a:LogicalName>" +
    "                        </b:value>" +
    "                    </a:KeyValuePairOfstringanyType>" +
    "                    <a:KeyValuePairOfstringanyType>" +
    "                        <b:key>State</b:key>" +
    "                        <b:value i:type='a:OptionSetValue'>" +
    "                            <a:Value>1</a:Value>" +   //<----------
    "                        </b:value>" +
    "                    </a:KeyValuePairOfstringanyType>" +
    "                    <a:KeyValuePairOfstringanyType>" +
    "                        <b:key>Status</b:key>" +
    "                        <b:value i:type='a:OptionSetValue'>" +
    "                            <a:Value>-1</a:Value>" +   //<----------
    "                        </b:value>" +
    "                    </a:KeyValuePairOfstringanyType>" +
    "                    <a:KeyValuePairOfstringanyType>" +
    "                        <b:key>MaintainLegacyAppServerBehavior</b:key>" +
    "                        <b:value i:type='c:boolean' xmlns:c='http://www.w3.org/2001/XMLSchema'>true</b:value>" +
    "                    </a:KeyValuePairOfstringanyType>" +
    "                </a:Parameters>" +
    "                <a:RequestId i:nil='true' />" +
    "                <a:RequestName>SetState</a:RequestName>" +
    "            </request>" +
    "        </Execute>" +
    "    </s:Body>" +
    "</s:Envelope>";