Create
1 min
To create a Purchase Order in NetSuite, use action as "createPurchaseOrder" and pass the Purchase Order data in the requestJSON under "purchaseOrders".
Note: You can only create one record at a time.
The following is an example of create Purchase Order where we are setting every possible field (See Purchase Order for the available fields). The response will return the response back from NetSuite, which includes the new NetSuite Purchase Order Id (internalId), plus the newly created Salesforce Purchase Order Id (salesforceID)
// Create a Map to store all the data of a Purchase Order
Map<String, Object> nsPurchaseorder = new Map<String, Object>();
nsPurchaseorder.put('classification', new Map<String, Object>{'internalId'=>'5'});
nsPurchaseorder.put('currencyRecord', new Map<String, Object>{'internalId'=>'1'});
nsPurchaseorder.put('department', new Map<String, Object>{'internalId'=>'2'});
nsPurchaseorder.put('dueDate', DateTime.newInstance(2020, 12, 31).getTime()/1000);
nsPurchaseorder.put('entity', new Map<String, Object>{'internalId'=>'30440'});
nsPurchaseorder.put('shipTo', new Map<String, Object>{'internalId'=>'362'});
nsPurchaseorder.put('memo', 'PO 12');
nsPurchaseorder.put('otherRefNum', '4343399');
nsPurchaseorder.put('tranDate', DateTime.newInstance(2020, 09, 07).getTime()/1000);
// Initializing the Purchase Order Custom Fields
List<Object> poCustomFieldList = new List<Object>();
Map<String, Object> poCustomField = new Map<String, Object>();
poCustomField.put('fieldType', 'select');
poCustomField.put('scriptId', 'custbody_customlist');
poCustomField.put('valueLookup', new Map<String, Object>{'internalId'=>'2'});
poCustomFieldList.add(poCustomField);
// Adding the Purchase Order Custom Fields
nsPurchaseorder.put('customFieldList', new Map<String, Object>{'customField'=>poCustomFieldList});
// Initializing the Purchase Order Line Items
List<Object> lineItemList = new List<Object>();
Map<String, Object> lineItem = new Map<String, Object>();
lineItem.put('description', 'A weapon for shooting arrows');
lineItem.put('item', new Map<String, Object>{'internalId'=>'239'});
lineItem.put('quantity', 1);
lineItem.put('rate', '999');
// Initializing the Line Item custom fields
List<Object> liCustomFieldList = new List<Object>();
Map<String, Object> liCustomField = new Map<String, Object>();
liCustomField.put('fieldType', 'boolean');
liCustomField.put('scriptId', 'custcol_checkbox');
liCustomField.put('value', 'true');
liCustomFieldList.add(liCustomField);
// Adding the Purchase Order Line Item Custom Fields
lineItem.put('customFieldList', new Map<String, Object>{'customField'=>liCustomFieldList});
lineItemList.add(lineItem);
// Adding the Line Items to Purchase Order
Map<String, Object> poLineitemList = new Map<String, Object>();
poLineitemList.put('item', lineItemList);
nsPurchaseorder.put('itemList', poLineitemList);
// Initializing the RequestJSON Data to be passed.
Map<String, Object> requestJSONMap = new Map<String, Object>();
requestJSONMap.put('purchaseOrders', new List<Object>{nsPurchaseorder});
String reqJSON = JSON.serialize(requestJSONMap);
Map<String, Object> reqObj = new Map<String, Object>();
reqObj.put('version', '1.0');
reqObj.put('action', 'createPurchaseOrder');
reqObj.put('requestJSON', reqJSON);
// Placing a request to Breadwinner Global API
Map<String, Object> resp = breadwinner_ns.BreadwinnerNetSuiteAPI.call(reqObj);
System.debug(resp);