# 场景案例
# 字段级校验
# 关联必填校验
场景描述:子表单内,出行方式选择为飞机后,航班信息字段必填
表单:
代码:
(function () {
var doc = $.context.getCurrentDocument();
var subDocs = doc.getElementByName("出行明细").getSubDocuments();
for (var i = 0; i < subDocs.size(); i++) {
var subDoc = subDocs.get(i);
var way = subDoc.getElementByName("出行方式").getValue();
var info = subDoc.getElementByName("航班信息").getValue();
if (way == 2 && info == null) {
//获取默认校验信息集合
var message = $.validate.create();
message.addError("航班信息不能为空");
//返回默认校验信息集合
return message;
}
}
})()
# 单一字段校验
场景描述:报销金额字段必须小于1000元
表单:
代码:
(function(){
//获取提交表单
var doc = $.context.getCurrentDocument();
//获取总金额控件值
var count = doc.getElementByName("报销金额").getIntValue();
if(count > 1000){
//创建校验消息对象
var message = $.validate.create();
message.addError("金额大于1000,不允许提交;");
return message;
}
})()
# 多重校验
场景描述:价值观收集:子表单内容需填写全部维度的价值观评价,价值观共有5个,需校验每个价值观都有填写到且不能重复填写
表单:
代码:
(function () {
var doc = $.context.getCurrentDocument();
var subDocs = doc.getElementByName("填报明细").getSubDocuments();
var set = new Packages.java.util.HashSet();
var isEmpty = false;
if (subDocs != null) {
for (var i = 0; i < subDocs.size(); i++) {
var subDoc = subDocs.get(i);
var infoList = subDoc.getElementByName("价值观").getValue();
set.add(infoList);
var description = subDoc.getElementByName("价值描述").getValue();
var comment = subDoc.getElementByName("关联评论").getValue();
// 判断关联评论和价值描述是否为空
if (description == null || description == '' || comment == null || comment == '') {
isEmpty = true;
}
}
if (set.size() != 5 || isEmpty) {
//获取默认校验信息集合
var message = $.validate.create();
message.addError("价值观填写不完整");
//返回默认校验信息集合
return message;
}
}
})()
# 复制逻辑校验
# 一次校验
场景描述:粤康码收集:查找昨天用户有身体不适的记录,今天不能再录入
表单:
代码:
(录入前校验)
(function () {
var doc = $.context.getCurrentDocument();
//应用Id
var applicationId = $.context.getCurrentApplicationId();
//表单名称
var formName = "健康码记录";
//控件值键对
var filterParamList = new Packages.java.util.ArrayList();
var userInfo = doc.getElementByName("登记人").getValue();
var calendar = new Packages.java.util.Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
var lastDayEnd = calendar.getTime();
calendar.add(Calendar.DATE, -1);
var lastDayStart = calendar.getTime();
var dateInfo = "(" + lastDayStart + "," + lastDayEnd + ")";
var filterParam = new Packages.cn.com.do1.do1cloud.runtime.dto.FilterParam("登记日期", "in", dateInfo);
var userInfoParam = new Packages.cn.com.do1.do1cloud.runtime.dto.FilterParam("登记人", "eq", userInfo);
FilterParamList.add(filterParam);
FilterParamList.add(userInfoParam);
var lasyDayDocumentList = $.form.getDocumentsByFilterParam(applicationId, formName, filterParamList);
for (var i = 0; i < lasyDayDocumentList.size(); i++) {
var doc = lasyDayDocumentList.get(i);
var statues = doc.getElementByName("健康状态");
if (statues == '不适') {
//获取默认校验信息集合
var message = $.validate.create();
message.addError("用户无法录入");
//返回默认校验信息集合
return message;
}
}
})()
# 多次校验
场景描述:粤康码收集:查找昨天用户有身体不适的记录,今天不能再录入/粤康码收集:今日已记录信息,不再重复提交
表单:
代码:
(function(){
var message = $.validate.create()
var userId = $.context.getCurrentUserId();
var appId = $.context.getCurrentApplicationId();
//获取当前日期的前一天
var date = $.date.getCurrentDate();
var cal = new Packages.java.util.Calendar.getInstance();
cal.setTime(date);
cal.add(5,-1);
var lastDate = cal.getTime();
var lastDateStr = $.date.dateToString(lastDate,"yyyy-MM-dd");
var fieldMap = new Packages.java.util.HashMap();
fieldMap.put("提交日期",lastDateStr);
fieldMap.put("员工姓名",userId);
var formModelEntity = $.form.getFormModel(appId,"信息记录表");
var docs = $.form.getDocumentsByFormModelAndCondition(appId,formModelEntity,fieldMap);
if(docs==null || docs.size()==0){
message.addError("您昨天的身体状况异常,不能进入公司");
return message;
}
var doc = docs.get(0);
var temperature = doc.getElementByName("体温情况").getValue();
var situation = doc.getElementByName("身体健康状况").getValue();
if(temperature!="1"|| situation!="1"){
message.addError("您昨天的身体状况异常,不能进入公司");
return message;
}
var date1 = $.date.getCurrentDate();
var dateStr1 = $.date.dateToString(date1,"yyyy-MM-dd");
var fieldMap1 = new Packages.java.util.HashMap();
fieldMap1.put("日期",dateStr1);
fieldMap1.put("姓名",userId);
var formModelEntity1 = $.form.getFormModel(appId,"复工体温表");
var docs1 = $.form.getDocumentsByFormModelAndCondition(appId,formModelEntity1,fieldMap1);
if(docs1!=null && docs1.size()>0){
message.addError("您今天已登记过体温,无法重复登记");
return message;
}
})()
# 消息推送
# 站内信
场景描述:CRM:客户报备保护期即将过,提醒对应的业务员
表单:
代码:
(function(){
//获取当前文档
var doc = $.context.getCurrentDocument();
//获取用户id
var userId = doc.getElementByName("客户表").getValue();
//自定义消息内容
var subject = "消息标题";
var context = "消息内容";
//发送七巧站内信
$.message.sendGeneralStationMessage(subject,context,userId);
})()
# 邮件
场景描述:完成客户报价后,将报价信息邮件发送给客户
邮箱字段:客户邮箱
表单:
备注:
代码:
(function () {
var doc = $.context.getCurrentDocument();
var email = doc.getElementByName("客户邮箱").getValue();
var userName = doc.getElementByName("客户名称").getValue();
var totalPrice = doc.getElementByName("报价总金额").getValue();
var date = doc.getElementByName("报价有效期").getValue();
var remark = doc.getElementByName("报价备注").getValue();
var infoList = doc.getElementByName("报名明细").getValue();
var
//邮件标题
var subject = "报价信息";
//邮件正文
var context = "" +userName + "客户,你的报价总金额为" + totalPrice +"元,有效期至:" + date +"\n" +"报价明细如下:";
for(var i = 0; i <infoList.size(); i++) {
var subDoc = infoList.get(i).getSubDocuments()
var priceProduct = subDoc.getElementByName("报价产品").getValue();
var num = subDoc.getElementByName("数量").getValue();
var price = subDoc.getElementByName("单价").getValue();
var total = subDoc.getElementByName("总价").getValue();
context = context + priceProduct + num + price + total + "\n";
}
context = context + remark;
//发送邮件
$.message.sendEmail(subject, context, email);
})()
”接口
表单:
代码:
(function(){
//构建URI
var uri = "http://sms.tencentcloudapi.com/?Action=SendSms";
//构建参数
var params = new Packages.java.util.HashMap();
params.put("PhoneNumberSet.0","电话号码(例:11111111111)");
params.put("TemplateID.0","1234");
params.put("Sign","腾讯云")
params.put("TemplateParamSet.0","12345");
params.put("SmsSdkAppid","14000006666");
params.put("SessionContext","test");
//构建请求头
var headers = new Packages.java.util.HashMap();
headers.put("Content-Type","application/json;charset=UTF-8");
//获取响应字符串
var response = $.httpclient.sendGet(uri,params,headers);
//返回响应字符串
return response;
})()
# 群机器人
场景描述:工时填报:提醒未填报工时的人进行同事填报
表单:
代码:
(function(){
var ignoreUserNamelist = new Packages.java.util.ArrayList();
ignoreUserNamelist.add("");
var uris = new Packages.java.util.ArrayList();
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f07169da-537c-451b-bd0c-e28fe4e3c40f");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8886d1ba-a234-4e71-b6f4-ed2e8f7ede70");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c72b7df7-9943-4fe8-b8dd-4dc80ab17120");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8c606413-96a3-4c0a-ad6a-641336fe1199");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=48d38b8f-4af2-4318-aedd-53024af8efd1");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=90251b67-8a03-4138-be0b-5848333e1b73");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fd13ece0-07cb-4f71-9ad3-4ef37375e08e");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=6da39e79-cccb-436f-9d2c-80a40ea76411");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4fe2f282-0851-4b51-b379-daf8c6740956");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=77e43b65-debe-4f25-8aad-fa9bf73d227e");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f0d13e77-0741-48dd-b9b6-48bd3237f278");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=cbfb7d01-07c3-4684-b3ea-aebfd924b50e");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f07169da-537c-451b-bd0c-e28fe4e3c40f");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=db081a76-3fe3-4b45-87d2-0c6106f6a014");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f2ad10b4-0b41-4068-a8ca-202259f977f9");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=866284aa-29f1-4fd8-a71f-0d25f378b653");
uris.add("http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=640b8a34-4441-4bcb-8cb9-dfad5bc124a6");
var appId = $.context.getCurrentApplicationId();
var date = $.date.getCurrentDate();
var dateStr = $.date.dateToString(date,"yyyy-MM-dd");
var fieldMap = new Packages.java.util.HashMap();
fieldMap.put("未填日期",dateStr);
var docs = $.form.getFormDocumentsByFieldNameAndValue(appId,"未填人员",fieldMap);
var headers = new Packages.java.util.HashMap();
headers.put("Content-Type","application/json;charset=UTF-8");
var jsonObject = new Packages.com.alibaba.fastjson.JSONObject();
jsonObject.put("msgtype","text");
var msgJsonObject = new Packages.com.alibaba.fastjson.JSONObject();
msgJsonObject.put("content","报平安提醒:请以下人员填写今日报平安数据\n(如果我没有@人,说明本群的小伙伴全部填写完成啦)");
var jsonArray = new Packages.com.alibaba.fastjson.JSONArray();
for(var i=0;i<docs.size();i++){
var doc = docs.get(i);
var userId = doc.getElementByName("人员名称").getValue();
var user = $.contact.getUserById(userId);
var userName = user.getName();
if(!ignoreUserNamelist.contains(userName)){
jsonArray.add(user.getTelephone());
}
}
if(jsonArray.size()>0){
msgJsonObject.put("mentioned_mobile_list",jsonArray);
jsonObject.put("text",msgJsonObject);
var params = new Packages.java.util.HashMap();
for(var j=0;j<uris.size();j++){
var uri = uris.get(j);
var response = $.httpclient.sendPost(uri,params,headers,jsonObject.toString());
//$.message.sendGeneralStationMessage("机器人接口返回数据",response,$.context.getCurrentUserId());
}
}
})()
# 表单更新
# 遍历子表更新
场景描述:项目预算,项目主要有总预算.已使用预算.剩余预算,每支出一笔费用,都需要更新已使用跟剩余预算
表单:
代码:
(function () {
//获取当前文档
var currentDocument = $.context.getCurrentDocument();
//获取子表单数据
var subDocs = currentDocument.getElementByName("预算使用计划").getSubDocuments();
//获取应用id
var applicationId = $.context.getCurrentApplicationId();
//获取子表数据
var allNum = 0;
if (!subDocs == null) {
for (var i = 0; i < subDocs.size(); i++) {
allNum = allNum + subDocs.get(i).getElementByName("支出金额").getValue();
}
var docAllNum = parseInt(currentDocument.getElementByName("剩余预算").getValue()) - parseInt(allNum);
var docalearyNum = parseInt(currentDocument.getElementByName("已使用预算").getValue()) + parseInt(allNum);
currentDocument.getElementByName("剩余预算").setValue(docAllNum);
currentDocument.getElementByName("已使用预算").setValue(docalearyNum);
//保存文档对象
$.form.saveFormDocument(document, applicationId);
}
})()
# 遍历子表添加
场景描述:创建考核模板,点击生成,生成对应岗位的考核记录,调整考核模板,则更新所有的考核记录
表单:
代码:
(function () {
//获取当前文档
var currentDocument = $.context.getCurrentDocument();
var station = currentDocument.getElementByName("考核岗位").getValue();
//获取应用id
var applicationId = $.context.getCurrentApplicationId();
//保存后获取文档对象
var formDocument = $.form.saveFormDocument(currentDocument, applicationId);
var subDocs = doc.getElementByName("考核内容").getSubDocuments();
//表单模型实体
var formModelOne = $.form.getFormModel(applicationId, "绩效考核记录");
//返回绩效考核记录子表单空文档对象
var appraisalDoc = $.form.createEmptyDocumentByFormModel(applicationId, formModelOne);
appraisalDoc.addElement("关联考核模板", formDocument.getId());
var appraisalDocId = $.form.saveFormDocument(appraisalDoc, applicationId).getId();
if (subDocs != null) {
for (var i = 0; i < subDocs.size(); i++) {
var subDoc = subDocs.get(i);
//表单模型实体
var formModel = $.form.getFormModel(applicationId, "考核明细");
//返回考核明细子表单空文档对象
var emptyDoc = $.form.createEmptyDocumentByFormModel(applicationId, formModel);
emptyDoc.addElement("项目", subDoc.getElementByName("项目").getValue());
emptyDoc.addElement("分值", subDoc.getElementByName("分值").getValue());
emptyDoc.addElement("描述", subDoc.getElementByName("描述").getValue());
emptyDoc.addElement("关联主表外键", appraisalDocId);
$.form.saveFormDocument(emptyDoc, applicationId);
}
}
})()
获取主要信息写到子表中 场景描述:添加跟进明细后,更新主表”最近更新时间“及子表“关联客户”
表单:
代码:
(function(){
var appId= $.context.getCurrentApplicationId();
var originalDocument = $.context.getHttpRequest().getAttribute("document");
var name = originalDocument.getElementByName("客户名称").getValue();
var storageDoc = $.context.getCurrentDocument();
storageDoc.addElement("所属客户",originalDocument.getId());
})()
# 数据复制
场景描述:教师确认上课课时后,财务中新增一项支出记录
表单:
代码:
(function () {
//获取当前文档
var currentDocument = $.context.getCurrentDocument();
var teach = currentDocument.getElementByName("关联教师").getValue();
var classTime = currentDocument.getElementByName("上课时间").getValue();
var classHours = currentDocument.getElementByName("上课课时").getValue();
//获取应用id
var applicationId = $.context.getCurrentApplicationId();
//表单模型实体
var formModelOne = $.form.getFormModel(applicationId, "财务预算支出记录");
//返回绩效考核记录子表单空文档对象
var financeDoc = $.form.createEmptyDocumentByFormModel(applicationId, formModelOne);
financeDoc.addElement("关联教师", teach);
financeDoc.addElement("上课时间", classTime);
financeDoc.addElement("上课课时", classHours);
//保存财务预算支出记录表单
$.form.saveFormDocument(financeDoc, applicationId);
})()
# 数据批量编辑
场景描述:批量编辑客户状态,提交后,变为已通过审核(自定义按钮)
表单:
代码:
(function () {
var doc = $.context.getCurrentDocument();
var applicationId = $.context.getCurrentApplicationId()
var statusEle = doc.getElementByName("客户状态");
statusEle.setValue("已通过审核");
$.form.saveFormDocument(doc, applicationId)
})()
# 其余场景
# 获取通讯录用户作为消息发送的对象
场景描述:获取通讯录所有用户作为发送消息的对象
代码:
(function(){
var pageSize = 5000;
var currPage = 1;
var userList = new Packages.java.util.ArrayList();
var map = new Packages.java.util.HashMap();
var SpringContextUtils = Packages.cn.com.do1.qiqiao.common.utils.SpringContextUtils;
var departmentAdaptee = SpringContextUtils.getBean("departmentAdaptee");
//var departmentAdaptee = Packages.cn.com.do1.qiqiao.common.utils.SpringContextUtils.getBean(Packages.cn.com.do1.qiqiao.compont.contact.adaptee.DepartmentAdaptee.class);
var allDepartmentMap = departmentAdaptee.getAllDepartmentIdAndName();
if(allDepartmentMap){
var entrySet = allDepartmentMap.entrySet();
for(var iterator = entrySet.iterator(); iterator.hasNext();){
var deptItem = iterator.next();
var departmentId = deptItem.getKey();
var users = $.contact.queryUsersByDepartmentId(departmentId, pageSize, currPage);
if(users){
var list = users.getList();
if(list){
for(var iterator2 = list.iterator(); iterator2.hasNext();){
var u = iterator2.next();
if(u){
map.put(u.getId(),u);
}
}
}
}
}
userList.addAll(map.values());
}
return userList;
})()
# 获取表单各类型控件值
场景描述:获取表单各类型控件值
代码:
(function(){
//获取当前文档
var doc = $.context.getCurrentDocument();
//获取单行文本控件值(返回类型:String)
var text = doc.getElementByName("姓名").getValue();
//获取数据控件控件值(返回类型:int)
var age = doc.getElementByName("年龄").getIntValue();
//获取子表单控件值(返回类型:List<Document>)
var subDocuments = doc.getElementByName("成绩单").getSubDocuments();
//获取人员单选控件值(返回类型:String)
var userId = doc.getElementByName("班主任").getValue();
//获取人员多选控件值(返回类型:JSONArray)
var userIds = doc.getElementByName("任课老师").getValue();
//获取部门单选控件值(返回类型:String)
var deptId = doc.getElementByName("部门单选").getValue();
//获取部门多选控件值(返回类型:JSONArray)
var deptIds = doc.getElementByName("部门多选").getValue();
})()
# 批量发起流程
场景描述:批量发起流程,批量选中列表数据,发起流程审批,发起人为表单数据字段“创建人”
代码:
(function(){
//流程模型key
var processModelKey = "5f111ec52cf6be0001fbe5e7";
//用户id
var userId = $.context.getCurrentUserId();
//文档对象
var document =$.context.getCurrentDocument();
//获取流程实例对象
var processInstance = $.process.startProcessInstance(processModelKey, userId, document);
//返回流程实例对象
return processInstance;
})()
# 为控件赋值
场景描述:为控件赋值,调整表单默认值,通常用于按钮执行前事件或流程中“系统任务”
说明:
- 如果是在提交或者编辑按钮的执行前,则可以不用写saveformDocument()方法保存
- 如果是自定义开发等按钮则在赋值后需要写saveformDocument()函数进行表单数据的保存。
代码:
(function(){
var doc = $.context.getCurrentDocument();
doc.addElement("姓名","steven");
doc.addElement("手机号码","138****0000");
})()
# 人员多选控件复值
场景描述:为人员、部门多选控件赋值
代码:
(function(){
//获取当前document
var currentDocument = $.context.getCurrentDocument();
//新建javalist
var list = new Packages.java.util.ArrayList();
//筛入人员ID
list.add("123");
list.add("345");
list.add("678");
//删除通讯录组件友好值缓存
currentDocument.removeElement("人员多选1_pretty_value");
//塞入当前document
currentDocument.getElementByName("人员多选1").setValue(list)
}())
# 表单列表的批量审批
场景描述:结合「自定义按钮」,实现自定义勾选n条已发起流程的表单,对特定节点实现流程批量审批功能
代码:
(function () {
var doc = $.context.getCurrentDocument();
var processInstanceId = doc.getProcessInstanceId();
//用户id
var userId = $.context.getCurrentUserId();
//应用id
var applicationId = $.context.getCurrentApplicationId();
//获取任务实例对象
var task = $.process.getTaskListByProcessInstanceIdAndUserIdAndApplicationId(processInstanceId, userId, applicationId);
for(i = 0; i < task.size(); i++){
var data = task.get(i);
var taskName = data.getActivityDefinitionId();
if(taskName == "obj_03"){
var taskId = data.getId();
var comment = "comment";
$.process.completeTask(processInstanceId, taskId, userId, doc, comment);
}
}
})()
# 获取顶级部门ID的方法(一层一层往上拿)
function recursionGetDepartment(department) {
if (department.getDirectParentId() == null || "".equals(department.getDirectParentId())) {
return department;
}
var dep = $.contact.getDepartmentById(department.getDirectParentId());
if (dep != null || dep) {
return recursionGetDepartment(dep);
}
return dep == null ? department : dep;
}
# 获取提单人的id
var userId = doc.getElementByName("提单人").getValue();
# 根据提单人的id获取user对象
var user = $.contact.getUserById(userId);
# 在user对象中找到提单人的部门id
var deptId = user.getDefaultDepartmentId()
# 根据部门id查询部门负责人
var departmentManagers = $.contact.listDepartmentManagersByDepartmentId(deptId);
# 获取浏览器cookie信息(用这个获取请求, 再拿里面的cookies)
(function(){
//获取客户端请求
var httpRequest = $.context.getHttpRequest();
//返回客户端请求
return httpRequest;
})()