Global API
...
Custom Object
Create Custom Object with chil...
Complete Parent and Child record creation
1min
Below is an example of creating a Parent and Child record in one action.
Request
// Create a Map to store all the data of a Custom Object
Map<String, Object> nsCustomObject = new Map<String, Object>();
nsCustomObject.put('name', 'Parent Object Record');
nsCustomObject.put('isInactive', false);
// Initializing the Custom Object Custom Fields
List<Object> customObjectCustomFieldList = new List<Object>();
Map<String,Object> customObjectCustomField1 = new Map<String,Object>();
customObjectCustomField1.put('fieldType', 'string');
customObjectCustomField1.put('scriptId', 'custrecordphone');
customObjectCustomField1.put('value', '9809876567');
customObjectCustomFieldList.add(customObjectCustomField1);
Map<String,Object> customObjectCustomField2 = new Map<String,Object>();
customObjectCustomField2.put('fieldType', 'double');
customObjectCustomField2.put('scriptId', 'custrecord6');
customObjectCustomField2.put('value', 90.0);
customObjectCustomFieldList.add(customObjectCustomField2);
// Adding the Custom Object Custom Fields
nsCustomObject.put('customFieldList', new Map<String,Object>{'customField'=>customObjectCustomFieldList});
// Initializing the RequestJSON Data to be passed.
Map<String, Object> requestJSONMap = new Map<String, Object>();
requestJSONMap.put('customObjectRecords', new List<Object>{nsCustomObject});
requestJSONMap.put('customObjectID', '17');
String reqJSON = JSON.serialize(requestJSONMap);
Map<String, Object> reqObj = new Map<String, Object>();
reqObj.put('version', '1.0');
reqObj.put('action', 'createCustomObjectRecord');
reqObj.put('skipDML', true);
reqObj.put('requestJSON', reqJSON);
// Placing a request to Breadwinner Global API
Map<String, Object> resp = breadwinner_ns.BreadwinnerNetSuiteAPI.call(reqObj);
System.debug(resp);
/*
We need to pass the internal id of the record created above as an attribute to create the child record.
We also want to check the status of the parent record to make sure it was successfully created.
*/
// Parse the "responseJSON" string into a map, "responseJSON" is a key from response
Map<String, Object> responseJSON = (Map<String, Object>)JSON.deserializeUntyped((String)resp.get('responseJSON'));
// Extract the "status" field from the parsed map
String status = (String)responseJSON.get('status');
// Extract the "customObjectRecords" list where internalId is present in the response
List<Object> customObjectRecords = (List<Object>)responseJSON.get('customObjectRecords');
// Check if the list is not empty and has at least one record
String parentInternalId;
if (!customObjectRecords.isEmpty()) {
// Extract the first record from the list
Map<String, Object> firstRecord = (Map<String, Object>)customObjectRecords[0];
// Extract the "internalId" field from the first record
parentInternalId = (String)firstRecord.get('internalId');
}
if(parentInternalId != null && status == '200'){
// Create a Map to store all the data of a Custom Object
Map<String, Object> nsCustomObject = new Map<String, Object>();
nsCustomObject.put('name', 'other object record');
nsCustomObject.put('isInactive', false);
// Initializing the Custom Object Custom Fields
List<Object> customObjectCustomFieldList = new List<Object>();
Map<String,Object> parentObjectLookupfield= new Map<String,Object>();
parentObjectLookupfield.put('fieldType', 'string');
parentObjectLookupfield.put('scriptId', 'custrecordpre0007');
parentObjectLookupfield.put('value', parentInternalId);
customObjectCustomFieldList.add(parentObjectLookupfield);
Map<String,Object> customObjectCustomField1 = new Map<String,Object>();
customObjectCustomField1.put('fieldType', 'string');
customObjectCustomField1.put('scriptId', 'custrecord194');
customObjectCustomField1.put('value', 'Auto mobiles');
customObjectCustomFieldList.add(customObjectCustomField1);
Map<String,Object> customObjectCustomField2 = new Map<String,Object>();
customObjectCustomField2.put('fieldType', 'string');
customObjectCustomField2.put('scriptId', 'custrecord193');
customObjectCustomField2.put('value', 'safari');
customObjectCustomFieldList.add(customObjectCustomField2);
// Adding the Custom Object Custom Fields
nsCustomObject.put('customFieldList', new Map<String,Object>{'customField'=>customObjectCustomFieldList});
// Initializing the RequestJSON Data to be passed.
Map<String, Object> requestJSONMap = new Map<String, Object>();
requestJSONMap.put('customObjectRecords', new List<Object>{nsCustomObject});
requestJSONMap.put('customObjectID', '12');
String reqJSON = JSON.serialize(requestJSONMap);
Map<String, Object> reqObj = new Map<String, Object>();
reqObj.put('version', '1.0');
reqObj.put('action', 'createCustomObjectRecord');
reqObj.put('requestJSON', reqJSON);
// Placing a request to Breadwinner Global API
Map<String, Object> resp = breadwinner_ns.BreadwinnerNetSuiteAPI.call(reqObj);
System.debug(resp);
}
else{
List<Object> errors = (List<Object>)responseJSON.get('errors');
system.debug(errors);
}
Updated 03 May 2024
Did this page help you?