计算特定联系人CRM 2011 Online JavaScript的上次完成约会
我希望能够计算并在我的联系人表单上显示该联系人约会的最新实际结束日期 下面的查询显示了我如何针对Account实体实现相同的功能 现在,我已经尝试在联系人上执行相同的操作,但regardingobjectid与任何联系人都不匹配。所以我在问我该怎么做 我是否需要打开并展开组织者、可选与会者和所需与会者的每个partylist?如果是这样的话,我将如何着手为此创建查询?使用$expand功能 请帮助一个烦恼的人计算特定联系人CRM 2011 Online JavaScript的上次完成约会,javascript,rest,dynamics-crm-2011,odata,appointment,Javascript,Rest,Dynamics Crm 2011,Odata,Appointment,我希望能够计算并在我的联系人表单上显示该联系人约会的最新实际结束日期 下面的查询显示了我如何针对Account实体实现相同的功能 现在,我已经尝试在联系人上执行相同的操作,但regardingobjectid与任何联系人都不匹配。所以我在问我该怎么做 我是否需要打开并展开组织者、可选与会者和所需与会者的每个partylist?如果是这样的话,我将如何着手为此创建查询?使用$expand功能 请帮助一个烦恼的人 碰撞 好的,我已经研究这个问题好几天了 我发现在以下工具的帮助下可以完成这项任务,以
碰撞 好的,我已经研究这个问题好几天了 我发现在以下工具的帮助下可以完成这项任务,以帮助生成我的OData查询代码 基本上,我遇到的问题是理解联系人如何与约会联系在一起。一旦我了解到约会的所有参与者都将包含在约会的“约会\活动\参与方”字段中,我就能够了解如何创建适当的查询来处理此问题 通过选择activityid、ActualLend字段并使用约会\活动\双方的展开功能,从该展开字段中选择PartyId使我能够检查该方类型是否为联系人,该方的contactId是否与我们正在查看的当前联系人相匹配。这样,我可以计算匹配的总数,并记录该联系人完成约会的最近日期 最后,我还将问题分解为两个查询。每年一次:当前和以前。我在联系人表单中添加了三个新字段。两个包含VisitsLastYear和VisitsThistYear的整数,以及一个包含约会链接的查找,如以下屏幕截图所示: 我的代码如下:
/// <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); }
//
///
///
///
函数HarrionAB_ContactForm_OnLoad(){
//从页面获取联系人id
var contactId=Xrm.Page.data.entity.getId().replace(“{”,“”)。replace(“}”,“”)
//如果我们得到一个值
如果(联系人ID!=“”){
var currentYear=新日期().getFullYear();
var query=“/AppointmentSet?”;//约会表
query+=“$select=ActualEnd,ActivityId,约会\活动\双方/PartyId”;//选择
query+=“&$expand=appointment\u activity\u parties”;//展开子表
query+=“&$filter=ActivityTypeCode eq‘约会’和StateCode/值eq 1和”//其中
CountVisitsThisYear(查询,当前年份);
CountVisitsLastYear(查询,当前年份-1);
}
}
函数CountVisitsThisYear(查询,当前年份){
var start=currentYear.toString()+“-01-01T00:00:00”;
var end=currentYear.toString()+“-12-31T00:00:00”;
query+=“ActualStart ge datetime”“+start+”'或ActualStart le datetime”“+start+”'和“;//其中
query+=“实际日期时间”“+end+””或实际日期时间”“+end+”;//其中
//调用函数以执行odata查询
ExecuteVisitsThisYearQuery(查询);
}
函数CountVisitsLastYear(查询,lastYear){
var start=lastYear.toString()+“-01-01T00:00:00”;
var end=lastYear.toString()+“-12-31T00:00:00”;
query+=“ActualStart ge datetime”“+start+”'或ActualStart le datetime”“+start+”'和“;//其中
query+=“实际日期时间”“+end+””或实际日期时间”“+end+”;//其中
//调用函数以执行odata查询
ExecuteVisitsLastYearQuery(查询);
}
//
//ExecuteQuery异步执行指定的OData查询
//
//注意:需要JSON和jQuery库。请先查看此Microsoft MSDN文章
//使用此脚本http://msdn.microsoft.com/en-us/library/gg328025.aspx
//
函数ExecuteVisitsThisYearQuery(ODataQuery){
//获取服务器url
var serverUrl=Xrm.Page.context.getServerUrl();
//调整本地和在线URL之间的差异
if(serverUrl.match(/\/$/)){
serverUrl=serverUrl.substring(0,serverUrl.length-1);
}
var ODataURL=serverUrl+“/XRMServices/2011/OrganizationData.svc”+ODataQuery;
$.ajax({
键入:“获取”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
网址:Odataul,
beforeSend:函数(XMLHttpRequest){
setRequestHeader(“接受”、“应用程序/json”);
},
成功:函数(数据、textStatus、XmlHttpRequest){
//
//处理成功执行的结果
//
//例如,数据和结果
var contactId=Xrm.Page.data.entity.getId().replace(“{”,“”)。replace(“}”,“”);
var lastVisitDate;
var-activityId;
var计数=0;
//如果我们有结果
如果(data.d.results.length>0){
//循环查看约会结果
对于(i=0;i0){
//循环浏览约会\活动\聚会
对于(j=0;j