You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Frappe中如何为Disciplinary Action单据添加关联历史单据列表字段

实现Frappe中Disciplinary Action单据自动加载历史记录的方案

核心思路

通过客户端脚本监听accused字段的变更事件,查询该用户的历史Disciplinary Action单据,动态填充到预先配置的子表中。

步骤1:配置子表结构

在自定义DocType Disciplinary Action 中添加一个子表字段(例如命名为history_records),关联的子表DocType(可自定义为Disciplinary Action History)需包含以下字段:

  • action_id:数据类型设为Link,关联到Disciplinary Action(存储历史单据ID)
  • document_state:数据类型根据实际状态配置(如Data或Link到状态DocType)
  • final_charges:数据类型设为Text或Data(匹配原单据的字段类型)

步骤2:编写客户端脚本

在Disciplinary Action的客户端脚本中添加以下逻辑,监听accused字段变化并填充历史记录:

frappe.ui.form.on('Disciplinary Action', {
    accused: function(frm) {
        // 清空子表原有内容
        frm.clear_table('history_records');
        
        // 若无选中被指控人,直接返回
        if (!frm.doc.accused) return;
        
        // 查询该被指控人的历史单据(排除当前单据)
        frappe.db.get_list('Disciplinary Action', {
            filters: {
                accused: frm.doc.accused,
                name: ['!=', frm.doc.name]
            },
            fields: ['name', 'document_state', 'final_charges'],
            order_by: 'creation desc' // 按创建时间倒序展示最新记录
        }).then(records => {
            if (records.length) {
                records.forEach(record => {
                    const row = frm.add_child('history_records');
                    row.action_id = record.name;
                    row.document_state = record.document_state;
                    row.final_charges = record.final_charges;
                });
                // 刷新子表字段展示数据
                frm.refresh_field('history_records');
            }
        });
    },
    // 可选:加载单据时自动触发(编辑已有单据时同步加载历史)
    onload: function(frm) {
        if (frm.doc.accused) {
            frm.trigger('accused');
        }
    }
});

补充说明

  • 确保子表字段名与查询返回的字段严格对应,避免填充失败
  • 可根据需求调整filters参数,例如只查询已提交/已结案的历史单据
  • 若存在权限控制,需保证当前用户拥有读取历史Disciplinary Action单据的权限

内容的提问来源于stack exchange,提问作者Eben van Deventer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 07:42:34