2014-09-04 75 views
0

我有一個自定義JavaScript約會窗體。絲帶上有一個按鈕。按鈕應該改變狀態碼然後保存表格。但是當我試圖保存時,如果有一個字段發生了變化,那麼這個表單不會自動保存。下面是我的代碼。不保存表格

Xrm.Page.data.entity.save(); 
var newStatus = 3; 
var json = new JSonObject(); 
var data = json.SetState(Xrm.Page.data.entity.getId(), Xrm.Page.data.entity.getEntityName(), 1, newStatus); 
if (data != true) 
    alert("Error!"); 
else { 
    Xrm.Page.data.entity.save('saveandclose'); 
    //window.parent.location.reload(); 
} 

我試過一切。首先保存然後setState然後再保存等。如果代碼是這樣的,它會導致一個錯誤,即「無法更新關閉或取消活動」。如果它先保存然後重新加載然後setState然後保存然後重新加載,那麼更改的字段沒有得到最後的值。它獲取從保存按鈕手動保存的以前的值。

回答

1
function changeRecordStatus(RECORD_ID,stateCode,statusCode) { 

    // create the SetState request 
    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>cmic_systemusersalesterritoryassociation</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: Xrm.Page.context.getServerUrl() + "/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) { 
      Xrm.Page.ui.close(); 
     }, 
     error: function (XMLHttpRequest, textStatus, errorThrown) { 
      alert(errorThrown); 
     } 
    }); 
} 

我用上面提到的功能更改RECORDSTATUS異步 像下面創建一個函數名AppointmentOnSave()並撥打了上面的函數:

function AppointmentOnSave(){ 
var AppointmentGuid = Xrm.Page.data.entity.getId(); 
var StateCode = 1; 
var StatusCode= 3; 
Xrm.Page.data.entity.save("saveandclose"); 
changeRecordStatus(AppointmentGuid,StateCode,StatusCode); //Mark Completed 

} 

Entity State Codes here

這裏是第二種方法和簡單之一: 試試這個:

首先需要在CRM這三個JavaScript文件添加爲webresource

jquery1.4.1.min.js json2.js SDK.JQuery.js

這些文件是在SDK腳本文件夾下(SDK \ samplecode \ JS \ restendpoint \ jqueryrestdataoperations \ jqueryrestdataoperations \腳本)

則:

function AppointmentOnSave(){ 
var AppointmentGuid = Xrm.Page.data.entity.getId(); 
var StateCode = 1; 
var StatusCode= 3; 
var Appoinment= {}; 
Appoinment.StateCode=1; 
Appoinment.StatusCode=3; 

setTimeout(function(){ 

SDK.JQuery.updateRecord(
AppointmentGuid , 
Appoinment, 
"task", 
function success (msg) { 
    alert(msg); 

}, 
function failed (error) { 
    alert(error); 
} 
); 
}, 500);at 
Xrm.Page.data.entity.save("saveandclose"); 
}