MSCRM 2011 操作大全
CRM字段类型:
货币:new Money(Decimal){SQL更新Money类型字段,需要同时更新_base字段,存在汇率差的时候值不同}
查找:new EntityReference(objecttypename,Guid)
下拉:new OptionSet(Int)
选项集:false/true
时间:DateTime
整数:Integer
十进制数:Decimal
浮点数:Double
单行/多行文本:String
CRM系统:
存储列表值:StringMap
存储附件:annotation
CRM,8个基本数据操作:
Associate 创建多对多关系
Create 创建数据
Delete 删除数据
Disassociate 删除多对多关系
Execute 执行请求
Retrieve 查询一个记录
RetrieveMultiple 查询多个记录
Update 更新数据
1.Xrm.Page.context
用户ID:getUserId()
用户角色:getUserRoles()
用户语言:getUserLcid()
组织名称:getOrgUniqueName()
组织语言:getOrgLcid()
路径参数:getQueryStringParameters()
服务器路径:getServerUrl()/getClientUrl()
前追加组织名称:prependOrgName("/WebResource/**")效果:crmtest/WebResource/**
当前主题:getCurrentTheme()
OutLook:isOutlookClient()/isOutlookOnline()
2.Xrm.Page.data.entity
所有页面元素:attributes【forEach(),get(),getLength()】
单个元素:attributes.get("")【同Xrm.Page.getAttribute()】
实体名称:getEntityName()
实体ID:getId()
是否修改:getIsDirty()
保存事件:save()
添加保存时事件:addOnSave()
移出保存时事件:removeOnSave()
3.Xrm.Page.getAttribute("")
取值:getValue()
赋值:setValue()
是否改动:getIsDirty()
表单载入时的值:getInitialValue()[Boolean, optionset]
提交类型:getSubmitMode(),setSubmitMode()//always,never,ditry
必填等级:getRequiredLevel(),setRequiredLevel()//required、none
用户操作等级:getUserPrivilege()//canCreate()、canRead()、canUpdate()
事件:addOnChange(),removeOnChange(),fireOnChange()[强制执行]
其他:getAttributeType(),getFormat(),getMax(),getMaxLength(),getMin(),getName(),getParent(),getPrecision()
optionset:getOption(),getOptions(),getSelectedOption(),getText()
4.Xrm.Page.getControl("")
属性:getAttribute()【同Xrm.Page.getAttribute()】
禁用/启用:setDisabled(true),setDisabled(false),getDisabled()
显示/隐藏:setVisible(true),setVisible(false),getVisible()
描述:setLabel(),getLabel()
聚焦选中:setFocus
刷新:refresh()//仅限SubGrid
其他:addCustomView(),addOption(),removeOption(value),clearOptions(),getControlType(),getData(),setData(),getDefaultView(),setDefaultView(),getName(),getParent(),getSrc(),setSrc(),getInitialUrl(),getObject()
5.Xrm.Page.ui
页面所有控件:controls【forEach(),get(),getLength()】
单个控件:controls.get("")【同Xrm.Page.getControl()】
页面Tab:tabs【forEach(),get(),getLength()】
单个Tab:tabs.get()【sections,getDisplayState(),getLabel(),getName(),getParent(),getVisible(),setDisplayState(),setFocus(),setLabel(),setVisible()】
Tab-Sections:tabs.get(0).sections【forEach(),get(),getLength()】
单个Section:tabs.get(0).sections.get(0)【getLabel(),getName(),getParent(),getVisible(),setLabel(),setVisible(),controls】
页面导航:navigation.items【forEach(),get(),getLength()】
单个导航:navigation.items.get()【getId(),getLabel(),getVisible(),setFocus(),setLabel(),setVisible()】
窗体:formSelector.items【forEach(),get(),getLenght()】
单个窗体:formSelector.items.get()【getId(),getLabel(),navigate()-窗体跳跃】
6.窗体状态
var FORM_TYPE_CREATE = 1;
var FORM_TYPE_UPDATE = 2;
var FORM_TYPE_READ_ONLY = 3;
var FORM_TYPE_DISABLED = 4;
var FORM_TYPE_QUICK_CREATE = 5;
var FORM_TYPE_BULK_EDIT = 6;
var formType = Xrm.Page.ui.getFormType();
刷新当前页面:
window.location.href = window.location.href;
window.location.reload();
禁用页面所有控件:
Xrm.Page.ui.controls.forEach(function (control, index) {
control.setDisabled(true);
});
防止保存事件(需要执行上下文):
function My_PreventSaveFunction(eContext) {
eContext.getEventArgs().preventDefault();
}
给Lookup取值赋值:
id:obj.getValue()[0].id;
name:obj.getValue()[0].name;
var objList = new Array();
var obj = new Object();
obj.id = accountObj[0].TerritoryId.Id;
obj.name = accountObj[0].TerritoryId.Name;
obj.typename = accountObj[0].TerritoryId.LogicalName;
objList[0] = obj;
Xrm.Page.getAttribute("yto_territory").setValue(accountObj[0].TerritoryId.Id == null ? null : objList);
Xrm.Page.getAttribute("yto_territory").setSubmitMode("always");
Xrm.Page.data.entity.save();
给OptionSet按条件显示列:
Xrm.Page.getControl("new_counter_type").clearOptions();
var options = Xrm.Page.getAttribute("new_counter_type").getOptions();
for (var i = 0; i < options.length; i++) {
if (options[i].value != 190 && options[i].value != 200 && options[i].value!="null" ) {
Xrm.Page.getControl("new_counter_type").addOption(options[i], i+1);
}
}
Xrm.Page.getControl("new_counter_type").addOption(Xrm.Page.getAttribute("new_counter_type").getOption(190),1);
JS,给日期类型控件赋值:
var fullYear = Xrm.Page.getAttribute("yto_confirmdate").getValue().getFullYear();
var month = Xrm.Page.getAttribute("yto_confirmdate").getValue().getMonth() + 1;
var date = Xrm.Page.getAttribute("yto_confirmdate").getValue().getDate();
Xrm.Page.getAttribute("yto_confirmdate").setValue(new Date(fullYear, month, date))//厂方确认交货日期+1月
Xrm.Page.data.entity.save();
7.给Lookup添加过滤:
var fetch = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true"><entity name="new_expensesdetail"><attribute name="new_expensesdetailid"/><attribute name="new_name"/><attribute name="createdon"/><order attribute="new_name" descending="false"/><filter type="and"><condition attribute="statecode" operator="eq" value="0"/></filter><link-entity name="new_buget" from="new_expenseitem" to="new_expensesdetailid" alias="aa"><filter type="and"><condition attribute="new_bugetunit" operator="eq" uitype="businessunit" value="' + Xrm.Page.getAttribute("new_businessunit").getValue()[0].id + '"/><condition attribute="new_bedgetsheet" operator="eq" uitype="new_budgetcycle" value="' + Xrm.Page.getAttribute("new_feeperiod").getValue()[0].id + '"/></filter></link-entity></entity></fetch>';
var view = '<grid name="resultset" object="10069" jump="new_name" select="1" icon="1" preview="1"><row name="result"
";
} else {
msg += "单据[" + objs[0].yto_name + "]审批成功" + "/n";
}
});
6.添加了审批流的实体,在状态变更的时候获取的上下文登陆人是system,根据顶级上下文获取当前真实登陆人:
/// <summary>
/// 获取最顶级插件上下文对象
/// </summary>
/// <param name="currentContext">当前插件上下文对象</param>
/// <returns>返回最顶级插件上下文对象</returns>
public IPluginExecutionContext GetTopContext(IPluginExecutionContext currentContext)
{
IPluginExecutionContext topContext = currentContext;
for (int i = 0; i <= currentContext.Depth; i++)
{
if (topContext.ParentContext!=null)
{
topContext = topContext.ParentContext;
}
}
return topContext;
}
8.插件注册管道
验证前: 数据补充和验证
操作前: 核心操作前对其他数据进行变更、创建、删除(失败可回滚)
操作后: 验证数据执行情况,执行核心操作后的后继工作
9.JS返回访问平台
navigator.platform
navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)
10.JS返回日期间隔
function dateDiff(interval, date1, date2)
{
var objInterval = {'D' : 1000 * 60 * 60 * 24, 'H' : 1000 * 60 * 60, 'M' : 1000 * 60, 'S' : 1000, 'T' : 1};
interval = interval.toUpperCase();
var dt1 = Date.parse(date1.replace(/-/g, '/'));
var dt2 = Date.parse(date2.replace(/-/g, '/'));
try
{
return Math.round((dt2 - dt1) / eval('(objInterval.' + interval + ')'));
}
catch (e)
{
return e.message;
}
}
说明:interval 取值: d (day), m(minutes), s(second), t(毫秒),不分区大小写
日期格式: yyyy (/-) (m)m (/-) (d)d
11.超过6个subgrid不会自动显示数据(onload,参数100)
//刷新MS CRM表单中未自动加载的SubGrid
var refreshSubGridId;
function loadSubGrids(milliSeconds) {
if (!milliSeconds) milliSeconds = 1000;
refreshSubGridId = setInterval(refreshSubGrids, milliSeconds);
}
function refreshSubGrids() {
var blnSuccess = false;
try {
var subgrids = Xrm.Page.ui.controls.get(function (control, index) { return control.getControlType() == "subgrid"; });
if (subgrids.length > 4) for (var i = 4; i < subgrids.length; i++) subgrids[i].refresh();
blnSuccess = true;
} catch (e) { }
if (blnSuccess) clearInterval(refreshSubGridId);
}
12.禁用SubGrid
function disableSubGrid(subGridName) {
try {
var subGrid = document.getElementById(subGridName + "_span");
if (subGrid) subGrid.disabled = true;
} catch (e) { }
}
13.创建服务
CrmConnection connection = CrmConnection.Parse(CRMConnectionPath);
using (CrmOrganizationServiceContext orgservice = new CrmOrganizationServiceContext(connection))
{
AutoShut(orgservice);
}
14.让报表取到当前表单的ID:
DS1:
select new_promotionid from filterednew_promotion as crmaf_filterednew_promotion
DS2:
select new_name from filterednew_promotion where new_promotionid=@proid
对DS2生成的参数@proid-属性-默认值-DS1
让报表取到当前用户ID:
SELECT [dbo].[fn_FindUserGuid]()
select systemuserid,fullname from FilteredSystemUser where systemuserid = dbo.fn_FindUserGuid()
让报表取到当前用户ID:
select businessunitid,name from businessunit where businessunitid = dbo.fn_FindBusinessGuid()
15.执行工作流
ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
{
WorkflowId = new Guid("C4745DA5-CA92-4E37-9A63-0F3BC73ECB06"),
EntityId = new Guid("C68B84F8-D397-E311-93FD-002481E8832E")
};
ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)testService.OrganizationService.Execute(request);
16.多对对,N对N,添加关系:
EntityReferenceCollection coll=new EntityReferenceCollection();
coll.Add(new EntityReference("systemuser",new Guid("1DCCAD1E-9F67-E311-93EF-002481E88330")));
testService.Associate("team", new Guid("99C72A6E-EA71-E311-93FD-002481E8832E"), new Relationship("teammembership_association"), coll);
已存在关系,会报错。
引用:http://blog.****.net/jxian2009/article/details/22179447