-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerPayments_AfterSubmit_Script_3.js
88 lines (88 loc) · 3.96 KB
/
CustomerPayments_AfterSubmit_Script_3.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
define(["require", "exports", "N/http", "N/record", "N/log"], function (require, exports, http, record, log) {
Object.defineProperty(exports, "__esModule", { value: true });
var apiUrl = 'http://12.176.134.134:9090/ws/simple/executeNSRequest';
var applyListId = "apply";
exports.afterSubmit = function sendProductData(context) {
var prodNewRecord = context.newRecord;
// var addresses = getSublistObjects(prodNewRecord, addressListId);
// var defaultBilling = addresses.filter(function (a) { return a.defaultbilling; })[0];
// log.debug("BILLING ADDR", JSON.stringify(defaultBilling));
var internalId = prodNewRecord.getValue("id");
prodNewRecord.getFields;
// var vendorID = prodNewRecord.getValue("entityid");
// var address = defaultBilling.addr1_initialvalue;
// var city = defaultBilling.city_initialvalue;
// var dropdownState = defaultBilling.displaystate_initialvalue;
// var zip = defaultBilling.zip_initialvalue;
// log.debug("VALUES", "sfdcNum: " + JSON.stringify(sfdcNum) + ", poNum: " + JSON.stringify(poNum));
var applyRows = getSublistObjects(prodNewRecord, applyListId);
var invoices = applyRows.map(function (r) {
var invoice = record.load({ id: r["internalid"], type: record.Type.INVOICE });
var salesForceId = invoice.getValue({ fieldId: "custbodysalesforce_order_id" });
var amountRemaining = invoice.getValue({ fieldId: "amountremaining" });
var amountPaid = invoice.getValue({ fieldId: "amountpaid" });
var invoiceId = r["internalid"];
return { salesForceId: salesForceId, amountRemaining: amountRemaining, amountPaid: amountPaid, invoiceId: invoiceId };
})
.filter(function (r) { return !!r.salesForceId; });
if (invoices.length > 0) {
// log.debug("APPLY FIELDS", JSON.stringify(test));
// log.debug("LISTS", lists.join(", "));
var postData = { parentRecordId: internalId, invoiceRecords: invoices, "object": "Customer Payments" };
try {
var response = postJson(postData);
var newSFID = response.body;
newSFID = newSFID.replace('\n', '');
}
catch (er02) {
log.error('ERROR', JSON.stringify(er02));
}
}
};
function postJson(json) {
var header = [];
header['Content-Type'] = 'application/json';
header['Accept'] = 'application/json';
return http.post({
url: apiUrl,
headers: header,
body: JSON.stringify(json)
});
}
function getSublistObjects(record, sublistId) {
var subListFields = record.getSublistFields({ sublistId: sublistId });
var rowCount = record.getLineCount({ sublistId: sublistId });
var list = [];
for (var r = 0; r < rowCount; r++) {
var obj = {};
for (var f = 0; f < subListFields.length; f++) {
var value = record.getSublistValue({
sublistId: sublistId,
fieldId: subListFields[f],
line: r
});
obj[subListFields[f]] = value;
}
list.push(obj);
}
return list;
}
function getSublistFields(record, sublistId) {
var subListFields = record.getSublistFields({ sublistId: sublistId });
var rowCount = record.getLineCount({ sublistId: sublistId });
var list = [];
for (var r = 0; r < rowCount; r++) {
var obj = {};
for (var f = 0; f < subListFields.length; f++) {
var field = record.getSublistField({
sublistId: sublistId,
fieldId: subListFields[f],
line: r
});
obj[subListFields[f]] = field;
}
list.push(obj);
}
return list;
}
});