Global API
...
Code Examples
Sales Order Flow
Class (Apex)
1min
Apex
Apex Test
1// Start Apex Class
2public with sharing class createNSSalesOrderRunAsync {
3 public createNSSalesOrderRunAsync() {
4 }
5
6 @InvocableMethod(label='Create NetSuite Sales Order' description='Create NetSuite Sales Order')
7 public static List<Response> createNetSuiteSalesOrder(List<Requests> requestList){
8 List<Response> resToBeReturnedList = new List<Response>();
9 List<Object> netSuiteSalesOrderList = new List<Object>();
10
11 for(Requests req : requestList){
12 netSuiteSalesOrderList.add(generateNetSuiteSalesOrderMap(req));
13 }
14 // Sending request to NetSuite via exposed Global API
15 Map<String, Object> reqJSONMap = new Map<String, Object>();
16 reqJSONMap.put('salesOrders' , netSuiteSalesOrderList);
17 String reqJSON = JSON.serialize(reqJSONMap);
18
19 Map<String, Object> finalReqMap = new Map<String, Object>();
20 finalReqMap.put('version' , '1.0');
21 finalReqMap.put('action' , 'createSalesOrder');
22 finalReqMap.put('RequestJSON' , reqJSON);
23 System.debug('Request ::: ' + finalReqMap);
24
25 Map<String, Object> respMap = breadwinner_ns.BreadwinnerNetSuiteAPI.call(finalReqMap);
26 System.debug('Response ::: ' + respMap);
27
28 Map<String,Object> soResponseJSONMap = (Map<String,Object>)JSON.deserializeUntyped((String)respMap.get('responseJSON'));
29
30 Response responseReturn = new Response();
31 if(soResponseJSONMap !=NULL && soResponseJSONMap.containskey('status') && soResponseJSONMap.get('status')!=null){
32 responseReturn.statusCode = (String)soResponseJSONMap.get('status');
33 }
34
35 if(soResponseJSONMap !=NULL && soResponseJSONMap.containskey('salesOrders') && soResponseJSONMap.get('salesOrders')!=null){
36 List<Object> soObj = (List<Object>)soResponseJSONMap.get('salesOrders');
37 Map<String,Object> savedSO = (Map<String,Object>)soObj[0];
38 responseReturn.recordId = (String)savedSO.get('salesforceID');
39 }
40
41
42 if(soResponseJSONMap !=NULL && soResponseJSONMap.containskey('errors') && soResponseJSONMap.get('errors')!=null){
43
44 responseReturn.error = JSON.serialize((List<Object>)soResponseJSONMap.get('errors'));
45 }
46 resToBeReturnedList.add(responseReturn);
47 //sendReqFuture(JSON.serialize(finalReqMap));
48
49 return resToBeReturnedList;
50 }
51
52 /*@future(callout = true)
53 public static void sendReqFuture(String jsonString){
54 Map<String, Object> finalReqMap = (Map<String, Object>)JSON.deserializeUntyped(jsonString);
55 Map<String, Object> respMap = breadwinner_ns.BreadwinnerNetSuiteAPI.call(finalReqMap);
56 System.debug('Response ::: ' + respMap);
57 }*/
58
59 public class Requests{
60 @InvocableVariable(label='Class' description='Class')
61 public String classification; //lookup
62
63 @InvocableVariable(label='Created From' description='Created From')
64 public String createdFrom; //lookup
65
66 @InvocableVariable(label='Currency' description='Currency')
67 public String currencyRecord; //lookup
68
69 @InvocableVariable(label='Custom Form' description='Custom Form')
70 public String customForm; //lookup
71
72 @InvocableVariable(label='Department' description='Department')
73 public String department; //lookup
74
75 @InvocableVariable(label='Location' description='Location')
76 public String location; //lookup
77
78 @InvocableVariable(label='Discount Item' description='Discount Item')
79 public String discountItem; //lookup
80
81 @InvocableVariable(label='Discount Rate' description='Discount Rate')
82 public String discountRate;
83
84 @InvocableVariable(label='Discount Total' description='Discount Total')
85 public Double discountTotal;
86
87 @InvocableVariable(label='Entity' description='Entity')
88 public String entity;//lookup
89
90 @InvocableVariable(label='Exchange Rate' description='Exchange Rate')
91 public Double exchangeRate;
92
93 @InvocableVariable(label='External Id' description='External Id')
94 public String externalId;
95
96 @InvocableVariable(label='Handling Cost' description='Handling Cost')
97 public Double handlingCost;
98
99 @InvocableVariable(label='Memo' description='Memo')
100 public String memo;
101
102 @InvocableVariable(label='Other Ref Num Id' description='Other Ref Num Id')
103 public String otherRefNum;
104
105 @InvocableVariable(label='Sales Rep' description='Sales Rep')
106 public String salesRep;
107
108 @InvocableVariable(label='Shipping Cost' description='Shipping Cost')
109 public Double shippingCost;
110
111 @InvocableVariable(label='Status' description='Status')
112 public String status;
113
114 @InvocableVariable(label='Sub Total Cost' description='Sub Total Cost')
115 public Double subTotal;
116
117 @InvocableVariable(label='Subsidiary' description='Subsidiary')
118 public String subsidiary;
119
120 @InvocableVariable(label='Tax Total' description='Tax Total')
121 public Double taxTotal;
122
123 @InvocableVariable(label='Total' description='Total')
124 public Double total;
125
126 @InvocableVariable(label='Tran Date' description='Tran Date')
127 public DateTime tranDate;
128
129 @InvocableVariable(label='Tran Id' description='Tran Id')
130 public String tranId;
131
132 //Sales Order Line items
133 @InvocableVariable(label='Sales Order Line Items' description='Sales Order Line Items')
134 public List<SObject> soLines;
135
136 @InvocableVariable(label='Line Description' description='Line Description')
137 public String lineDescription;
138
139 @InvocableVariable(label='Line Unit Amount' description='Line Unit Amount')
140 public String lineUnitAmount;
141
142 @InvocableVariable(label='Line Quantity' description='Line Quantity')
143 public String lineQuantity;
144
145 @InvocableVariable(label='Line Department' description='Line Department')
146 public String lineDepartment; //lookup
147
148 @InvocableVariable(label='Line Gross Amt' description='Line Gross Amt')
149 public String GrossAmt;
150
151 @InvocableVariable(label='Line Item' description='Line Item')
152 public String Items;
153
154 @InvocableVariable(label='Line Number' description='Line Number')
155 public String Line;
156
157 @InvocableVariable(label='Line Location' description='Line Location')
158 public String lineLocation;
159
160 @InvocableVariable(label='Line Price' description='Line Price')
161 public String Price;
162
163 @InvocableVariable(label='Line Rate' description='Line Rate')
164 public String Rate;
165
166 @InvocableVariable(label='Line RevRec EndDate' description='Line RevRec EndDate')
167 public String RevRecEndDate;
168
169 @InvocableVariable(label='Line RevRec Schedule' description='Line RevRec Schedule')
170 public String RevRecSchedule;
171
172 @InvocableVariable(label='Line RevRec StartDate' description='Line RevRec StartDate')
173 public String RevRecStartDate;
174
175 @InvocableVariable(label='Line Tax1 Amt' description=' Line Tax1 Amt')
176 public String Tax1Amt;
177
178 @InvocableVariable(label='Line Tax Code' description='Line Tax Code')
179 public String TaxCode;
180
181 @InvocableVariable(label='Line Tax Rate1' description='Line Tax Rate1')
182 public String TaxRate1;
183
184 }
185 public class Response{
186 @InvocableVariable(label='status Code' description='Status of the request')
187 public string statusCode;
188
189 @InvocableVariable(label='SF Record Id' description='SF Record Id')
190 public Id recordId;
191
192 @InvocableVariable(label='Error' description='Error')
193 public string error;
194 }
195
196 public static Map<String, Object> generateNetSuiteSalesOrderMap(Requests req){
197 Map<String, Object> netSuiteSalesOrderMap = new Map<String, Object>();
198 // Assign values from the Request to NetSuite Sales Order
199
200 If(req.classification != NULL)
201 netSuiteSalesOrderMap.put('classification', new Map<String, Object>{'internalId'=>req.classification});
202
203 If(req.currencyRecord != NULL)
204 netSuiteSalesOrderMap.put('currencyRecord', new Map<String, Object>{'internalId'=>req.currencyRecord});
205
206 If(req.customForm != NULL)
207 netSuiteSalesOrderMap.put('customForm', new Map<String, Object>{'internalId'=>req.customForm});
208
209 If(req.createdFrom != NULL)
210 netSuiteSalesOrderMap.put('createdFrom', new Map<String, Object>{'internalId'=>req.createdFrom});
211
212 If(req.department != null)
213 netSuiteSalesOrderMap.put('department', new Map<String, Object>{'internalId'=>req.department});
214
215 If(req.location != null)
216 netSuiteSalesOrderMap.put('location', new Map<String, Object>{'internalId'=>req.location});
217
218 If(req.discountItem != NULL)
219 netSuiteSalesOrderMap.put('discountItem', new Map<String, Object>{'internalId'=>req.discountItem});
220
221 If(req.discountRate != NULL)
222 netSuiteSalesOrderMap.put('discountRate',req.discountRate);
223
224 If(req.discountTotal != NULL)
225 netSuiteSalesOrderMap.put('discountTotal',req.discountTotal);
226
227 If(req.entity != NULL)
228 netSuiteSalesOrderMap.put('entity', new Map<String, Object>{'internalId'=>req.entity});
229
230 If(req.exchangeRate != NULL)
231 netSuiteSalesOrderMap.put('exchangeRate',req.exchangeRate);
232
233 If(req.externalId != NULL)
234 netSuiteSalesOrderMap.put('externalId',req.externalId);
235
236 If(req.handlingCost != NULL)
237 netSuiteSalesOrderMap.put('handlingCost',req.handlingCost);
238
239 If(req.memo != NULL)
240 netSuiteSalesOrderMap.put('memo',req.memo);
241
242 If(req.otherRefNum != NULL)
243 netSuiteSalesOrderMap.put('otherRefNum',req.otherRefNum);
244
245 If(req.salesRep != NULL)
246 netSuiteSalesOrderMap.put('salesRep', new Map<String, Object>{'internalId'=>req.salesRep});
247
248 If(req.shippingCost != NULL)
249 netSuiteSalesOrderMap.put('shippingCost',req.shippingCost);
250
251 If(req.status != NULL)
252 netSuiteSalesOrderMap.put('status',req.status);
253
254 If(req.subTotal != NULL)
255 netSuiteSalesOrderMap.put('subTotal',req.subTotal);
256
257 If(req.subsidiary != NULL)
258 netSuiteSalesOrderMap.put('subsidiary', new Map<String, Object>{'internalId'=>req.subsidiary});
259
260 If(req.taxTotal != NULL)
261 netSuiteSalesOrderMap.put('taxTotal',req.taxTotal);
262
263 If(req.total != NULL)
264 netSuiteSalesOrderMap.put('total',req.total);
265
266 if(req.tranDate != null){
267 netSuiteSalesOrderMap.put('tranDate',req.tranDate.getTime()/1000);
268 }
269 If(req.tranId != NULL)
270 netSuiteSalesOrderMap.put('tranId',req.tranId);
271
272 if(!(req.soLines).isEmpty()){
273 // Initializing the Sales Order Line Items
274 List<Object> lineItemList = new List<Object>();
275 for(SObject sobj : req.soLines){
276 Map<String, Object> lineItemMap = new Map<String, Object>();
277 if(req.lineDescription != null){
278 lineItemMap.put('description', sobj.get(req.lineDescription));
279 }
280 if(req.lineUnitAmount != null){
281 lineItemMap.put('amount', sobj.get(req.lineUnitAmount));
282 }
283 if(req.lineQuantity != null){
284 lineItemMap.put('quantity', sobj.get(req.lineQuantity));
285 }
286 if(req.lineDepartment != null)
287 lineItemMap.put('department' , new Map<String, Object>{'internalId' => sobj.get(req.lineDepartment)});
288
289 if(req.GrossAmt != null)
290 lineItemMap.put('grossAmt', sobj.get(req.GrossAmt));
291
292 if(req.Items != null)
293 lineItemMap.put('item' , new Map<String, Object>{'internalId' => sobj.get(req.Items)});
294
295 if(req.Line != null)
296 lineItemMap.put('line', sobj.get(req.Line));
297
298 if(req.lineLocation != null)
299 lineItemMap.put('location' , new Map<String, Object>{'internalId' => sobj.get(req.lineLocation)});
300
301 if(req.Price != null)
302 lineItemMap.put('price' , new Map<String, Object>{'internalId' => sobj.get(req.Price)});
303
304 if(req.Rate != null)
305 lineItemMap.put('rate', String.valueOf(sobj.get(req.Rate)));
306
307 if(req.RevRecStartDate != null){
308 DateTime getStartDate = DateTime.valueOf(sobj.get(req.RevRecStartDate));
309 lineItemMap.put('revRecStartDate', getStartDate.getTime()/1000);
310 }
311
312 if(req.RevRecEndDate != null){
313 DateTime getEndDate = DateTime.valueOf(sobj.get(req.RevRecEndDate));
314 lineItemMap.put('revRecEndDate', getEndDate.getTime()/1000);
315 }
316
317 if(req.RevRecSchedule != null)
318 lineItemMap.put('revRecSchedule', new Map<String, Object>{'internalId' => sobj.get(req.RevRecSchedule)});
319
320 if(req.Tax1Amt != null)
321 lineItemMap.put('tax1Amt', sobj.get(req.Tax1Amt));
322
323 if(req.TaxCode != null)
324 lineItemMap.put('taxCode' , new Map<String, Object>{'internalId' => sobj.get(req.TaxCode)});
325
326 if(req.TaxRate1 != null)
327 lineItemMap.put('taxRate1', sobj.get(req.TaxRate1));
328
329 lineItemList.add(lineItemMap);
330 }
331 // Adding the Line Items to Sales Order
332 Map<String, Object> LineItemObjMap = new Map<String, Object>();
333 LineItemObjMap .put('item',lineItemList);
334 netSuiteSalesOrderMap.put('itemList',LineItemObjMap );
335 }
336 return netSuiteSalesOrderMap;
337 }
338 }
Updated 03 Mar 2023
Did this page help you?