我們在CRM 2013上。要求是完全從任務表單中刪除「關閉」按鈕,然後創建一個新的按鈕'取消'取消任務。所以當點擊這個按鈕時,任務將會被保存爲取消。客戶關係管理2013年:當從高級查找打開記錄時,Javascript取消任務不起作用
所以我們跟着一個關於使用JavaScript改變記錄博客由大師普拉薩德:http://guruprasadcrm.blogspot.com/2013/02/change-record-status-using-javascript.html
腳本工作正常。當點擊「取消」按鈕時,它會將狀態更改爲取消。
我還在SOAP請求之前包含了這行代碼,以防在發送取消請求之前修改了頁面。
Xrm.Page.data.entity.save("saveandclose");
但是,我們發現一個非常奇怪的錯誤,出於某種原因,當從Advanced Find打開記錄時,腳本不會被觸發。這就好像該頁面在點擊這個新的自定義取消按鈕時立即關閉。
您是否知道在從高級查找打開的記錄上運行的腳本與常規腳本之間是否存在差異?
這裏是改變記錄狀態的JavaScript:
function changeRecordStatus(RECORD_ID, stateCode, statusCode) {
//first of all save all data before sending the request, in case if there are some data that hasn't been saved
Xrm.Page.data.entity.save("saveandclose");
// 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>task</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);
}
});
}
那麼這裏就是新的自定義功能區按鈕取消任務的命令定義。我將statuscode = 2和statecode = 6傳入上面的函數。
<CommandDefinitions>
<CommandDefinition Id="Mscrm.SaveAsCancelled">
<EnableRules>
<EnableRule Id="Mscrm.VisualizationPaneNotMaximized" />
<EnableRule Id="Mscrm.SaveAsCancelled.FormRule" />
</EnableRules>
<DisplayRules>
<DisplayRule Id="Mscrm.CanWriteSelected" />
<DisplayRule Id="Mscrm.SelectedEntityHasStatecode" />
</DisplayRules>
<Actions>
<JavaScriptFunction FunctionName="changeRecordStatus" Library="$webresource:new_DeactivateTask.js">
<CrmParameter Value="FirstPrimaryItemId" />
<IntParameter Value="2" />
<IntParameter Value="6" />
</JavaScriptFunction>
</Actions>
</CommandDefinition>
非常感謝您的幫助。