[英]How to test what data is present in d.data.results when querying CRM 2011 AppointmentSet using JavaScript and REST OData
我正在嘗試填寫我的帳戶表單上的字段,該字段顯示帳戶已完成約會的最后實際結束日期。 我的查詢似乎完全符合我的要求。 我使用Dynamics XRM工具解決方案來創建我的查詢,但是我無法處理如果例如帳戶沒有完成的活動或我創建一個根本沒有約會的新帳戶會發生什么。
我試圖在我的JavaScript的ExecuteQuery函數中測試值data.d.results返回到我的成功方法。
在新創建帳戶或帳戶沒有完成的約會活動時,visual studio中即時窗口中的data.d.results定義如下:
[]
[prototype]: []
我希望能夠測試是否發生這種情況,然后阻止嘗試將實際結束日期值設置為字段。
我的代碼如下:
/// <reference path="JQuery.js" />
/// <reference path="SDK.REST.js" />
/// <reference path="json2.js" />
// function to set read only fields on form load
function HarrionAB_AccountForm_OnLoad() {
debugger;
var accountId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");
if (accountId != "") {
RetrieveRecords(accountId);
}
}
function RetrieveRecords(id) {
// create the odata query
var query = "/AppointmentSet?$select=*&$top=1&$orderby=ActualEnd desc&$filter=RegardingObjectId/Id eq guid'" + id + "' and StateCode/Value eq 1 and ActivityTypeCode eq 'appointment'";
ExecuteQuery(query);
}
//
// ExecuteQuery executes the specified OData Query asyncronously
//
// NOTE: Requires JSON and jQuery libraries. Review this Microsoft MSDN article before
// using this script http://msdn.microsoft.com/en-us/library/gg328025.aspx
//
function ExecuteQuery(ODataQuery) {
var serverUrl = Xrm.Page.context.getServerUrl();
// Adjust URL for differences between on premise and online
if (serverUrl.match(/\/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
var ODataURL = serverUrl + "/XRMServices/2011/OrganizationData.svc" + ODataQuery;
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: ODataURL,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
//
// Handle result from successful execution
//
// e.g. data.d.results
if (data.d.results != "[]") { // I NEED TO TEST HERE
Xrm.Page.getAttribute("new_lastvisit").setValue(new Date(parseInt(data.d.results[0].ActualEnd.substr(6))));
}
},
error: function (XmlHttpRequest, textStatus, errorObject) {
//
// Handle result from unsuccessful execution
//
alert("OData Execution Error Occurred");
}
});
}
//
// Error Handler
//
function ErrorHandler(XMLHttpRequest, textStatus, errorObject)
{ alert("Error Occurred : " + textStatus + ": " + JSON.parse(XMLHttpRequest.responseText).error.message.value); }
任何幫助將非常感激
該值是一個數組,因此您可以檢查它是否包含任何值,如下所示:
if (data.d.results.length > 0) {
//Do whatever you need to in here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.