1// Start Apex Class
2public with sharing class createNSSalesOrderUsingFuture {
3 public createNSSalesOrderUsingFuture() {
4 }
5
6 @InvocableMethod(label='Create NetSuite Sales Order' description='Create NetSuite Sales Order')
7 public static void createNetSuiteSalesOrderUsingFuture(List<Requests> requestList){
8 createNetSuiteSalesOrder(JSON.serialize(requestList));
9 }
10
11 @future(callout = true)
12 public static void createNetSuiteSalesOrder(String requestListJSON){
13 List<Requests> requestList = (List<Requests>)JSON.deserialize(requestListJSON, List<Requests>.class);
14 List<Object> netSuiteSalesOrderList = new List<Object>();
15
16 for(Requests req : requestList){
17 netSuiteSalesOrderList.add(generateNetSuiteSalesOrderMap(req));
18 }
19 // Sending request to NetSuite via exposed Global API
20 Map<String, Object> reqJSONMap = new Map<String, Object>();
21 reqJSONMap.put('salesOrders' , netSuiteSalesOrderList);
22 String reqJSON = JSON.serialize(reqJSONMap);
23
24 Map<String, Object> finalReqMap = new Map<String, Object>();
25 finalReqMap.put('version' , '1.0');
26 finalReqMap.put('action' , 'createSalesOrder');
27 finalReqMap.put('RequestJSON' , reqJSON);
28 System.debug('Request ::: ' + finalReqMap);
29
30 Map<String, Object> respMap = breadwinner_ns.BreadwinnerNetSuiteAPI.call(finalReqMap);
31 System.debug('Response ::: ' + respMap);
32 }
33
34 public class Requests{
35 @InvocableVariable(label='Class' description='Class')
36 public String classification; //lookup
37
38 @InvocableVariable(label='Created From' description='Created From')
39 public String createdFrom; //lookup
40
41 @InvocableVariable(label='Currency' description='Currency')
42 public String currencyRecord; //lookup
43
44 @InvocableVariable(label='Custom Form' description='Custom Form')
45 public String customForm; //lookup
46
47 @InvocableVariable(label='Department' description='Department')
48 public String department; //lookup
49
50 @InvocableVariable(label='Discount Item' description='Discount Item')
51 public String discountItem; //lookup
52
53 //Adding Custom Fields
54 @InvocableVariable(label='New Field Label' description='New Field Description')
55 public String newField;
56
57 @InvocableVariable(label='referenceFieldId Label' description='Reference Field Description')
58 public String referenceFieldId;
59 }
60
61 public static Map<String, Object> generateNetSuiteSalesOrderMap(Requests req){
62 Map<String, Object> netSuiteSalesOrderMap = new Map<String, Object>();
63 // Initialize the Custom Fields
64 List<Object> soCustomFieldList = new List<Object>();
65 if(req.newField != NULL) {
66 Map<String,Object> soCustomField1 = new Map<String,Object>();
67 soCustomField1.put('fieldType', 'string');//pass datatype correctly
68 //pass custom field (scriptId) correctly, it starts with custbody
69 soCustomField1.put('scriptId', 'YourNSCustomFieldAPIName');
70 soCustomField1.put('value', req.newField);
71 soCustomFieldList.add(soCustomField1);
72 }
73 if(req.referenceFieldId != NULL) {
74 Map<String,Object> soCustomField2 = new Map<String,Object>();
75 soCustomField2.put('fieldType', 'select');
76 //pass custom field (scriptId) correctly, it starts with custbody
77 soCustomField2.put('scriptId', 'YourNSCustomFieldAPIName');
78 soCustomField2.put('valueLookup', new Map<String,Object>{'internalId'=>req.referenceFieldId});
79 soCustomFieldList.add(soCustomField2);
80 }
81 // Finally Add the Custom Fields to the "netSuiteSalesOrderMap"
82 netSuiteSalesOrderMap.put('customFieldList', new Map<String,Object>{'customField'=>soCustomFieldList});
83
84 // Assign values from the Request to NetSuite Sales Order
85 // Initialize the Standard Fields
86 If(req.classification != NULL)
87 netSuiteSalesOrderMap.put('classification', new Map<String, Object>{'internalId'=>req.classification});
88
89 If(req.currencyRecord != NULL)
90 netSuiteSalesOrderMap.put('currencyRecord', new Map<String, Object>{'internalId'=>req.currencyRecord});
91
92 If(req.customForm != NULL)
93 netSuiteSalesOrderMap.put('customForm', new Map<String, Object>{'internalId'=>req.customForm});
94
95 If(req.createdFrom != NULL)
96 netSuiteSalesOrderMap.put('createdFrom', new Map<String, Object>{'internalId'=>req.createdFrom});
97
98 If(req.department != null)
99 netSuiteSalesOrderMap.put('department', new Map<String, Object>{'internalId'=>req.department});
100
101 return netSuiteSalesOrderMap;
102 }
103}