1public class BW_CreateNetSuiteCompany_Invokable {
2
3 @InvocableMethod(label='Create NetSuite Company' description='Creates NetSuite Companies invoked by Flow' category='Account')
4 // The main method to be implemented. The Flow calls this at run time.
5
6 public static List <Results> execute (List<Requests> requestList) {
7 // Get the user input from the flow
8 String testMode = requestList[0].testMode;
9 String firstName = requestList[0].firstName;
10 String lastName = requestList[0].lastName;
11 String companyName = requestList[0].companyName;
12 String accountNumber = requestList[0].AccountNumber; //accountNumber
13 String phone = requestList[0].Phone; //phone
14 String website = requestList[0].Website; //url
15 String billingStreet = requestList[0].BillingStreet;
16 String billingCity = requestList[0].BillingCity;
17 String billingState = requestList[0].BillingState;
18 String billingPostalCode = requestList[0].BillingPostalCode;
19 String shippingStreet = requestList[0].ShippingStreet;
20 String shippingCity = requestList[0].ShippingCity;
21 String shippingState = requestList[0].ShippingState;
22 String shippingPostalCode = requestList[0].ShippingPostalCode;
23 String subsidiaryId = requestList[0].SubsidiaryID;
24 //String taxNumber = requestList[0].taxNumber; //Example of custom Field SF: ncf_entity_tax_reg_no__c NS: custentity_tax_reg_no
25
26 //Create a Results object to hold the return values
27 Results response = new Results();
28
29 if (testMode == 'sendtonetsuite') { //using the testMode parameter filled with 'sendtonetsuite' is required for this class, as a security feature. You may want to remove this for production deploy.
30 try{
31 List<Object> netSuiteCustomerList = new List<Object>();
32
33 Map<String, Object> netSuiteCustomerMap = new Map<String, Object>();
34
35 netSuiteCustomerMap.put('subsidiary', new Map<String, Object>{'internalId'=>'8'}); //subsidiaryId);
36 //Assign value of Account to NetSuite Customer
37 netSuiteCustomerMap.put('accountNumber',accountNumber);
38 netSuiteCustomerMap.put('companyName',companyName);
39 netSuiteCustomerMap.put('phone',phone);
40 netSuiteCustomerMap.put('url',website);
41 //netSuiteCustomerMap.put('custentity_tax_reg_no',taxNumber); //Custom Field example
42
43 String billingAddr = String.isNotBlank(billingStreet) ? billingStreet : '';
44 String billingAddr1 = '';
45 String billingAddr2 = '';
46 String billingAddr3 = '';
47 if(billingAddr.length() >= 0 && billingAddr.length() <= 150){
48 billingAddr1 = billingAddr.substring(0,billingAddr.length());
49 }else if(billingAddr.length() > 150 && billingAddr.length() <= 300){
50 billingAddr1 = billingAddr.substring(0,150);
51 billingAddr2 = billingAddr.substring(150,billingAddr.length());
52 }else if(billingAddr.length() > 300 && billingAddr.length() <= 450){
53 billingAddr1 = billingAddr.substring(0,150);
54 billingAddr2 = billingAddr.substring(150,300);
55 billingAddr3 = billingAddr.substring(300,billingAddr.length());
56 }else if(billingAddr.length() > 450){
57 billingAddr1 = billingAddr.substring(0,150);
58 billingAddr2 = billingAddr.substring(150,300);
59 billingAddr3 = billingAddr.substring(300,450);
60 }
61
62 Map<String, Object> billingAddrFieldMap = new Map<String, Object>();
63 billingAddrFieldMap.put('addr1' , billingAddr1);
64 billingAddrFieldMap.put('addr2' , billingAddr2);
65 billingAddrFieldMap.put('addr3' , billingAddr3);
66 billingAddrFieldMap.put('city' , billingCity);
67 billingAddrFieldMap.put('state' , billingState);
68 billingAddrFieldMap.put('zip' , billingPostalCode);
69
70 Map<String, Object> abaAddressForBillingMap = new Map<String, Object>();
71 abaAddressForBillingMap.put('addressbookAddress' , billingAddrFieldMap);
72
73 String shippingAddr = String.isNotBlank(shippingStreet) ? shippingStreet : '';
74 String shippingAddr1 = '';
75 String shippingAddr2 = '';
76 String shippingAddr3 = '';
77 if(shippingAddr.length() >= 0 && shippingAddr.length() <= 150){
78 shippingAddr1 = shippingAddr.substring(0,shippingAddr.length());
79 }else if(shippingAddr.length() > 150 && shippingAddr.length() <= 300){
80 shippingAddr1 = shippingAddr.substring(0,150);
81 shippingAddr2 = shippingAddr.substring(150,shippingAddr.length());
82 }else if(shippingAddr.length() > 300 && shippingAddr.length() <= 450){
83 shippingAddr1 = shippingAddr.substring(0,150);
84 shippingAddr2 = shippingAddr.substring(150,300);
85 shippingAddr3 = shippingAddr.substring(300,shippingAddr.length());
86 }else if(shippingAddr.length() > 450){
87 shippingAddr1 = shippingAddr.substring(0,150);
88 shippingAddr2 = shippingAddr.substring(150,300);
89 shippingAddr3 = shippingAddr.substring(300,450);
90 }
91
92 Map<String, Object> shippingAddrFieldMap = new Map<String, Object>();
93 shippingAddrFieldMap.put('addr1' , shippingAddr1);
94 shippingAddrFieldMap.put('addr2' , shippingAddr2);
95 shippingAddrFieldMap.put('addr3' , shippingAddr3);
96 shippingAddrFieldMap.put('city' , shippingCity);
97 shippingAddrFieldMap.put('state' , shippingState);
98 shippingAddrFieldMap.put('zip' , shippingPostalCode);
99
100 Map<String, Object> abaAddressForShippingMap = new Map<String, Object>();
101 abaAddressForShippingMap.put('addressbookAddress' , shippingAddrFieldMap);
102
103 Map<String, Object> abBillingShippingMap = new Map<String, Object>();
104 abBillingShippingMap.put('addressbook' , new List<Object>{abaAddressForBillingMap,abaAddressForShippingMap});
105 netSuiteCustomerMap.put('addressbookList' , abBillingShippingMap);
106
107 //Enable This Section for Custom Fields
108 /*
109 // Initializing the Custom Fields
110 List<Object> custCustomFieldList = new List<Object>();
111 Map<String, Object> custCustomField1 = new Map<String, Object>();
112 custCustomField1.put('fieldType', 'string');
113 custCustomField1.put('scriptId', 'custentity_tax_reg_no');
114 custCustomField1.put('value', taxNumber);
115 custCustomFieldList.add(custCustomField1);
116
117 // Adding the Customer Custom Fields
118 netSuiteCustomerMap.put('customFieldList', new Map<String, Object>{'customField'=>custCustomFieldList});
119*/
120
121
122 netSuiteCustomerList.add(netSuiteCustomerMap);
123
124
125 Map<String, Object> reqJSONMap = new Map<String, Object>();
126 reqJSONMap.put('customers' , netSuiteCustomerList);
127 String reqJSON = JSON.serialize(reqJSONMap);
128
129 Map<String, Object> finalReqMap = new Map<String, Object>();
130 finalReqMap.put('version' , '1.0');
131 finalReqMap.put('action' , 'createCustomer');
132 finalReqMap.put('RequestJSON' , reqJSON);
133 System.debug('Request ::: ' + finalReqMap);
134 //Namespace should be same as breadwinner objects in this org
135 //Map<String, Object> respMap = ns_rc_02.BreadwinnerNetSuiteAPI.call(finalReqMap);
136 Map<String, Object> respMap = breadwinner_ns.BreadwinnerNetSuiteAPI.call(finalReqMap);
137 System.debug('Response ::: ' + respMap);
138
139 response.companyInternalId = 'Blank';
140
141 //Take the NetSuite response map and convert to single string
142 String nsResponseString = '';
143 for(String key : respMap.keySet()){
144 if(nsResponseString.length() !=0){
145 nsResponseString += '::';
146 }
147 nsResponseString += key + '=' + respMap.get(key);
148 }
149 response.netSuiteResponse = nsResponseString;
150 }
151 catch(Exception e) {
152 System.debug('Exception Error ::: ' + e);
153 response.netSuiteResponse = 'Exception: ' + e;
154 }
155
156 } //Only executes if test mode set correctly. This should be removed/reworked prior to production deploy.
157
158 // Send return values to Flow
159 //Add the return values to the Results object
160 //response.outputMember = outputMember;
161 response.testMode = testMode;
162 response.firstName = 'name';
163 response.lastName = 'changes';
164 response.companyName = companyName;
165
166
167 //Wrap the Results object in a List container
168 //(an extra step added to allow this interface to also support bulkification)
169 List<Results> responseWrapper= new List<Results>();
170 responseWrapper.add(response);
171 return responseWrapper;
172 }
173
174 public class Requests {
175 //@InvocableVariable(label='Records for Input' description='Creates NetSuite Companies invoked by Flow' required=false)
176 //public List<SObject> inputCollection;
177
178 @InvocableVariable(label='testMode')
179 public string testMode;
180
181 @InvocableVariable(label='firstName')
182 public string firstName;
183
184 @InvocableVariable(label='lastName')
185 public string lastName;
186
187 @InvocableVariable(label='companyName')
188 public string companyName;
189
190 @InvocableVariable(label='AccountNumber')
191 public string AccountNumber;
192
193 @InvocableVariable(label='Phone')
194 public string Phone;
195
196 @InvocableVariable(label='Website')
197 public string Website;
198
199 @InvocableVariable(label='BillingStreet')
200 public string BillingStreet;
201
202 @InvocableVariable(label='BillingCity')
203 public string BillingCity;
204
205 @InvocableVariable(label='BillingState')
206 public string BillingState;
207
208 @InvocableVariable(label='BillingPostalCode')
209 public string BillingPostalCode;
210
211 @InvocableVariable(label='ShippingStreet')
212 public string ShippingStreet;
213
214 @InvocableVariable(label='ShippingCity')
215 public string ShippingCity;
216
217 @InvocableVariable(label='ShippingState')
218 public string ShippingState;
219
220 @InvocableVariable(label='ShippingPostalCode')
221 public string ShippingPostalCode;
222
223 @InvocableVariable(label='SubsidiaryId')
224 public string SubsidiaryId;
225
226 //Custom Field Example
227 /*
228 @InvocableVariable(label='taxNumber')
229 public string taxNumber;
230*/
231 }
232
233 public class Results {
234 //@InvocableVariable(label='Records for Output' description='Creates NetSuite Companies invoked by Flow' required=false)
235 //public SObject outputMember;
236
237 @InvocableVariable(label='testMode')
238 public string testMode;
239
240 @InvocableVariable(label='firstName')
241 public string firstName;
242
243 @InvocableVariable(label='lastName')
244 public string lastName;
245
246 @InvocableVariable(label='companyName')
247 public string companyName;
248
249 @InvocableVariable(label='companyInternalId')
250 public string companyInternalId;
251
252 @InvocableVariable(label='netSuiteResponse')
253 public string netSuiteResponse;
254 }
255
256}
257
258//Original from: https://mukulmahawariya11.medium.com/calling-invocable-apex-from-flow-838879a332f7
259//Retooled for standard @invokable according to SF Docs: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm
260
261