1// Start Apex Class
2public with sharing class createNSContactUsingFuture {
3 public createNSContactUsingFuture() {
4 }
5
6@InvocableMethod(label='Create NetSuite Contact' description='Create NetSuite Contact' category='Breadwinner')
7public static void createNetSuiteContactUsingFuture(List<Requests> requestList){
8 createNetSuiteContact(JSON.serialize(requestList));
9 }
10
11 @future(callout = true)
12public static void createNetSuiteContact(String requestListJSON){
13List<Requests> requestList = (List<Requests>)JSON.deserialize(requestListJSON, List<Requests>.class);
14 List<Object> netSuiteContactList = new List<Object>();
15
16 for(Requests req : requestList){
17 netSuiteContactList.add(generateNetSuiteContactMap(req));
18 }
19 // Sending request to NetSuite via exposed Global API
20 Map<String, Object> reqJSONMap = new Map<String, Object>();
21 reqJSONMap.put('contacts' , netSuiteContactList);
22 String reqJSON = JSON.serialize(reqJSONMap);
23
24 Map<String, Object> finalReqMap = new Map<String, Object>();
25 finalReqMap.put('version' , '1.0');
26 finalReqMap.put('action' , 'createContact');
27 finalReqMap.put('RequestJSON' , reqJSON);
28 System.debug('Request ::: ' + finalReqMap);
29
30 Map<String, Object> respMap = breadwinner_ns.BreadwinnerNetSuiteAPI.call(finalReqMap);
31 System.debug('Response ::: ' + respMap);
32 }
33
34 public class Requests{
35 @InvocableVariable(label='Alternate Email' description='Alternate Email')
36 public String altEmail;
37
38 @InvocableVariable(label='Assistant' description='Assistant')
39 public String assistant;
40
41 @InvocableVariable(label='Comments' description='Comments')
42 public String comments;
43
44 @InvocableVariable(label='Billing Street' description='Billing Street')
45 public String BillingStreet;
46
47 @InvocableVariable(label='Shipping Street' description='Shipping Street')
48 public String ShippingStreet;
49
50 @InvocableVariable(label='Billing Country' description='Billing Country')
51 public String BillingCountry;
52
53 @InvocableVariable(label='Billing Postal Code' description='Billing Postal Code')
54 public String BillingPostalCode;
55
56 @InvocableVariable(label='Billing City' description='Billing City')
57 public String BillingCity;
58
59 @InvocableVariable(label='Billing State' description='Billing State')
60 public String BillingState;
61
62 @InvocableVariable(label='Shipping Country' description='Shipping Country')
63 public String ShippingCountry;
64
65 @InvocableVariable(label='Shipping Postal Code' description='Shipping Postal Code')
66 public String ShippingPostalCode;
67
68 @InvocableVariable(label='Shipping City' description='Shipping City')
69 public String ShippingCity;
70
71 @InvocableVariable(label='Shipping State' description='Shipping State')
72 public String ShippingState;
73
74 @InvocableVariable(label='Company' description='Company')
75 public String company;
76
77 @InvocableVariable(label='Custom Form' description='Custom Form')
78 public String customForm;
79
80 @InvocableVariable(label='Email' description='Email')
81 public String email;
82
83 @InvocableVariable(label='External Id' description='External Id')
84 public String externalId;
85
86 @InvocableVariable(label='Fax' description='Fax')
87 public String fax;
88
89 @InvocableVariable(label='First Name' description='First Name')
90 public String firstName;
91
92 @InvocableVariable(label='Home Phone' description='Home Phone')
93 public String homePhone;
94
95 @InvocableVariable(label='internalId' description='internalId')
96 public String internalId;
97 @InvocableVariable(label='is Inactive' description='Is Inactive')
98 public Boolean isInactive;
99
100 @InvocableVariable(label='Is Private' description='Is Private')
101 public Boolean isPrivate;
102
103 @InvocableVariable(label='Last Name' description='Last Name')
104 public String lastName;
105
106 @InvocableVariable(label='Middle Name' description='Middle Name')
107 public String middleName;
108
109 @InvocableVariable(label='Mobile Phone' description='Mobile Phone')
110 public String mobilePhone;
111
112 @InvocableVariable(label='Office Phone' description='Office Phone')
113 public String officePhone;
114
115 @InvocableVariable(label='Phone' description='Phone')
116 public String phone;
117
118 @InvocableVariable(label='salutation' description='salutation')
119 public String salutation;
120
121 @InvocableVariable(label='Subsidiary' description='Subsidiary')
122 public String subsidiary;
123
124 @InvocableVariable(label='Title' description='Title')
125 public String title;
126
127 }
128
129public static Map<String, Object> generateNetSuiteContactMap(Requests req){
130 Map<String, Object> netSuiteContactMap = new Map<String, Object>();
131 // Assign values from the Request to NetSuite Contact
132 List<Object> addressBookList = new List<Object>();
133 Map<String, Object> billingAddressBook = new Map<String, Object>();
134 Map<String, Object> billingAddressbookAddress = new Map<String, Object>();
135
136 If(req.BillingCity != NULL && String.isNotBlank(req.BillingCity))
137 billingAddressbookAddress.put('city' , req.BillingCity);
138
139 If(req.BillingState != NULL && String.isNotBlank(req.BillingState))
140 billingAddressbookAddress.put('state' , req.BillingState);
141
142 If(req.BillingCountry != NULL && String.isNotBlank(req.BillingCountry))
143 billingAddressbookAddress.put('country' ,req.BillingCountry);
144
145 If(req.BillingPostalCode != NULL && String.isNotBlank(req.BillingPostalCode))
146 billingAddressbookAddress.put('zip' , req.BillingPostalCode);
147
148 billingAddressBook.put('defaultBilling' , true);
149 billingAddressBook.put('isResidential' , true);
150
151 If(req.BillingStreet != NULL && String.isNotBlank(req.BillingStreet)){
152 String billingAddr = String.isNotBlank(req.BillingStreet) ? req.BillingStreet : '';
153 String billingAddr1 = '';
154 String billingAddr2 = '';
155 String billingAddr3 = '';
156
157 if(billingAddr.length() >= 0 && billingAddr.length() <= 150){
158 billingAddr1 = billingAddr.substring(0,billingAddr.length());
159 }else if(billingAddr.length() > 150 && billingAddr.length() <= 300){
160 billingAddr1 = billingAddr.substring(0,150);
161 billingAddr2 = billingAddr.substring(150,billingAddr.length());
162 }else if(billingAddr.length() > 300 && billingAddr.length() <= 450){
163 billingAddr1 = billingAddr.substring(0,150);
164 billingAddr2 = billingAddr.substring(150,300);
165 billingAddr3 = billingAddr.substring(300,billingAddr.length());
166 }else if(billingAddr.length() > 450){
167 billingAddr1 = billingAddr.substring(0,150);
168 billingAddr2 = billingAddr.substring(150,300);
169 billingAddr3 = billingAddr.substring(300,450);
170 }
171 billingAddressbookAddress.put('addr1' , billingAddr1);
172 billingAddressbookAddress.put('addr2' , billingAddr2);
173 billingAddressbookAddress.put('addr3' , billingAddr3);
174 }
175
176 billingAddressBook.put('addressbookAddress', billingAddressbookAddress);
177 addressBookList.add(billingAddressBook);
178
179 Map<String, Object> shippingAddressBookAddress = new Map<String, Object>();
180
181 If(req.ShippingCity != NULL && String.isNotBlank(req.ShippingCity))
182 shippingAddressBookAddress.put('city' , req.ShippingCity);
183
184 If(req.ShippingState != NULL && String.isNotBlank(req.ShippingState))
185 shippingAddressBookAddress.put('state' , req.ShippingState);
186
187 If(req.ShippingCountry != NULL && String.isNotBlank(req.ShippingCountry))
188 shippingAddressBookAddress.put('country' ,req.ShippingCountry);
189
190 If(req.ShippingPostalCode != NULL && String.isNotBlank(req.ShippingPostalCode))
191 shippingAddressBookAddress.put('zip' , req.ShippingPostalCode);
192
193 Map<String, Object> ShippingAddressBook = new Map<String, Object>();
194 ShippingAddressBook.put('defaultShipping' , true);
195 ShippingAddressBook.put('isResidential' , true);
196
197 If(req.ShippingStreet != NULL && String.isNotBlank(req.ShippingStreet)){
198 String shippingAddr = String.isNotBlank(req.ShippingStreet) ? req.ShippingStreet : '';
199 String shippingAddr1 = '';
200 String shippingAddr2 = '';
201 String shippingAddr3 = '';
202 if(shippingAddr.length() >= 0 && shippingAddr.length() <= 150){
203 shippingAddr1 = shippingAddr.substring(0,shippingAddr.length());
204 }else if(shippingAddr.length() > 150 && shippingAddr.length() <= 300){
205 shippingAddr1 = shippingAddr.substring(0,150);
206 shippingAddr2 = shippingAddr.substring(150,shippingAddr.length());
207 }else if(shippingAddr.length() > 300 && shippingAddr.length() <= 450){
208 shippingAddr1 = shippingAddr.substring(0,150);
209 shippingAddr2 = shippingAddr.substring(150,300);
210 shippingAddr3 = shippingAddr.substring(300,shippingAddr.length());
211 }else if(shippingAddr.length() > 450){
212 shippingAddr1 = shippingAddr.substring(0,150);
213 shippingAddr2 = shippingAddr.substring(150,300);
214 shippingAddr3 = shippingAddr.substring(300,450);
215 }
216 shippingAddressBookAddress.put('addr1' , shippingAddr1);
217 shippingAddressBookAddress.put('addr2' , shippingAddr2);
218 shippingAddressBookAddress.put('addr3' , shippingAddr3);
219 }
220
221 ShippingAddressBook.put('addressbookAddress', shippingAddressBookAddress);
222 addressBookList.add(ShippingAddressBook);
223
224 // Adding Shipping and Billing Address List to Map
225netSuiteContactMap.put('addressbookList',new Map<String,Object>{'addressbook'=>addressBookList});
226
227 If(req.altEmail != NULL)
228 netSuiteContactMap.put('altEmail',req.altEmail);
229
230 If(req.assistant != NULL)
231 netSuiteContactMap.put('assistant',req.assistant);
232
233 If(req.comments != NULL)
234 netSuiteContactMap.put('comments',req.comments);
235
236 If(req.company != NULL)
237netSuiteContactMap.put('company', new Map<String, Object>{'internalId'=>req.company});
238
239 If(req.customForm != NULL)
240netSuiteContactMap.put('customForm' ,new Map<String, Object>{'internalId' => req.customForm});
241
242 If(req.email != NULL)
243 netSuiteContactMap.put('email',req.email);
244
245 If(req.externalID != NULL)
246 netSuiteContactMap.put('externalID',req.externalID);
247
248 If(req.fax != NULL)
249 netSuiteContactMap.put('fax',req.fax);
250
251 If(req.firstName != NULL)
252 netSuiteContactMap.put('firstName',req.firstName);
253
254 If(req.homePhone != NULL)
255 netSuiteContactMap.put('homePhone',req.homePhone);
256
257 If(req.isInactive != NULL)
258 netSuiteContactMap.put('isInactive',req.isInactive);
259
260 If(req.isPrivate != NULL)
261 netSuiteContactMap.put('isPrivate',req.isPrivate);
262
263 If(req.lastName != NULL)
264 netSuiteContactMap.put('lastName',req.lastName);
265
266 If(req.middleName != NULL)
267 netSuiteContactMap.put('middleName',req.middleName);
268
269 If(req.mobilePhone != NULL)
270 netSuiteContactMap.put('mobilePhone',req.mobilePhone);
271
272 If(req.officePhone != NULL)
273 netSuiteContactMap.put('officePhone',req.officePhone);
274
275 If(req.phone != NULL)
276 netSuiteContactMap.put('phone',req.phone);
277
278 If(req.salutation != NULL)
279 netSuiteContactMap.put('salutation',req.salutation);
280
281 If(req.subsidiary != NULL)
282netSuiteContactMap.put('subsidiary', new Map<String, Object>{'internalId'=>req.subsidiary});
283
284 If(req.title != NULL)
285 netSuiteContactMap.put('title',req.title);
286
287 return netSuiteContactMap;
288 }
289}