1// Create a Map to store all the data of a Sales Order
2Map<String, Object> nsSalesOrder = new Map<String, Object>();
3nsSalesOrder.put('classification', new Map<String, Object>{'internalId'=>'5'});
4nsSalesOrder.put('currencyRecord', new Map<String, Object>{'internalId'=>'2'});
5nsSalesOrder.put('department', new Map<String, Object>{'internalId'=>'2'});
6nsSalesOrder.put('discountItem', new Map<String, Object>{'internalId'=>'-6'});
7nsSalesOrder.put('discountRate', '-15');
8nsSalesOrder.put('entity', new Map<String, Object>{'internalId'=>'362'});
9nsSalesOrder.put('memo', 'Order 25');
10nsSalesOrder.put('otherRefNum', '4343399');
11nsSalesOrder.put('tranDate', DateTime.newInstance(2020, 09, 07).getTime()/1000);
12
13// Initialize the Sales Order Custom Fields
14List<Object> soCustomFieldList = new List<Object>();
15Map<String,Object> soCustomField = new Map<String,Object>();
16soCustomField.put('fieldType', 'select');
17soCustomField.put('scriptId', 'custbody_customlist');
18soCustomField.put('valueLookup', new Map<String,Object>{'internalId'=>'2'});
19soCustomFieldList.add(soCustomField);
20
21// Add custom segment field
22Map<String,Object> soCustomField2 = new Map<String,Object>();
23soCustomField2.put('fieldType', 'select');
24soCustomField2.put('scriptId', 'cseg_lead_source');
25soCustomField2.put('valueLookup', new Map<String,Object>{'internalId'=>'3'});
26soCustomFieldList.add(soCustomField2);
27
28// Add the Sales Order Custom Fields
29nsSalesOrder.put('customFieldList', new Map<String,Object>{'customField'=>soCustomFieldList});
30
31// Initializing the Sales Order Line Items
32List<Object> lineItemList = new List<Object>();
33Map<String, Object> lineItem = new Map<String, Object>();
34lineItem.put('description', 'A weapon for shooting arrows');
35lineItem.put('item', new Map<String, Object>{'internalId'=>'239'});
36lineItem.put('quantity', 1);
37lineItem.put('price',new Map<String, Object>{'internalId'=>'-1'});
38lineItem.put('rate', '999');
39
40// Initialize the Line Item custom fields
41List<Object> liCustomFieldList = new List<Object>();
42Map<String, Object> liCustomField = new Map<String, Object>();
43liCustomField.put('fieldType', 'boolean');
44liCustomField.put('scriptId', 'custcol_checkbox');
45liCustomField.put('value', 'true');
46liCustomFieldList.add(liCustomField);
47
48// Add custom segment field
49Map<String,Object> liCustomField2 = new Map<String,Object>();
50liCustomField2.put('fieldType', 'select');
51liCustomField2.put('scriptId', 'cseg_lead_source');
52liCustomField2.put('valueLookup', new Map<String,Object>{'internalId'=>'3'});
53liCustomFieldList.add(liCustomField2);
54
55// Add the Sales Order Line Item Custom Fields
56lineItem.put('customFieldList', new Map<String, Object>{'customField'=>liCustomFieldList});
57lineItemList.add(lineItem);
58
59// Add the Line Items to Sales Order
60Map<String, Object> soLineitemList = new Map<String, Object>();
61soLineitemList.put('item', lineItemList);
62
63nsSalesOrder.put('itemList', soLineitemList);
64
65// Initialize the RequestJSON Data to be passed.
66Map<String, Object> requestJSONMap = new Map<String, Object>();
67requestJSONMap.put('salesOrders', new List<Object>{nsSalesOrder});
68
69String reqJSON = JSON.serialize(requestJSONMap);
70
71Map<String, Object> reqObj = new Map<String, Object>();
72reqObj.put('version', '1.0');
73reqObj.put('action', 'createSalesOrder');
74reqObj.put('requestJSON', reqJSON);
75
76// Place a request to Breadwinner Global API
77Map<String, Object> resp = breadwinner_ns.BreadwinnerNetSuiteAPI.call(reqObj);
78System.debug(resp);