使用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

查看:72
本文介绍了使用JavaScript和REST OData查询CRM 2011 AppointmentSet时,如何测试d.data.results中存在哪些数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在帐户"表单上填充一个字段,该字段显示帐户已完成约会的最后实际结束日期.我的查询似乎完全按照我的要求工作.我使用了 Dynamics XRM Tools 解决方案来创建查询,但是如果例如该帐户没有完成的活动,或者我创建了一个完全没有约会的新帐户.

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.

我正在尝试测试值data.d.results返回到我的JavaScript的ExecuteQuery函数中的成功方法.

I am trying to test the value data.d.results returned to my success Method within the ExecuteQuery function of my JavaScript.

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:

[]

    [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.

我的代码如下:

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

任何帮助将不胜感激

推荐答案

该值是一个数组,因此您可以检查它是否包含以下任何值:

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
}

这篇关于使用JavaScript和REST OData查询CRM 2011 AppointmentSet时,如何测试d.data.results中存在哪些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆