Adding Custom Fields
The code generated using the Apex generator doesn't include any custom fields, and we have to add the custom fields into the code manually.
This document is an example of creating a Sales Order using an Apex generator code. The procedure is the same for other transactions.
In this document, we will explain how to add two different types of custom fields. The first type is a primitive data field, which can include strings, booleans, dates, and date-times. The second type is for reference-related data, such as lookup fields that require passing an internal ID.
To add custom fields to the Apex Generator's code, follow these three steps.
1. Open the Custom Fields tab
Go to the Breadwinner for NetSuite tab, click on the Custom Fields tab, and select the Sales Order custom fields. Look for the NetSuite API Name (Script Id) and Datatype of the custom field you want to add.
2. Declare the Custom 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 field label, description, datatype, and variable name match your NetSuite custom field.
//Adding Custom Fields
@InvocableVariable(label='New Field Label' description='New Field Description')
public String newField;
@InvocableVariable(label='referenceFieldId Label' description='Reference Field Description')
public String referenceFieldId;The above code adds two custom fields to the Requests class.
3. Initialize the custom fields and Add them to the map
Custom fields should be initialized inside the "generateNetSuiteSalesOrderMap" method. To initialize the custom fields, paste the code snippet below in the "generateNetSuiteSalesOrderMap" method after initializing the variable "netSuiteSalesOrderMap" and edit them as required.
// Initialize the Custom Fields
List<Object> soCustomFieldList = new List<Object>();
if(req.newField != NULL) {
Map<String,Object> soCustomField1 = new Map<String,Object>();
soCustomField1.put('fieldType', 'string');//pass datatype correctly
//pass custom field (scriptId) correctly, it starts with custbody
soCustomField1.put('scriptId', 'YourNSCustomFieldAPIName');
soCustomField1.put('value', req.newField);
soCustomFieldList.add(soCustomField1);
}
if(req.referenceFieldId != NULL) {
Map<String,Object> soCustomField2 = new Map<String,Object>();
soCustomField2.put('fieldType', 'select');
//pass custom field (scriptId) correctly, it starts with custbody
soCustomField2.put('scriptId', 'YourNSCustomFieldAPIName');
soCustomField2.put('valueLookup', new Map<String,Object>{'internalId'=>req.referenceFieldId});
soCustomFieldList.add(soCustomField2);
}
// Finally Add the Custom Fields to the "netSuiteSalesOrderMap"
netSuiteSalesOrderMap.put('customFieldList', new Map<String,Object>{'customField'=>soCustomFieldList});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 Custom Fields
@InvocableVariable(label='New Field Label' description='New Field Description')
public String newField;
@InvocableVariable(label='referenceFieldId Label' description='Reference Field Description')
public String referenceFieldId;
}
public static Map<String, Object> generateNetSuiteSalesOrderMap(Requests req){
Map<String, Object> netSuiteSalesOrderMap = new Map<String, Object>();
// Initialize the Custom Fields
List<Object> soCustomFieldList = new List<Object>();
if(req.newField != NULL) {
Map<String,Object> soCustomField1 = new Map<String,Object>();
soCustomField1.put('fieldType', 'string');//pass datatype correctly
//pass custom field (scriptId) correctly, it starts with custbody
soCustomField1.put('scriptId', 'YourNSCustomFieldAPIName');
soCustomField1.put('value', req.newField);
soCustomFieldList.add(soCustomField1);
}
if(req.referenceFieldId != NULL) {
Map<String,Object> soCustomField2 = new Map<String,Object>();
soCustomField2.put('fieldType', 'select');
//pass custom field (scriptId) correctly, it starts with custbody
soCustomField2.put('scriptId', 'YourNSCustomFieldAPIName');
soCustomField2.put('valueLookup', new Map<String,Object>{'internalId'=>req.referenceFieldId});
soCustomFieldList.add(soCustomField2);
}
// Finally Add the Custom Fields to the "netSuiteSalesOrderMap"
netSuiteSalesOrderMap.put('customFieldList', new Map<String,Object>{'customField'=>soCustomFieldList});
// Assign values from the Request to NetSuite Sales Order
// Initialize the Standard Fields
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;
}
}To learn more about custom fields, check this doc: NetSuite Custom Fields