简体   繁体   English

使用JavaScript和REST OData查询CRM 2011 AppointmentSet时如何测试d.data.results中存在哪些数据

[英]How to test what data is present in d.data.results when querying CRM 2011 AppointmentSet using JavaScript and REST OData

I am trying to populate a field on my Account form that shows the last Actual End date of the accounts completed appointments. 我正在尝试填写我的帐户表单上的字段,该字段显示帐户已完成约会的最后实际结束日期。 My Query seems to work exactly as I want. 我的查询似乎完全符合我的要求。 I used the Dynamics XRM Tools solution to create my query, but I am unable to handle what happens if for instance the account has no completed activities or I create a new account that has no appointments at all. 我使用Dynamics XRM工具解决方案来创建我的查询,但是我无法处理如果例如帐户没有完成的活动或我创建一个根本没有约会的新帐户会发生什么。

I am trying to test the value data.d.results returned to my success Method within the ExecuteQuery function of my JavaScript. 我试图在我的JavaScript的ExecuteQuery函数中测试值data.d.results返回到我的成功方法。

data.d.results in the immediate window in visual studio is defined as follows when the account is newly created or there are no completed appointment activities for the account: 在新创建帐户或帐户没有完成的约会活动时,visual studio中即时窗口中的data.d.results定义如下:

[]

    [prototype]: []

I want to be able to test if this situation occurs and then prevent the attempt to set the actual end date value to the field. 我希望能够测试是否发生这种情况,然后阻止尝试将实际结束日期值设置为字段。

My code follows: 我的代码如下:

/// <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); }

Any help would be appreciated greatly 任何帮助将非常感激

The value is an array so you can check to see if it contains any values as follows: 该值是一个数组,因此您可以检查它是否包含任何值,如下所示:

if (data.d.results.length > 0) {
    //Do whatever you need to in here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM