Adding Standard Fields
In this document, we will explain how to add any new Standard Field to the Apex Generator code. This is an example of creating a Sales Order using an Apex generator code. The procedure is the same for other transactions. In the below example, we will explain how to add the standard field called terms.
Follow these two steps to add standard fields to the Apex Generator's code.
1. Know the Standard Fields
Go to the respective Global API request object. Look for the NetSuite API Name and Datatype of the standard field you want to add.
Example: Standard fields supported by the Sales Order
2. Declare the Standard Fields
Fields are declared/added inside the "Requests" class. So, navigate to the "Requests" class, paste the below code snippet inside the "Requests" class, and edit it as per the requirement. Ensure that the field label, description, datatype, and variable name match your NetSuite Standard field.
//Adding Custom Fields
@InvocableVariable(label='Terms' description='Terms')
public String terms; //lookup3. Initialize the standard fields and Add them to the map
Standard fields should be initialized inside the "generateNetSuiteSalesOrderMap" method. To initialize the standard fields, paste the code snippet below in the "generateNetSuiteSalesOrderMap" method after initializing the variable "netSuiteSalesOrderMap" and edit them as required.
Note: Terms is a Reference Field in NetSuite, so we need to pass it's internalId
// Initialize the standard Fields
If(req.terms!= NULL)
netSuiteSalesOrderMap.put('terms', new Map<String, Object>{'internalId'=>req.terms});Note: The complete example is given below, and for the sake of code readability and length, a few fields that would auto-populate by the Apex generator are removed here
// Start Apex Class
public with sharing class createNSSalesOrderUsingFuture {
public createNSSalesOrderUsingFuture() {
}
@InvocableMethod(label='Create NetSuite Sales Order' description='Create NetSuite Sales Order')
public static void createNetSuiteSalesOrderUsingFuture(List<Requests> requestList){
createNetSuiteSalesOrder(JSON.serialize(requestList));
}
@future(callout = true)
public static void createNetSuiteSalesOrder(String requestListJSON){
List<Requests> requestList = (List<Requests>)JSON.deserialize(requestListJSON, List<Requests>.class);
List<Object> netSuiteSalesOrderList = new List<Object>();
for(Requests req : requestList){
netSuiteSalesOrderList.add(generateNetSuiteSalesOrderMap(req));
}
// Sending request to NetSuite via exposed Global API
Map<String, Object> reqJSONMap = new Map<String, Object>();
reqJSONMap.put('salesOrders' , netSuiteSalesOrderList);
String reqJSON = JSON.serialize(reqJSONMap);
Map<String, Object> finalReqMap = new Map<String, Object>();
finalReqMap.put('version' , '1.0');
finalReqMap.put('action' , 'createSalesOrder');
finalReqMap.put('RequestJSON' , reqJSON);
System.debug('Request ::: ' + finalReqMap);
Map<String, Object> respMap = breadwinner_ns.BreadwinnerNetSuiteAPI.call(finalReqMap);
System.debug('Response ::: ' + respMap);
}
public class Requests{
@InvocableVariable(label='Class' description='Class')
public String classification; //lookup
@InvocableVariable(label='Created From' description='Created From')
public String createdFrom; //lookup
@InvocableVariable(label='Currency' description='Currency')
public String currencyRecord; //lookup
@InvocableVariable(label='Custom Form' description='Custom Form')
public String customForm; //lookup
@InvocableVariable(label='Department' description='Department')
public String department; //lookup
@InvocableVariable(label='Discount Item' description='Discount Item')
public String discountItem; //lookup
//Adding standard Fields
@InvocableVariable(label='Terms' description='Terms')
public String terms;
}
public static Map<String, Object> generateNetSuiteSalesOrderMap(Requests req){
Map<String, Object> netSuiteSalesOrderMap = new Map<String, Object>();
// Assign values from the Request to NetSuite Sales Order
// Initialize the standard Fields
If(req.terms!= NULL)
netSuiteSalesOrderMap.put('terms', new Map<String, Object>{'internalId'=>req.terms});
If(req.classification != NULL)
netSuiteSalesOrderMap.put('classification', new Map<String, Object>{'internalId'=>req.classification});
If(req.currencyRecord != NULL)
netSuiteSalesOrderMap.put('currencyRecord', new Map<String, Object>{'internalId'=>req.currencyRecord});
If(req.customForm != NULL)
netSuiteSalesOrderMap.put('customForm', new Map<String, Object>{'internalId'=>req.customForm});
If(req.createdFrom != NULL)
netSuiteSalesOrderMap.put('createdFrom', new Map<String, Object>{'internalId'=>req.createdFrom});
If(req.department != null)
netSuiteSalesOrderMap.put('department', new Map<String, Object>{'internalId'=>req.department});
return netSuiteSalesOrderMap;
}
}