forked from BKnights/KotN-Netsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kotnMergeTemplate.js
54 lines (41 loc) · 1.89 KB
/
kotnMergeTemplate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function kotnMergeTemplate(templateId, primType, primId, secType, secId, extraFields, outType){
var template = nlapiLoadRecord('emailtemplate', templateId);
if('FREEMARKER' != template.getFieldValue('templateversion')){
return nlapiMergeRecord(templateId, primType, primId, secType, secId, extraFields);
}
var renderer = nlapiCreateTemplateRenderer();
var content = 'T' == template.getFieldValue('usesmedia') ?
(function(){
var file = nlapiLoadFile(template.getFieldValue('mediaitem'));
return file.getValue();
})() :
template.getFieldValue('content');
var custFieldPatt = /\{\{([^\}]+)\}\}/g;
content = content.replace(custFieldPatt, function(a,m){
return extraFields[m] || extraFields[m.toUpperCase()] || extraFields[m.toLowerCase()] || '';
});
var oldCustFieldPatt = /<(nl[^ >]+)>(\s*<\/\1>)?/ig;
content = content.replace(oldCustFieldPatt, function(a,m){
return extraFields[m.toUpperCase()] || extraFields[m.toLowerCase()] || '';
});
var fillStandards = function (suppliedType, rec){
if(rec.getFieldValue('trandate') && suppliedType != 'transaction' ) renderer.addRecord('transaction', rec);
if(rec.getFieldValue('entityid') && suppliedType != 'entity') renderer.addRecord('entity', rec);
if('supportcase' == rec.getRecordType() && suppliedType != 'case') renderer.addRecord('case', 'rec');
};
if(secType) {
var secRec = nlapiLoadRecord(secType, secId);
renderer.addRecord(secType, secRec);
fillStandards(secType, secRec);
}
if(primType) {
var primRec = nlapiLoadRecord(primType, primId);
renderer.addRecord(primType, primRec);
fillStandards(primType, primRec);
}
var getRendered = function (tmplt){
renderer.setTemplate(tmplt);
return renderer.renderToString();
};
return nlapiCreateFile(getRendered(template.getFieldValue('subject')), outType || 'HTMLDOC', getRendered(content));
}