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
1// Create a Map to store all the data of a Custom Object
2 Map<String, Object> nsCustomObject = new Map<String, Object>();
3 nsCustomObject.put('name', 'Parent Object Record');
4 nsCustomObject.put('isInactive', false);
5
6// Initializing the Custom Object Custom Fields
7 List<Object> customObjectCustomFieldList = new List<Object>();
8
9 Map<String,Object> customObjectCustomField1 = new Map<String,Object>();
10 customObjectCustomField1.put('fieldType', 'string');
11 customObjectCustomField1.put('scriptId', 'custrecordphone');
12 customObjectCustomField1.put('value', '9809876567');
13 customObjectCustomFieldList.add(customObjectCustomField1);
14
15 Map<String,Object> customObjectCustomField2 = new Map<String,Object>();
16 customObjectCustomField2.put('fieldType', 'double');
17 customObjectCustomField2.put('scriptId', 'custrecord6');
18 customObjectCustomField2.put('value', 90.0);
19 customObjectCustomFieldList.add(customObjectCustomField2);
20
21// Adding the Custom Object Custom Fields
22 nsCustomObject.put('customFieldList', new Map<String,Object>{'customField'=>customObjectCustomFieldList});
23
24// Initializing the RequestJSON Data to be passed.
25Map<String, Object> requestJSONMap = new Map<String, Object>();
26 requestJSONMap.put('customObjectRecords', new List<Object>{nsCustomObject});
27 requestJSONMap.put('customObjectID', '17');
28 String reqJSON = JSON.serialize(requestJSONMap);
29
30Map<String, Object> reqObj = new Map<String, Object>();
31 reqObj.put('version', '1.0');
32 reqObj.put('action', 'createCustomObjectRecord');
33 reqObj.put('skipDML', true);
34 reqObj.put('requestJSON', reqJSON);
35
36// Placing a request to Breadwinner Global API
37Map<String, Object> resp = breadwinner_ns.BreadwinnerNetSuiteAPI.call(reqObj);
38System.debug(resp);
39
40/*
41We need to pass the internal id of the record created above as an attribute to create the child record.
42We also want to check the status of the parent record to make sure it was successfully created.
43*/
44
45// Parse the "responseJSON" string into a map, "responseJSON" is a key from response
46Map<String, Object> responseJSON = (Map<String, Object>)JSON.deserializeUntyped((String)resp.get('responseJSON'));
47// Extract the "status" field from the parsed map
48String status = (String)responseJSON.get('status');
49
50// Extract the "customObjectRecords" list where internalId is present in the response
51List<Object> customObjectRecords = (List<Object>)responseJSON.get('customObjectRecords');
52
53// Check if the list is not empty and has at least one record
54String parentInternalId;
55if (!customObjectRecords.isEmpty()) {
56 // Extract the first record from the list
57 Map<String, Object> firstRecord = (Map<String, Object>)customObjectRecords[0];
58
59 // Extract the "internalId" field from the first record
60 parentInternalId = (String)firstRecord.get('internalId');
61}
62
63
64if(parentInternalId != null && status == '200'){
65 // Create a Map to store all the data of a Custom Object
66 Map<String, Object> nsCustomObject = new Map<String, Object>();
67 nsCustomObject.put('name', 'other object record');
68 nsCustomObject.put('isInactive', false);
69
70 // Initializing the Custom Object Custom Fields
71 List<Object> customObjectCustomFieldList = new List<Object>();
72
73 Map<String,Object> parentObjectLookupfield= new Map<String,Object>();
74 parentObjectLookupfield.put('fieldType', 'string');
75 parentObjectLookupfield.put('scriptId', 'custrecordpre0007');
76 parentObjectLookupfield.put('value', parentInternalId);
77 customObjectCustomFieldList.add(parentObjectLookupfield);
78
79 Map<String,Object> customObjectCustomField1 = new Map<String,Object>();
80 customObjectCustomField1.put('fieldType', 'string');
81 customObjectCustomField1.put('scriptId', 'custrecord194');
82 customObjectCustomField1.put('value', 'Auto mobiles');
83 customObjectCustomFieldList.add(customObjectCustomField1);
84
85 Map<String,Object> customObjectCustomField2 = new Map<String,Object>();
86 customObjectCustomField2.put('fieldType', 'string');
87 customObjectCustomField2.put('scriptId', 'custrecord193');
88 customObjectCustomField2.put('value', 'safari');
89 customObjectCustomFieldList.add(customObjectCustomField2);
90
91 // Adding the Custom Object Custom Fields
92 nsCustomObject.put('customFieldList', new Map<String,Object>{'customField'=>customObjectCustomFieldList});
93
94 // Initializing the RequestJSON Data to be passed.
95 Map<String, Object> requestJSONMap = new Map<String, Object>();
96 requestJSONMap.put('customObjectRecords', new List<Object>{nsCustomObject});
97 requestJSONMap.put('customObjectID', '12');
98 String reqJSON = JSON.serialize(requestJSONMap);
99
100 Map<String, Object> reqObj = new Map<String, Object>();
101 reqObj.put('version', '1.0');
102 reqObj.put('action', 'createCustomObjectRecord');
103 reqObj.put('requestJSON', reqJSON);
104
105 // Placing a request to Breadwinner Global API
106 Map<String, Object> resp = breadwinner_ns.BreadwinnerNetSuiteAPI.call(reqObj);
107 System.debug(resp);
108}
109else{
110 List<Object> errors = (List<Object>)responseJSON.get('errors');
111 system.debug(errors);
112}
113
Updated 03 May 2024
Did this page help you?