两个多对多关系的表单需要计算金额

两个多对多关系的表单需要计算金额

背景:回款模块包含子表单,子表单中可以选择应收单,需要计算应收单的认领金额和未认领金额,认领金额就是应收单关联了多少回款

解决办法:建立中间模块,中间模块创建两个查找字段,同时关联应收和回款,然后创建工作流调用函数,保证无论回款明细行怎么编辑修改,中间模块的回款金额,应收单和回款明细行保持一致即可进行计算。

Sample Code:

record = zoho.crm.getRecordById("CustomModule48",id);
//获取回款明细
subform = record.get("LinkingModule22");
//同步更新应收-回款中间表
if(subform.size() > 0)
{
info subform;
//遍历明细表
for each  huiKuan in subform
{
//获取序号
serialNumber = huiKuan.get("LinkingModule22_Serial_Number").toLong();
info "序号:" + serialNumber;
yingShouDan = huiKuan.get("field18");
if(!yingShouDan.isNull())
{
//获取应收单id
yingShouDanId = huiKuan.get("field18").get("id");
info "应收单id:" + yingShouDanId;
//获取认领金额,认领金额=总金额
renLingAmount = ifNull(huiKuan.get("field"),0);
info "认领金额:" + renLingAmount;
//根据明细行序号和回款id查找是否有重复数据
dataList = zoho.crm.searchRecords("CustomModule95","((field1.id:equals:" + id + ")and(field2:equals:" + serialNumber + "))");
info dataList;
info "重复数据长度:" + dataList.size();
m1 = Map();
if(dataList.size() > 0)
{
m1.put("field",yingShouDanId);
//更新重复数据的认领金额
m1.put("field3",renLingAmount);
result = zoho.crm.updateRecord("CustomModule95",dataList.get(0).get("id"),m1,{"trigger":{"workflow","blueprint","approval"}});
info "更新结果:" + result;
if(dataList.size() > 1)
{
//删除这张回款-应收中间表
response = invokeurl
[
url :"https://www.zohoapis.com.cn/crm/v3/CustomModule95/" + dataList.get(1).get("id") + "?wf_trigger=true"
type :DELETE
connection:"crm"
];
info "删除结果:" + response;
}
}
else
{
//生成中间表数据
//更新回款
m1.put("field1",id);
//应收单
m1.put("field",yingShouDanId);
//认领金额
m1.put("field3",renLingAmount);
//序号
m1.put("field2",serialNumber);
result = zoho.crm.createRecord("CustomModule95",m1,{"trigger":{"workflow","blueprint","approval"}});
info "创建结果:" + result;
}
}
else
{
//删除这一行的中间记录
info "删除中间表";
middleRecords = zoho.crm.searchRecords("CustomModule95","((field1.id:equals:" + id + ")and(field2:equals:" + serialNumber + "))");
info "中间表记录大小:" + middleRecords.size();
if(middleRecords.size() > 0)
{
info middleRecords.get(0).get("id");
//删除这张回款-应收中间表
response = invokeurl
[
url :"https://www.zohoapis.com.cn/crm/v3/CustomModule95/" + middleRecords.get(0).get("id") + "?wf_trigger=true"
type :DELETE
connection:"crm"
];
info response;
}
}
}
//查找回款关联的回款-应收明细相关列表
yingShouRelaterdList = zoho.crm.getRelatedRecords("relatedlist1","CustomModule48",id);
info "关联应收明细列表长度:" + yingShouRelaterdList.size();
if(yingShouRelaterdList.size() > subform.size())
{
//遍历应收执行相关列表
for each  yingShou in yingShouRelaterdList
{
//如果序号大于应收执行列表,则删除这张中间表
if(yingShou.get("field2") > subform.size())
{
//删除这张回款-应收中间表
response = invokeurl
[
url :"https://www.zohoapis.com.cn/crm/v3/CustomModule95/" + yingShou.get("id") + "?wf_trigger=true"
type :DELETE
connection:"crm"
];
info response;
}
}
}
}
else
{
//查找回款关联的回款-应收明细相关列表
yingShouRelaterdList = zoho.crm.getRelatedRecords("relatedlist1","CustomModule48",id);
info "关联应收明细列表长度:" + yingShouRelaterdList.size();
if(yingShouRelaterdList.size() > subform.size())
{
//遍历应收执行相关列表
for each  yingShou in yingShouRelaterdList
{
//删除回款-应收中间表
response = invokeurl
[
url :"https://www.zohoapis.com.cn/crm/v3/CustomModule95/" + yingShou.get("id") + "?wf_trigger=true"
type :DELETE
connection:"crm"
];
info response;
}
}
}