-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRestletExample.js
More file actions
149 lines (119 loc) · 5.16 KB
/
RestletExample.js
File metadata and controls
149 lines (119 loc) · 5.16 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @NApiVersion 2.x
* @NScriptType Restlet
* @NModuleScope SameAccount
*/
define(['N/search', 'N/record'],
/**
* @param {search} search
* @param {record} record
*/
function (search, record) {
/**
* Function called upon sending a GET request to the RESTlet.
*
* @param {Object} requestParams - Parameters from HTTP request URL; parameters will be passed into function as an Object (for all supported content types)
* @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
* @since 2015.1
*/
function doGet() {
}
/**
* Function called upon sending a PUT request to the RESTlet.
* @param {string | Object} requestBody - The HTTP request body; request body will be passed into function as a string when request Content-Type is 'text/plain'
* or parsed into an Object when request Content-Type is 'application/json' (in which case the body must be a valid JSON)
* @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
* @since 2015.2
*/
function doPut(requestBody) {
}
/**
* Function called upon sending a POST request to the RESTlet.
*
* @param {string | Object} requestBody - The HTTP request body; request body will be passed into function as a string when request Content-Type is 'text/plain'
* or parsed into an Object when request Content-Type is 'application/json' (in which case the body must be a valid JSON)
* @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
* @since 2015.2
*/
function doPost(requestBody) {
var salesOrderId = CreateNetSuiteOrder(requestBody);
if (salesOrderId > 0) {
log.debug('SO', salesOrderId);
} else {
log.debug('SO', 'Fail');
}
}
/**
* Function called upon sending a DELETE request to the RESTlet.
*
* @param {Object} requestParams - Parameters from HTTP request URL; parameters will be passed into function as an Object (for all supported content types)
* @returns {string | Object} HTTP response body; return string when request Content-Type is 'text/plain'; return Object when request Content-Type is 'application/json'
* @since 2015.2
*/
function doDelete(requestParams) {
}
function CreateNetSuiteOrder(requestBody) {
log.debug('Post body', requestBody);
var salesOrder = record.create({
type: record.Type.SALES_ORDER,
isDynamic: true,
defaultValues: {
entity: 123242440099
}
});
salesOrder.setValue({ fieldId: 'trandate', value: new Date('5/25/2019') });
var subrec = salesOrder.getSubrecord({
fieldId: 'shippingaddress'
});
subrec.setValue({ fieldId: 'addr1', value: '123 street' });
subrec.setValue({ fieldId: 'city', value: 'city' });
subrec.setValue({ fieldId: 'state', value: 'state' });
subrec.setValue({ fieldId: 'zip', value: 'CA' });
subrec.setValue({ fieldId: 'addressee', value: 'John' });
subrec.setValue({ fieldId: 'attention', value: 'John' });
salesOrder.selectNewLine({
sublistId: 'item'
});
var items = requestBody.items;
items.forEach(function (item) {
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: '4353535334535'//internal id
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: item.quantity_order
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'price',
value: -1 //custom in netsuite
});
salesOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: item.price_value
});
salesOrder.commitLine({
sublistId: 'item'
});
});
try {
var id = salesOrder.save({
ignoreMandatoryFields: false
});
log.debug('record save with id', id);//sales order internal id
return id;
} catch (e) {
return 0;
}
}
return {
'get': doGet,
put: doPut,
post: doPost,
'delete': doDelete
};
});