前面介绍过自定义审批流:

Dynamic CRM 2013学习笔记(十九)自定义审批流1 - 效果演示

Dynamic CRM 2013学习笔记(二十一)自定义审批流2 - 配置按钮

Dynamic CRM 2013学习笔记(三十二)自定义审批流3 - 节点及实体配置

Dynamic CRM 2013学习笔记(三十三)自定义审批流4 - 规则节点 -有分支的流程处理

Dynamic CRM 2013学习笔记(三十四)自定义审批流5 - 自动邮件通知

Dynamic CRM 2013学习笔记(三十五)自定义审批流6 - 审批通过后,再审批 - 二次审批

Dynamic CRM 2013学习笔记(三十七)自定义审批流7 - 初始化(整套审批流下载、安装)

虽然灵活,功能强大,不用修改代码,完全用配置就可以搞定。

最近客户需要实现一个很简单的审批流,就有了这篇简单审批流的实现。

首先根据需求文档分析流程:

Dynamic CRM 2013学习笔记(四十六)简单审批流的实现

功能实现如下:

一、 添加字段

一个是让用户修改的审批状态,如果当前用户是审批人,放开审批状态字段,让用户审批或拒绝;值为 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();
    },

相关文章: