C#示例
示例一:多表内联
需求:根据记录的id查询给记录的历史记录中的更改人(用户名称)是否拥有管理员角色
分步查询
// 根据唯一线索id获取所有记录中的用户名称
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
<entity name="jk_assignment_log" >
<attribute name="jk_assignment_object" />
<filter type="and" >
<condition attribute="statecode" operator="eq" value="0" />
<condition attribute="jk_lead" operator="eq" value="618620BD-EF28-417E-982C-8057201B6D48" />
</filter>
</entity>
</fetch>
// 根据用户名称获取用户名称和id
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
<entity name="systemuser" >
<attribute name="systemuserid" />
<attribute name="fullname" />
<filter type="and" >
<condition attribute="fullname" operator="eq" value="优文途新管理员" />
</filter>
</entity>
</fetch>
// 查询此用户id是否存在执行角色名称
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
<entity name="role" >
<attribute name="name" />
<attribute name="businessunitid" />
<attribute name="roleid" />
<filter type="and" >
<condition attribute="name" operator="eq" value="系统管理员" />
</filter>
<link-entity name="systemuserroles" from="roleid" to="roleid" visible="false" intersect="true" >
<link-entity name="systemuser" from="systemuserid" to="systemuserid" alias="ad" >
<attribute name="systemuserid" />
<filter type="and" >
<condition attribute="systemuserid" operator="eq" value="65AD644C-64F7-E811-A81E-9A16184AF7BF" />
</filter>
</link-entity>
</link-entity>
</entity>
</fetch>
联表查询
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
<entity name="jk_assignment_log" >
<attribute name="jk_assignment_object" />
<filter type="and" >
<condition attribute="statecode" operator="eq" value="0" />
<condition attribute="jk_lead" operator="eq" value="618620BD-EF28-417E-982C-8057201B6D48" />
</filter>
<link-entity name="systemuser" from="fullname" to="jk_assignment_object">
<attribute name="systemuserid" />
<attribute name="fullname" />
<link-entity name="systemuserroles" from="systemuserid" to="systemuserid">
<link-entity name="role" from="roleid" to="roleid">
<filter type="and" >
<condition attribute="name" operator="eq" value="系统管理员" />
</filter>
</link-entity>
</link-entity>
</link-entity>
</entity>
</fetch>
示例二:事务操作
从Dynamics CRM 2015 UR1
新增了 ExecuteTransactionRequest
,主要用来处理事务操作,即一组操作;
例一
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
public static void ExecuteTransactionAction(OrganizationServiceProxy server)
{
#region 增加操作
CreateRequest add_req = new CreateRequest();
Entity add_entity = new Entity("mcs_tc_order");
add_entity["mcs_state"] = new OptionSetValue(1);
add_entity["mcs_approvalstatus"] = new OptionSetValue(1);
add_req.Target = add_entity;
#endregion
#region 修改操作
UpdateRequest up_req = new UpdateRequest();
ColumnSet attributes = new ColumnSet(new string[] { "mcs_state", "ownerid" });
Entity up_entity = server.Retrieve("mcs_tc_order", new Guid("xxx"), attributes);
up_entity.Attributes["mcs_state"] = new OptionSetValue(2);
up_req.Target = up_entity;
#endregion
#region 删除操作
DeleteRequest del_req = new DeleteRequest();
Guid id = new Guid("xxx");
del_req.Target = new EntityReference("mcs_tc_order",id);
#endregion
ExecuteTransactionRequest req = new ExecuteTransactionRequest();
req.Requests = new OrganizationRequestCollection() { add_req,up_req, del_req };
server.Execute(req);
}
例二
// 1.创建事务对象
var TranRequest = new ExecuteTransactionRequest()
{
ReturnResponses = true, // 可选
Requests = new OrganizationRequestCollection(),
};
// 2.1 更新操作,temp为更新的记录且已存在的记录
TranRequest.Requests.Add(new UpdateRequest() { Target = temp });
// 2.2 删除操作,temp为ToEntityReference类型且已存在的记录
TranRequest.Requests.Add(new DeleteRequest() { Target = temp.ToEntityReference() });
// 2.3 新建操作,temp为新记录
TranRequest.Requests.Add(new CreateRequest() { Target = temp });
// 3. 执行事务
if (TranRequest.Requests.Count() > 0) service.Execute(TranRequest);
JavaScript示例
示例一:禁用表单字段
function disableForm()
{
var controls = Xrm.Page.ui.controls.get();
for (var i in controls) {
var control = controls[i];
if (control.getControlType() != "iframe"
&& control.getControlType() != "webresource"
&& control.getControlType() != "subgrid"
&& control.getDisabled() == false)
{
control.setDisabled(true);
}
}
}
示例二:窗体保存事件
该代码需要配置OnSave
事件,并启用“将执行上下文作为第一个参数”
示例场景:通过判断是否字段未保存来防止触发
function SaveAlert(ExecutionObj) {
var project_stage = Xrm.Page.getAttribute("new_productproject_stage").getValue();
if (project_stage >= 7) {
if (Xrm.Page.getAttribute("new_internalresourcesid").getIsDirty()) {
if (confirm("生产公司简称变更将会邮件通知总经理,请点击确认/取消变更。")) {
}
else {
ExecutionObj.getEventArgs().preventDefault();//阻止保存操作
}
}
}
}