Purpose#
Before saving or submitting a workflow, split files from one source attachment field into multiple target attachment fields according to filename keywords.
In this snippet:
scoahtmbis the source attachment field.sfsybzmbis the control field; the split only runs when its value is0.- A filename containing
经销协议is written to thehtfjattachment field. - A filename containing
奖励协议is written to thexyfjattachment field. /api/doc/save/getAccListForEditis used to fetch the full attachment display data, andspecialobj.filedataswrites it back into attachment fields.
Code#
<script>
jQuery(document).ready(function () {
var sourceField = WfForm.convertFieldNameToId("scoahtmb");
var controlField = WfForm.convertFieldNameToId("sfsybzmb");
var rules = [
{ keyword: "经销协议", targetField: WfForm.convertFieldNameToId("htfj") },
{ keyword: "奖励协议", targetField: WfForm.convertFieldNameToId("xyfj") }
];
function getFieldRealValue(fieldMark) {
var v = "";
try {
v = WfForm.getFieldValue(fieldMark);
} catch (e) {}
if (!v) {
v = jQuery("#" + fieldMark).val();
}
return v || "";
}
function canSplitAttach() {
return String(getFieldRealValue(controlField)) === "0";
}
function getAttachInfo(docid, callback) {
jQuery.ajax({
url: "/api/doc/save/getAccListForEdit",
type: "GET",
dataType: "json",
data: { id: docid },
success: function (res) {
var item = null;
if (res && res.dataList && res.dataList.length > 0) {
item = res.dataList[0];
}
callback(item);
},
error: function () {
callback(null);
}
});
}
function buildFileData(item) {
return {
fileExtendName: item.fileExtendName || "",
fileid: item.id,
filelink: item.filelink || "",
filename: item.filename || "",
filesize: item.filesize || "",
imgSrc: item.imgSrc || "/images/filetypeicons/txt_wev8.png",
loadlink: item.loadlink || "",
showLoad: true,
showDelete: true,
isImg: item.isImg || false,
imagefileid: item.fileid || "",
secretLevel: item.secretLevel || "4",
secretLevelValidity: item.secretLevelValidity || "",
secretLevelValidityValue: item.secretLevelValidityValue || ""
};
}
function setAttachField(fieldMark, fileDatas) {
var ids = [];
for (var i = 0; i < fileDatas.length; i++) {
ids.push(fileDatas[i].fileid);
}
var value = ids.join(",");
WfForm.changeFieldValue(fieldMark, {
value: value,
specialobj: {
filedatas: fileDatas,
showBatchLoad: false
}
});
jQuery("#" + fieldMark).val(value);
}
function splitAttachByName(callback) {
if (!canSplitAttach()) {
if (callback) {
callback();
}
return;
}
var sourceValue = getFieldRealValue(sourceField);
if (!sourceValue) {
if (callback) {
callback();
}
return;
}
var docids = sourceValue.split(",");
var realDocids = [];
for (var i = 0; i < docids.length; i++) {
var id = jQuery.trim(docids[i]);
if (id !== "") {
realDocids.push(id);
}
}
if (realDocids.length === 0) {
if (callback) {
callback();
}
return;
}
var finished = 0;
var matchedMap = {};
for (var r = 0; r < rules.length; r++) {
matchedMap[r] = [];
}
for (var j = 0; j < realDocids.length; j++) {
getAttachInfo(realDocids[j], function (item) {
finished++;
if (item) {
var fileData = buildFileData(item);
var filename = fileData.filename || "";
for (var k = 0; k < rules.length; k++) {
if (filename.indexOf(rules[k].keyword) > -1) {
matchedMap[k].push(fileData);
}
}
}
if (finished === realDocids.length) {
for (var m = 0; m < rules.length; m++) {
setAttachField(rules[m].targetField, matchedMap[m]);
}
if (callback) {
callback();
}
}
});
}
}
WfForm.registerCheckEvent(WfForm.OPER_SAVE + "," + WfForm.OPER_SUBMIT, function (callback) {
splitAttachByName(function () {
callback();
});
});
});
</script>What to Change#
- To change the source attachment field, update the field name assigned to
sourceField. - To change whether the split runs, update
controlFieldand the condition insidecanSplitAttach(). - To change routing rules, edit
keywordand the target attachment field inrules. - To add more target fields, append more entries to the
rulesarray. - If the attachment API response changes, adjust only
getAttachInfo()andbuildFileData().
Notes#
- This logic runs on
OPER_SAVEandOPER_SUBMIT, socallback()must be called only after all async attachment requests finish. - Target attachment fields are rewritten from the match result; if no file matches a rule, that target field becomes empty.
- If one filename matches multiple keywords, it will be written to multiple target attachment fields.
