我希望能夠計算並顯示聯繫表上的聯繫人約會的最新實際結束日期。計算特定聯繫CRM 2011聯機JavaScript的最後完成約會
以下查詢顯示我如何實現針對帳戶實體的相同功能。
現在我試圖做的聯繫人相同,但regardingobjectid不匹配反對任何接觸。所以我問我該如何解決這個問題。
是否需要打開並展開組織者,可選參加者和所需參加者的每個部件列表?如果是的話我將如何去創建這個查詢?使用$ expand功能?
請幫助一個困擾的心靈!
凹凸!
我希望能夠計算並顯示聯繫表上的聯繫人約會的最新實際結束日期。計算特定聯繫CRM 2011聯機JavaScript的最後完成約會
以下查詢顯示我如何實現針對帳戶實體的相同功能。
現在我試圖做的聯繫人相同,但regardingobjectid不匹配反對任何接觸。所以我問我該如何解決這個問題。
是否需要打開並展開組織者,可選參加者和所需參加者的每個部件列表?如果是的話我將如何去創建這個查詢?使用$ expand功能?
請幫助一個困擾的心靈!
凹凸!
好,所以我一直在這個問題上工作了幾天。
我發現可以在以下工具XRM Dynamics Tools的幫助下完成此任務,以幫助生成我的OData查詢代碼。
基本上我遇到的問題是理解聯繫人如何鏈接到約會。一旦我瞭解到約會的所有參與者都將包含在約會的「appointment_activity_parties」字段中,我就能夠看到如何創建合適的查詢來處理此問題。
通過選擇activityid,actualEnd字段並在appointment_activity_parties上使用展開功能,從此擴展字段中選擇PartyId使我能夠檢查該聚會類型是否爲聯繫人,該聚會的contactId與當前聯繫人匹配我們正在觀看。這樣我就可以計算匹配的總數並記錄該聯繫人已完成約會的最近日期。
最後我還將問題分解爲2個查詢。每年一次:當前和以前。我在聯繫人表單中添加了三個新字段。二是保持VisitsLastYear和VisitsThisYear和查找整數持有環節爲可以在下面的截圖中可以看到的約會:
我的代碼如下:
/// <reference path="XrmPageTemplate.js" />
/// <reference path="JQuery.js" />
/// <reference path="SDK.REST.js" />
/// <reference path="json2.js" />
function HarrionAB_ContactForm_OnLoad() {
// get the contact id from the page
var contactId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "")
// if we got a value
if (contactId != "") {
var currentYear = new Date().getFullYear();
var query = "/AppointmentSet?"; // Appointments table
query += "$select=ActualEnd,ActivityId,appointment_activity_parties/PartyId"; // Select
query += "&$expand=appointment_activity_parties"; // Expand sub tables
query += "&$filter=ActivityTypeCode eq 'appointment' and StateCode/Value eq 1 and "; // Where
CountVisitsThisYear(query, currentYear);
CountVisitsLastYear(query, currentYear - 1);
}
}
function CountVisitsThisYear(query, currentYear) {
var start = currentYear.toString() + "-01-01T00:00:00";
var end = currentYear.toString() + "-12-31T00:00:00";
query += "ActualStart ge datetime'" + start + "' or ActualStart le datetime'" + start + "' and "; // Where
query += "ActualEnd ge datetime'" + end + "' or ActualEnd le datetime'" + end + "'"; // Where
// call function to execute the odata query
ExecuteVisitsThisYearQuery(query);
}
function CountVisitsLastYear(query, lastYear) {
var start = lastYear.toString() + "-01-01T00:00:00";
var end = lastYear.toString() + "-12-31T00:00:00";
query += "ActualStart ge datetime'" + start + "' or ActualStart le datetime'" + start + "' and "; // Where
query += "ActualEnd ge datetime'" + end + "' or ActualEnd le datetime'" + end + "'"; // Where
// call function to execute the odata query
ExecuteVisitsLastYearQuery(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 ExecuteVisitsThisYearQuery(ODataQuery) {
// get the server url
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
var contactId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");
var lastVisitDate;
var activityId;
var count = 0;
// if we have results
if (data.d.results.length > 0) {
// loop through the appointment results
for (i = 0; i < data.d.results.length; i++) {
// if we have results
if (data.d.results[i].appointment_activity_parties.results.length > 0) {
// loop through the appointment_activity_parties
for (j = 0; j < data.d.results[i].appointment_activity_parties.results.length; j++) {
// if the party id type is contact and the contact ids match
if (data.d.results[i].appointment_activity_parties.results[j].PartyId.LogicalName == "contact" && contactId.toLowerCase() == data.d.results[i].appointment_activity_parties.results[j].PartyId.Id.toLowerCase()) {
// if we have not got a date yet
if (lastVisitDate == null) {
// set the date as this is the first date we found
lastVisitDate = data.d.results[i].ActualEnd;
activityId = data.d.results[i].ActivityId;
} else {
// if the current date is < new date
if (lastVisitDate < data.d.results[i].ActualEnd) {
// reset the date as we have found a later one
lastVisitDate = data.d.results[i].ActualEnd;
activityId = data.d.results[i].ActivityId;
}
}
++count;
}
}
}
}
}
Xrm.Page.getAttribute("new_visitsthisyear").setValue(count);
// if we found a completed appointment
if (count > 0) {
SetLookup("new_lastvisitcompleted", activityId, ParseJsonDate(lastVisitDate).toString('dd/MM/yyyy'), "Appointment");
}
},
error: function (XmlHttpRequest, textStatus, errorObject) {
//
// Handle result from unsuccessful execution
//
alert("OData Execution Error Occurred");
}
});
}
//
// 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 ExecuteVisitsLastYearQuery(ODataQuery) {
// get the server url
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
var contactId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");
var count = 0;
// if we have results
if (data.d.results.length > 0) {
// loop through the appointment results
for (i = 0; i < data.d.results.length; i++) {
// if we have results
if (data.d.results[i].appointment_activity_parties.results.length > 0) {
// loop through the appointment_activity_parties
for (j = 0; j < data.d.results[i].appointment_activity_parties.results.length; j++) {
// if the party id type is contact and the contact ids match
if (data.d.results[i].appointment_activity_parties.results[j].PartyId.LogicalName == "contact" && contactId.toLowerCase() == data.d.results[i].appointment_activity_parties.results[j].PartyId.Id.toLowerCase()) {
++count;
}
}
}
}
}
Xrm.Page.getAttribute("new_visitslastyear").setValue(count);
},
error: function (XmlHttpRequest, textStatus, errorObject) {
//
// Handle result from unsuccessful execution
//
alert("OData Execution Error Occurred");
}
});
}
// function to parse JSON date into JavaScript Date
function ParseJsonDate(jsonDate) {
var offset = new Date().getTimezoneOffset() * 60000;
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined)
parts[2] = 0;
if (parts[3] == undefined)
parts[3] = 0;
return new Date(+parts[1] + offset + parts[2] * 3600000 + parts[3] * 60000);
};
//function to create a lookup control
function SetLookup(fieldName, idValue, textValue, typeValue) {
var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].typename = typeValue;
Xrm.Page.getAttribute(fieldName).setValue(value);
}
//
// Error Handler
//
function ErrorHandler(XMLHttpRequest, textStatus, errorObject)
{ alert("Error Occurred : " + textStatus + ": " + JSON.parse(XMLHttpRequest.responseText).error.message.value); }
希望這幫助任何有類似問題的人。