问题
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.
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 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.
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
回答1:
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
}
来源:https://stackoverflow.com/questions/19729936/how-to-test-what-data-is-present-in-d-data-results-when-querying-crm-2011-appoin