/
NewLead.aspx.cs
executable file
·365 lines (308 loc) · 15 KB
/
NewLead.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using com.netsuite.webservices;
//for Local DB
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
/* This is our new lead class - the class will have methods for accepting data and will apply business rules as needed */
public partial class _Default : System.Web.UI.Page {
class New_Lead {
private NetSuiteService _service;
private Boolean _isAuthenticated;
private String _companyName;
private String _email;
private String _phone;
private String _firstname;
private String _lastname;
private String _industry;
private String _source;
private String _ip;
private String _customerID;
private String _entityName;
private String _contactID;
private Status _customerStatus;
private Status _contactStatus;
private Status _attachStatus;
public New_Lead() {
_isAuthenticated = false;
_service = new NetSuiteService();
_service.Timeout = 1000*60*60*2;
//Enable cookie management
Uri myUri = new Uri("http://www.domain.com");
_service.CookieContainer = new CookieContainer();
}
public void login(string email, string password, string account) {
//login to NetSuite
Passport passport = new Passport();
passport.account = account;
passport.email = email;
passport.password = password;
Status status = _service.login(passport).status;
_isAuthenticated = status.isSuccess;
}
public void logout() {
_isAuthenticated = !(_service.logout().status.isSuccess);
}
public Boolean isAuthenticated() {
return _isAuthenticated;
}
public void sendEmail() {
MailAddress from = new MailAddress("sales@domain.com", "Sales Department");
MailAddress to = new MailAddress(_email, _firstname + " " + _lastname);
MailMessage message = new MailMessage(from,to);
String body;
using (StreamReader sr = new StreamReader("path_to_email_template.html")) {
body = sr.ReadToEnd();
}
//body = "This is a test.<br>";
message.IsBodyHtml = true;
message.Subject = "Thank You";
message.Body = body;
SmtpClient client = new SmtpClient("smtp.domain.com");
client.Send(message);
}
/*
At one point, NetSuite's WebServices was considered to be buggy. As a backup, the client asked that this form save
each submission to a local database. This is no longer needed but is left in as an example of ODBC connectivity
*/
public void saveToDB() {
string sDBConn = "Data Source=DB_SERVER,1433;Initial Catalog=leads;User ID=sa;Password=1234";
string query = "INSERT INTO xx_NeedsAnalysis VALUES(@lead_datetime,@lead_lastname,@lead_firstname,@lead_companyname,@lead_email,@lead_phone,@lead_industry,@lead_remoteip,@lead_source)";
SqlConnection conn = new SqlConnection(sDBConn);
SqlCommand cmd = new SqlCommand(query, conn);
//BIND THE DATA TO THE PARAMETERS
cmd.Parameters.AddWithValue("@lead_datetime", DateTime.Now);
cmd.Parameters.AddWithValue("@lead_lastname", _lastname);
cmd.Parameters.AddWithValue("@lead_firstname", _firstname);
cmd.Parameters.AddWithValue("@lead_companyname",_companyName);
cmd.Parameters.AddWithValue("@lead_email",_email);
cmd.Parameters.AddWithValue("@lead_phone",_phone);
cmd.Parameters.AddWithValue("@lead_industry", _industry);
cmd.Parameters.AddWithValue("@lead_remoteip", _ip);
cmd.Parameters.AddWithValue("@lead_source", _source);
//OPEN THE CONNECTION AND EXECUTE THE QUERY
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
/*
These three calls parse the returned XML from NetSuite to determine if the operations for creating
a new customer, creating a new contact, and attaching the new contact to the new customer were a
success, respectively.
*/
public String getCustomerStatus() {
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (_customerStatus.statusDetail != null) {
for (int i = 0; i < _customerStatus.statusDetail.Length; i++) {
sb.Append("[Code=" + _customerStatus.statusDetail[i].code + "] " + _customerStatus.statusDetail[i].message + "\n");
}
}
return sb.ToString();
}
public String getContactStatus() {
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (_contactStatus.statusDetail != null) {
for (int i = 0; i < _contactStatus.statusDetail.Length; i++) {
sb.Append("[Code=" + _contactStatus.statusDetail[i].code + "] " + _contactStatus.statusDetail[i].message + "\n");
}
}
return sb.ToString();
}
public String getAttachStatus() {
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (_attachStatus.statusDetail != null) {
for (int i = 0; i < _attachStatus.statusDetail.Length; i++) {
sb.Append("[Code=" + _attachStatus.statusDetail[i].code + "] " + _attachStatus.statusDetail[i].message + "\n");
}
}
return sb.ToString();
}
public void loadData(String companyName, String email, String phone, String firstname, String lastname, String industry, String ip, String source) {
if (companyName == null) { companyName = ""; }
_companyName = companyName;
if (firstname == null) { firstname = ""; }
_firstname = firstname;
if (lastname == null) { lastname = ""; }
_lastname = lastname;
if (email == null) { email = ""; }
_email = email;
if (phone == null) { phone = ""; }
_phone = phone;
if (industry == null) { industry = ""; }
_industry = industry;
if (source == null) { source = ""; }
_source = source;
if (ip == null) { ip = ""; }
_ip = ip;
}
/*
Creates the new Lead/Customer record in NetSuite. Must be called first because we'll need the resulting Customer ID
to place in the new Contact record, and both ids to run the 'attach' operation. These are NetSuite's rules, sadly.
*/
public void createCustomer() {
Customer customer = new Customer();
// Populate information provided by the user
customer.companyName = _companyName;
customer.email = _email;
customer.phone = _phone;
// Set as Lead - New
RecordRef status = new RecordRef();
status.internalId = "6"; // LEAD - New
customer.entityStatus = status;
/*
Custom Fields require creating a reference record based on the type of field, and then we
add each reference object to an array that gets passed into the XML renderer that NetSuite
provides for us.
Industry, IP address and Source are custom fields we've created. Industry is a drop down list, which
means our reference object must contain a reference to which drop down entry the new lead selected.
This seems a bit overboard but lists can be multi-selects as well. IP Address and source are just simple
text fields, so nothing fancy is required, they're just a simple name/value pair.
*/
System.Collections.ArrayList customFields = new System.Collections.ArrayList();
if (_industry != "") {
SelectCustomFieldRef custIndustry = new SelectCustomFieldRef();
custIndustry.internalId = "custentity_customerindustry";
ListOrRecordRef custIndustryList = new ListOrRecordRef();
custIndustryList.internalId = _industry;
custIndustry.value = custIndustryList;
customFields.Add(custIndustry);
}
if (_ip != "") {
StringCustomFieldRef ip = new StringCustomFieldRef();
ip.internalId = "custentity_fnaipaddress";
ip.value = _ip;
customFields.Add(ip);
}
if (_source != "") {
StringCustomFieldRef source = new StringCustomFieldRef();
source.internalId = "custentity_custfnasource";
source.value = _source;
customFields.Add(source);
}
CustomFieldRef[] customFieldRefs = new CustomFieldRef[customFields.Count];
IEnumerator ienum = customFields.GetEnumerator();
for (int i = 0; ienum.MoveNext(); i++) {
customFieldRefs[i] = (CustomFieldRef)ienum.Current;
}
customer.customFieldList = customFieldRefs;
/*
Now with our customer record created, we're going to add it to our XML renderer and pass it on NetSuite.
NetSuite will be returning a response in XML so we're going to grab that so we can get the id of the newly
created record in NetSuite.
*/
WriteResponse response = _service.add(customer);
_customerStatus = response.status;
if (this.getCustomerStatus() == "") {
_customerID = ((RecordRef)response.baseRef).internalId;
_entityName = ((RecordRef)response.baseRef).name;
}
}
/*
If createCustomer() has been called, we can create the contact. We could actually create a new contact without
a customer id - but the problem is that with so many contacts, this would be hard to find. Plus, contacts don't
really have a CRM status in NetSuite like customers do. We want our sales teams to be alerted for LEAD - New.
*/
public void createContact() {
Contact contact = new Contact();
// Populate information provided by the user
contact.firstName = _firstname;
contact.lastName = _lastname;
contact.email = _email;
contact.unsubscribe = false;
contact.unsubscribeSpecified = true;
contact.phone = _phone;
contact.company = new RecordRef();
contact.company.internalId = _customerID;
contact.company.name = _entityName;
// Add the new contact to the database
WriteResponse response = _service.add(contact);
_contactStatus = response.status;
if (this.getContactStatus() == "") {
_contactID = ((RecordRef)response.baseRef).internalId;
}
}
/*
Attaching the contact to the customer is not only establishing the 1-to-N relationship but also
assigning a role to the contact within the customer organization. This form doesn't ask what the
end user's role is within their organization, but it could and that data could be recorded here.
Right now we're assuming that the customer is the primary point of contact.
*/
public void attachContact() {
// Create a new contact role (primary)
RecordRef contactRole = new RecordRef();
contactRole.internalId = "-10"; //Primary
contactRole.type = RecordType.contactRole;
contactRole.typeSpecified = true;
/* We're going to create a reference to the new contact record via it's internal ID */
RecordRef contactRef = new RecordRef();
contactRef.internalId = _contactID;
contactRef.type = RecordType.contact;
contactRef.typeSpecified = true;
/* And the same here for the newly created customer record */
RecordRef customerRef = new RecordRef();
customerRef.internalId = _customerID;
customerRef.type = RecordType.customer;
customerRef.typeSpecified = true;
/* We attach the two by using a pre-made XML renderer from NetSuite. */
AttachContactReference attachReference = new AttachContactReference();
attachReference.attachTo = customerRef;
attachReference.contact = contactRef;
attachReference.contactRole = contactRole;
_attachStatus = _service.attach(attachReference).status;
}
}
//debug
public String getStatus(Status status) {
System.Text.StringBuilder sb = new System.Text.StringBuilder();
if (status.statusDetail != null) {
for (int i = 0; i < status.statusDetail.Length; i++) {
sb.Append("[Code=" + status.statusDetail[i].code + "] " + status.statusDetail[i].message + "\n");
}
}
return sb.ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Save_Lead() {
New_Lead submitted_lead = new New_Lead();
String companyName;
String email;
String phone;
String firstname;
String lastname;
String industry;
String source;
String ip;
companyName = Request.Form.Get("txtOrganization");
email = Request.Form.Get("txtEmail");
phone = Request.Form.Get("txtPhone");
firstname = Request.Form.Get("txtFirstname");
lastname = Request.Form.Get("txtLastname");
industry = Request.Form.Get("txtNeedsIndustry");
source = Request.Form.Get("source");
ip = Request.ServerVariables.Get("REMOTE_ADDR");
submitted_lead.loadData(companyName, email, unsubscribe, phone, firstname, lastname, industry, ip, source);
submitted_lead.saveToDB();
submitted_lead.login("netsuiteuser@domain.com", "password", "012345"); // user, pass & netsuite id
submitted_lead.createCustomer();
submitted_lead.createContact();
submitted_lead.attachContact();
submitted_lead.logout();
if (email != "" && email != null) {
submitted_lead.sendEmail();
}
}
}