Dynamic CRM 2013学习笔记(四十六)简单审批流的实现
前面介绍过自定义审批流:
Dynamic CRM 2013学习笔记(十九)自定义审批流1 - 效果演示
Dynamic CRM 2013学习笔记(二十一)自定义审批流2 - 配置按钮
Dynamic CRM 2013学习笔记(三十二)自定义审批流3 - 节点及实体配置
Dynamic CRM 2013学习笔记(三十三)自定义审批流4 - 规则节点 -有分支的流程处理
Dynamic CRM 2013学习笔记(三十四)自定义审批流5 - 自动邮件通知
虽然灵活,功能强大,不用修改代码,完全用配置就可以搞定。
最近客户需要实现一个很简单的审批流,就有了这篇简单审批流的实现。
首先根据需求文档分析流程:
功能实现如下:
一、 添加字段
一个是让用户修改的审批状态,如果当前用户是审批人,放开审批状态字段,让用户审批或拒绝;值为 Pending, Approved, Rejected
一个是实体的状态,用户审批后根据业务逻辑来判断实体的状态;值为 Pending, Approved, Rejected
一个审批人字段,提交或用户修改了审批状态后,设置下一个审批人
另外可以设置几个隐藏审批人的字段,方便在插件里一次得出,js里就方便设置了(尽量避免在js里运行大量逻辑),如上图主要是把 sales director, gm 存下来
二. JS控制提交按钮和审批状态字段
1. 提交控制
AllowSubmit: function () {var currentUserId = Xrm.Page.context.getUserId();var userHasRole = HP.QuoteRibbon.UserHasRole(["Reginal Sales"], currentUserId);var Approver = Xrm.Page.getAttribute("tm_approver").getValue();var AccountTypeValue = Xrm.Page.getAttribute("wf_accounttype").getValue();var QuoteTypeValue = Xrm.Page.getAttribute("tm_quote_type").getValue();var TotalAmount = Xrm.Page.getAttribute("tm_total_amount").getValue() / 1000;var AccountType = {};AccountType.House = 3;var QuoteType = {};QuoteType.Standard = 1;QuoteType.Custom = 2;if (userHasRole && Approver == null) {if (TotalAmount < 100 && QuoteTypeValue == QuoteType.Custom && AccountTypeValue != AccountType.House) {return false;}return true;}else {return false;}},Submit: function () {var AccountTypeValue = Xrm.Page.getAttribute("wf_accounttype").getValue();var QuoteTypeValue = Xrm.Page.getAttribute("tm_quote_type").getValue();var TotalAmount = Xrm.Page.getAttribute("tm_total_amount").getValue() / 1000;var ApproverAttribute = Xrm.Page.getAttribute("tm_approver");var Approver = ApproverAttribute.getValue();var QuoteStatusAttribute = Xrm.Page.getAttribute("tm_quote_status");var ApprovalResultAttribute = Xrm.Page.getAttribute("tm_approval_result");var CentralizedValue = Xrm.Page.getAttribute("tm_centralized").getValue();var SubmitAttribute = Xrm.Page.getAttribute("tm_submit");SubmitAttribute.setSubmitMode("always");QuoteStatusAttribute.setSubmitMode("always");ApprovalResultAttribute.setSubmitMode("always");var AccountType = {};AccountType.House = 3;var QuoteType = {};QuoteType.Standard = 1;QuoteType.Custom = 2;var QuoteStatus = {};QuoteStatus.Pending = 1;QuoteStatus.Approved = 2;QuoteStatus.Rejected = 3;var ApprovalStatus = {};ApprovalStatus.Pending = 1;ApprovalStatus.Approved = 2;ApprovalStatus.Rejected = 3;if (QuoteTypeValue == QuoteType.Standard) {if (TotalAmount <= 500) {QuoteStatusAttribute.setValue(QuoteStatus.Approved);ApprovalResultAttribute.setValue(ApprovalStatus.Approved);}else {//var sd = HP.QuoteRibbon.GetSalesDirector();//var user = [{ "id": sd.guid, "name": sd.name, "entityType": sd.logicalName }];//ApproverAttribute.setValue(user);QuoteStatusAttribute.setValue(QuoteStatus.Pending);ApprovalResultAttribute.setValue(ApprovalStatus.Pending);}}else {if (AccountTypeValue == AccountType.House) {if (TotalAmount < 500) {ApproverAttribute.setValue(CentralizedValue);QuoteStatusAttribute.setValue(QuoteStatus.Pending);ApprovalResultAttribute.setValue(ApprovalStatus.Pending);}else if (TotalAmount >= 500) {//var sd = HP.QuoteRibbon.GetSalesDirector();//var user = [{ "id": sd.guid, "name": sd.name, "entityType": sd.logicalName }];//ApproverAttribute.setValue(user);QuoteStatusAttribute.setValue(QuoteStatus.Pending);ApprovalResultAttribute.setValue(ApprovalStatus.Pending);}}else {if (TotalAmount >= 100 && TotalAmount < 500) {ApproverAttribute.setValue(CentralizedValue);QuoteStatusAttribute.setValue(QuoteStatus.Pending);ApprovalResultAttribute.setValue(ApprovalStatus.Pending);}else if (TotalAmount >= 500) {//var sd = HP.QuoteRibbon.GetSalesDirector();//var user = [{ "id": sd.guid, "name": sd.name, "entityType": sd.logicalName }];//ApproverAttribute.setValue(user);QuoteStatusAttribute.setValue(QuoteStatus.Pending);ApprovalResultAttribute.setValue(ApprovalStatus.Pending);}}}SubmitAttribute.setValue("Submit_" + new Date());Xrm.Page.data.entity.save();},
2. 审批状态修改事件
ApprovalResultChanged: function () {var ApprovalStatus = {};ApprovalStatus.Pending = 1;ApprovalStatus.Approved = 2;ApprovalStatus.Rejected = 3;var QuoteType = {};QuoteType.Standard = 1;QuoteType.Custom = 2;var QuoteStatus = {};QuoteStatus.Pending = 1;QuoteStatus.Approved = 2;QuoteStatus.Rejected = 3;var ApproverAttribute = Xrm.Page.getAttribute("tm_approver");var CentralizedValue = Xrm.Page.getAttribute("tm_centralized").getValue();var approvalResultValue = Xrm.Page.getAttribute("tm_approval_result").getValue();var QuoteStatusAttribute = Xrm.Page.getAttribute("tm_quote_status");var ApprovalResultAttribute = Xrm.Page.getAttribute("tm_approval_result");var GMValue = Xrm.Page.getAttribute("tm_gmid").getValue();var AccountTypeValue = Xrm.Page.getAttribute("wf_accounttype").getValue();var QuoteTypeValue = Xrm.Page.getAttribute("tm_quote_type").getValue();var TotalAmount = Xrm.Page.getAttribute("tm_total_amount").getValue() / 1000;QuoteStatusAttribute.setSubmitMode("always");ApproverAttribute.setSubmitMode("always");if (approvalResultValue == ApprovalStatus.Approved) {if (QuoteTypeValue == QuoteType.Standard) {if (TotalAmount >= 500 && TotalAmount < 1000) {QuoteStatusAttribute.setValue(QuoteStatus.Approved);//ApprovalResultAttribute.setValue(ApprovalStatus.Approved);}else if (TotalAmount >= 1000) {if (HP.QuoteForm.UserHasRole(["General Manager"]) == true) {QuoteStatusAttribute.setValue(QuoteStatus.Approved);//ApprovalResultAttribute.setValue(ApprovalStatus.Approved);}else {if (CentralizedValue && HP.QuoteForm.GuidsisEqual(Xrm.Page.context.getUserId(), CentralizedValue[0].id)) {HP.QuoteForm.SetLookupValue(ApproverAttribute, GMValue);}else {HP.QuoteForm.SetLookupValue(ApproverAttribute, CentralizedValue);}QuoteStatusAttribute.setValue(QuoteStatus.Pending);ApprovalResultAttribute.setValue(ApprovalStatus.Pending);}}}else {if (TotalAmount < 1000) {QuoteStatusAttribute.setValue(QuoteStatus.Approved);//ApprovalResultAttribute.setValue(ApprovalStatus.Approved);}else if (TotalAmount >= 1000) {if (CentralizedValue && HP.QuoteForm.GuidsisEqual(Xrm.Page.context.getUserId(), CentralizedValue[0].id)) {QuoteStatusAttribute.setValue(QuoteStatus.Approved);//ApprovalResultAttribute.setValue(ApprovalStatus.Approved);}else {if (HP.QuoteForm.UserHasRole(["General Manager"]) == true) {HP.QuoteForm.SetLookupValue(ApproverAttribute, CentralizedValue);}else {HP.QuoteForm.SetLookupValue(ApproverAttribute, GMValue);}QuoteStatusAttribute.setValue(QuoteStatus.Pending);ApprovalResultAttribute.setValue(ApprovalStatus.Pending);}}}}else if (approvalResultValue == ApprovalStatus.Rejected) {ApproverAttribute.setValue(null);QuoteStatusAttribute.setValue(QuoteStatus.Rejected);//ApprovalResultAttribute.setValue(ApprovalStatus.Rejected);}},
三、插件设置审批人
为了在js里快速方便地设置下一个审批人,可以通过插件在后台来设置。当提交后,触发插件把下几个审批人放到隐藏字段里以供js快速赋值
if (entity.Contains("tm_submit"))
{
Entity preImageEntity = (Entity)context.PreEntityImages["PreImage"];
if (preImageEntity.Contains("tm_approval_result") && ((OptionSetValue)preImageEntity["tm_approval_result"]).Value == 2)
return;
CRMConfigHelper.CRMConfig crmConfig = CRMConfigHelper.InitialCRMConfig(context.OrganizationName);
string sql = string.Format(@"select top 1 op.new_product_line
from Quote q join Opportunity o on q.OpportunityId = o.OpportunityId
join wf_opportunityproduct op on o.OpportunityId = op.wf_OpportunityId
where op.wf_IncludeinForecast = 1 and q.QuoteId = '{0}'", entity.Id);
var dtPL = SqlHelper.SQLExecuteQuery(crmConfig.CRMSqlConnStr, sql).Tables[0];
if (dtPL.Rows.Count == 1 && dtPL.Rows[0]["new_product_line"] != null)
{
string productLine = dtPL.Rows[0]["new_product_line"].ToString();
sql = string.Format(@"select top 1 bu.new_nlt_sales_director 'NLTSD', bu.new_tm_sales_director 'TMSD'
from BusinessUnit bu
join SystemUser u on bu.BusinessUnitId = u.BusinessUnitId
where u.SystemUserId = '{0}'", context.UserId);
var dtSD = SqlHelper.SQLExecuteQuery(crmConfig.CRMSqlConnStr, sql).Tables[0];
if (dtSD.Rows.Count == 1)
{
if (productLine == "1") //TM
{
entity["tm_sales_directorid"] = new EntityReference("systemuser", Guid.Parse(dtSD.Rows[0]["TMSD"].ToString()));
}
else if (productLine == "2") //NLT
{
entity["tm_sales_directorid"] = new EntityReference("systemuser", Guid.Parse(dtSD.Rows[0]["NLTSD"].ToString()));
}
//if (preImageEntity["tm_approver"] == null )
{
entity["tm_approver"] = entity["tm_sales_directorid"];
}
}
}
//if (preImageEntity.Contains("tm_gmid") && preImageEntity["tm_gmid"] != null)
// return;
sql = @"select top 1 u.SystemUserId 'GM'
from SystemUser u
join SystemUserRoles ur on u.SystemUserId = ur.SystemUserId
join role r on r.RoleId = ur.RoleId
where r.Name = 'General Manager'";
var dtGM = SqlHelper.SQLExecuteQuery(crmConfig.CRMSqlConnStr, sql).Tables[0];
if (dtGM.Rows.Count == 1 && dtGM.Rows[0]["GM"] != null)
{
entity["tm_gmid"] = new EntityReference("systemuser", Guid.Parse(dtGM.Rows[0]["GM"].ToString()));
}
}
四、公用JS方法
1. 设置Lookup值
SetLookupValue: function (LookupAttribute, LookupValue) {
var lookupReference = [];
lookupReference[0] = {};
lookupReference[0].id = LookupValue[0].id;
lookupReference[0].entityType = LookupValue[0].entityType;
lookupReference[0].name = LookupValue[0].name;
LookupAttribute.setValue(lookupReference);
},
2. OData查询
ODataRetrieve: function (oDataString) {
var retrieveReq = new XMLHttpRequest();
retrieveReq.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/" + oDataString), false);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
retrieveReq.send();
return JSON.parse(retrieveReq.responseText).d;
},
3. 当前用户是否有指定的角色
UserHasRole: function (roleNames) {
var ODataResult = HP.QuoteForm.ODataRetrieve("SystemUserSet?$select=systemuserroles_association/Name&$expand=systemuserroles_association&$filter=SystemUserId eq guid'" + Xrm.Page.context.getUserId() + "'");
if (ODataResult != null && ODataResult.results.length == 1 && ODataResult.results[0].systemuserroles_association.results.length > 0) {
var oDataRoles = ODataResult.results[0].systemuserroles_association.results;
for (var i = 0; i < oDataRoles.length; i++) {
for (var j = 0; j < roleNames.length; j++) {
if (oDataRoles[i].Name == roleNames[j]) {
return true;
}
}
}
}
return false;
},
4. 判断两个GUID是相同
GuidsisEqual: function (guid1, guid2) {
var isEqual = false;
if (guid1 != null && guid2 != null) {
isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
}
return isEqual;
},
Dynamic CRM 2013学习笔记(四十六)简单审批流的实现的更多相关文章
- Dynamic CRM 2013学习笔记(十六)用JS控制Tab可见,可用
一个Form里经常会有好几个Tab,有时要根据一些条件设置哪些Tab可用,可见.下面就介绍下如何用JS对Tab进行控制. 1. 控制可见 function setTabVisableByName( ...
- Dynamic CRM 2013学习笔记(十四)复制/克隆记录
经常有这样的需求,一个单据上有太多要填写的内容,有时还关联多个子单据,客户不想一个一个地填写,他们想从已有的单据上复制数据,克隆成一条新的记录.本文将介绍如何克隆一条记录,包括它的子单据以生成一条新的 ...
- Dynamic CRM 2013学习笔记(十二)实现子表合计(汇总,求和)功能的通用插件
上一篇 Dynamic CRM 2013学习笔记(十一)利用Javascript实现子表合计(汇总,求和)功能 , 介绍了如何用js来实现子表合计功能,这种方法要求在各个表单上添加js方法,如果有很多 ...
- Dynamic CRM 2013学习笔记(十)客户端几种查询数据方式比较
我们经常要在客户端进行数据查询,下面分别比较常用的几种查询方式:XMLHttpRequest, SDK.JQuery, SDK.Rest. XMLHttpRequest是最基本的调用方式,JQuery ...
- Dynamic CRM 2013学习笔记(十八)根据主表状态用JS控制子表自定义按钮
有时要根据主表的审批状态来控制子表上的按钮要不要显示,比如我们有一个需求审批通过后就不能再上传文件了. 首先打开Visual Ribbon Editor, 如下图,我们可以利用Enable Rules ...
- Dynamic CRM 2013学习笔记(十五)报表设计:报表入门、开发工具及注意事项
本文是关于CRM 2013报表开发入门介绍,包括开发工具的使用,以及不同于普通Reporting service的相关注意事项. 一.CRM报表简介 报表有两种,SQL-based报表和Fetch-b ...
- Dynamic CRM 2013学习笔记(十九)自定义审批流1 - 效果演示
CRM的项目,审批流是一个必须品.为了更方便灵活地使用.配置审批流,我们自定义了一整套审批流.首先来看下它的效果: 1. 审批模板 这是一个最简单的审批流,首先指定审批实体,及相关字段,再配置流程节点 ...
- Dynamic CRM 2013学习笔记 系列汇总
这里列出所有 Dynamic CRM 2013学习笔记 系列文章,方便大家查阅.有任何建议.意见.需要,欢迎大家提交评论一起讨论. 本文原文地址: Dynamic CRM 2013学习笔记 系列汇总 ...
- Dynamic CRM 2013学习笔记(二十六)报表设计:Reporting Service报表 动态参数、参数多选全选、动态列、动态显示行字体颜色
上次介绍过CRM里开始报表的一些注意事项:Dynamic CRM 2013学习笔记(十五)报表入门.开发工具及注意事项,本文继续介绍报表里的一些动态效果:动态显示参数,参数是从数据库里查询出来的:参数 ...
随机推荐
- Bitmap 的bitmapdata和texture区别
Texture:纹理类是对不同平台不同的图片资源的封装在HTML5中,资源是一个HTMLElement对象在OpenGL / WebGL中,资源是一个提交GPU后获取的纹理idTexture类封装了这 ...
- JS --正则表达式验证、实战之邮箱模式
JS验证格式:提高用户体验,验证文本. 需要防止程序员的代码结构更改攻击,因为web段的代码有可能会被更改,更改后JS有可能会验证不住那么,C#端在JS段通过验证的情况下,还需要进行二次验证 < ...
- JStrom的zk数据
/jstorm/masterlock 用于LeaderSelector的锁. /jstorm/master get /jstorm/master localhost.localdomain:7627 ...
- ios 学习总结之动画(转)
转自:http://blog.sina.com.cn/s/blog_a85effc301012wu4.html UIView的,翻转.旋转,偏移,翻页,缩放,取反的动画效果 翻转的动画 //开始动 ...
- map/reduce of python
[map/reduce of python] 参考: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac92 ...
- iframe自动适应高度1
js: function iFrameHeight() { var ifm= document.getElementById("iframepage"); var subWeb = ...
- 数据库记录转换成json格式 (2011-03-13 19:48:37) (转)
http://blog.sina.com.cn/s/blog_621768f30100r6v7.html 数据库记录转换成json格式 (2011-03-13 19:48:37) 转载▼ 标签: 杂谈 ...
- Factory Method(工厂方法)-对象创建型模式
1.意图 定义一个用于创建对象的接口,让子类决定实例化哪一个类.Factory Method使一个类的实例化延迟到其子类. 2.动机 框架使用抽象类定义和维护对象之间的关系.这些对象的创建通常也由框架 ...
- poj 2446 Chessboard (二分匹配)
Chessboard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12800 Accepted: 4000 Descr ...
- 使用 IntraWeb (43) - 测试读取 SqLite (二)
一般情况下, 数据源相关控件应该有数据模块中统一管理, 这也方便其他窗体调用; UserSessionUnit 就是一个现成的数据模块. 现在把数据源相关控件放在 UserSessionUnit 的窗 ...