-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_contacts.js
437 lines (403 loc) · 14.9 KB
/
update_contacts.js
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below will solicit users in the "SEA Current Contacts" spreadsheet to update
// their contact info via a custom Google form. Code adapted from examples in
// https://developers.google.com/apps-script/reference/forms
//
//////////////////////////////////////////////////////////////////////////////////////////
SALESFORCE_CONTACT_FORM_CONFIG = {
"First Name": {
"required": true,
"description": "",
},
"Last Name":
{
"required": true,
"description": "",
},
"Mailing Street": {
"required": true,
"description": "",
},
"Mailing City": {
"required": true,
"description": "",
},
"Mailing State/Province": {
"required": true,
"description": "",
},
"Mailing Zip/Postal Code": {
"required": true,
"description": "",
},
"Mobile": {
"required": false,
"description": "place cell phone contact here",
},
"Phone": {
"required": false,
"description": "leave blank if no landline available",
},
"Email": {
"required": true,
"description": "preferred non-minds matter email contact",
},
"Employer": {
"required": false,
"description": "for volunteers only",
},
"Title": {
"required": false,
"description": "title at work (for volunteers only)",
}
};
// Create Form object (to be prefilled with contact info)
function create_update_contacts_form() {
// Creates the contact info update form. If form exists, returns the
// Script Property "contactUpdateFormId"
// TODO: Add field descriptions and specify which ones are required
var scriptProps = PropertiesService.getScriptProperties();
var formID = scriptProps.getProperty('contactUpdateFormId');
Logger.log(formID);
if (formID == null) {
var formTitle = "Contact Info Update Form"
var formDescription = "Form for updating your contact information with Minds Matter"
var contactInfoForm = FormApp.create(formTitle);
contactInfoForm.setDescription(formDescription);
for (var key in SALESFORCE_CONTACT_FORM_CONFIG) {
var item_config = SALESFORCE_CONTACT_FORM_CONFIG[key];
var item = contactInfoForm.addTextItem();
item.setHelpText(item_config['description']);
item.setRequired(item_config['required']);
item.setTitle(key);
}
formID = contactInfoForm.getId();
scriptProps.setProperty('contactUpdateFormId', formID);
}
else {
contactInfoForm = FormApp.openById(formID);
}
return contactInfoForm;
}
function create_form_response(data, form) {
// Pre-fills a form pre-filled with fields from data. Fields in form must match keys of data.
// Arguments:
// - data (dict): dictionary of key, value pairs that will be used to prefill form fields;
// for updating contacts, required keys included "First Name", "Last Name", "Mailing Street",
// "Mailing State/Province", "Mailing Zip/Postal Code", "Mobile", "Phone", "Email", "Employer"
// - form (Form object): a Google Form object returned from the FormApp.create() function
//
// Returns:
// - url to prefilled Google form
var formResponse = form.createResponse();
var items = form.getItems();
var title_dict = {};
for (var item in items) {
title_dict[items[item].getTitle()] = items[item];
}
for (var key in data) {
// var item = form.addTextItem()
// item.setTitle(key)
var item = title_dict[key]
var response = item.asTextItem().createResponse(data[key]);
formResponse.withItemResponse(response);
}
var url = formResponse.withItemResponse(response).toPrefilledUrl();
Logger.log('Form URL: ' + url);
return url
}
function parse_contact_sheet(spreadsheet) {
// Reads a spreadsheet of Current Contacts and returns
// an array of dictionaries with key, value pairs for
// each required field in the "Contact Info Update Form"
// Required Fields:
// - First Name
// - Last Name
// - Mailing Street
// - Mailing State/Province
// - Mailing Zip/Postal Code
// - Mobile
// - Phone
// - Email
// - Employer
usersSheet = spreadsheet.getActiveSheet();
range = usersSheet.getDataRange();
values = range.getValues();
// Get num rows and columns
lastRow = range.getLastRow();
lastColumn = range.getLastColumn();
// Map column names to column index
var columnDict = {}
for (i = 0; i < lastColumn; i++) {
columnDict[values[0][i]] = i;
}
var contacts = [];
for (i = 1; i < lastRow; i++) {
var contact = {};
for (var field in SALESFORCE_CONTACT_FORM_CONFIG) {
colIndex = columnDict[field]
contact[field] = values[i][colIndex]
}
Logger.log(contact);
contacts.push(contact);
}
return contacts
}
function create_prefilled_links() {
// Parses a spreadsheet of current contacts and creates
// a pre-filled Google form for each contact.
// Form links will be emailed to contacts using MailMerge.js
var scriptProps = PropertiesService.getScriptProperties();
var updateContactsForm = create_update_contacts_form()
var salesforceSpreadSheetID = scriptProps.getProperty('salesforceSpreadSheetID');
var ss = SpreadsheetApp.openById(salesforceSpreadSheetID);
var contacts = parse_contact_sheet(ss);
var mailmerge_sheetID = PropertiesService.getScriptProperties().getProperty('contactMailMergeSheetID');
if (mailmerge_sheetID == null) {
var mailmerge_ss = SpreadsheetApp.create("Contact Update MailMerge", 500, 4);
mailmerge_sheetID = mailmerge_ss.getId();
scriptProps.setProperty('contactMailMergeSheetID', mailmerge_sheetID);
mailmerge_ss.appendRow(["FirstName", "email", "form_link"]);
}
else {
var mailmerge_ss = SpreadsheetApp.openById(mailmerge_sheetID);
var activeSheet = mailmerge_ss.getActiveSheet();
if (activeSheet.getMaxRows() > 1) {
activeSheet.deleteRows(2, activeSheet.getMaxRows() - 1)
}
}
var domainname = scriptProps.getProperty('domainname');
// Iterate through array of user data and create prefilled form for each one
for (var idx in contacts) {
var contact = contacts[idx];
Logger.log(idx)
var prefilledFormLink = create_form_response(contact, updateContactsForm)
var email = contact["First Name"].toLowerCase() + "." + contact["Last Name"].toLowerCase() + "@" + domainname;
email = email.replace(" ", ".");
email = email.replace(" ", ".");
mailmerge_ss.appendRow([contact["First Name"],
email,
prefilledFormLink]);
}
}
function add_salesforce_ID_to_users() {
var domainname = PropertiesService.getScriptProperties().getProperty('domainname');
var scriptProps = PropertiesService.getScriptProperties();
var salesforceSpreadSheetID = scriptProps.getProperty('salesforceSpreadSheetID');
var ss = SpreadsheetApp.openById(salesforceSpreadSheetID);
var salesforceSheetName = PropertiesService.getScriptProperties().getProperty('salesforceSheetName');
var sheet = ss.getSheetByName(salesforceSheetName);
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(1, 1, 1, lastColumn);
var rangeValues = searchRange.getValues();
var columnDict = {}
for (i = 0; i < lastColumn; i++) {
columnDict[rangeValues[0][i]] = i;
}
data = rangeData.getValues();
for (i = 1; i < lastRow; i++) {
var email = data[i][columnDict['First Name']].toLowerCase() + "." + data[i][columnDict['Last Name']].toLowerCase() + "@" + domainname;
email = email.replace(" ", ".");
email = email.replace(" ", ".");
try {
var user = AdminDirectory.Users.get(email);
}
catch (e) {
Logger.log("email not found " + email);
var user = null;
}
if (user != null) {
var exIDs = user.getExternalIds();
var salesforceID = data[i][columnDict["Contact ID"]];
if (exIDs == null) {
user.setExternalIds([{
"value": salesforceID,
"type": "account"
}]);
AdminDirectory.Users.update(user, email);
}
}
}
}
function SalesforceLogin() {
var sf_version = "42.0";
var instanceUrl = "https://na88.lightning.force.com/";
var scriptProps = PropertiesService.getScriptProperties();
var token = scriptProps.getProperty('sfAuthToken')
var soap_url = 'https://login.salesforce.com/services/Soap/u/' + sf_version;
var client_id = 'RestForce'
var username = scriptProps.getProperty('sfUsername');
var password = scriptProps.getProperty('sfPassword');
login_soap_request_body = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\
<env:Envelope\
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\
xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\"\
xmlns:urn=\"urn:partner.soap.sforce.com\">\
<env:Header>\
<urn:CallOptions>\
<urn:client>" + client_id + "</urn:client>\
<urn:defaultNamespace>sf</urn:defaultNamespace>\
</urn:CallOptions>\
</env:Header>\
<env:Body>\
<n1:login xmlns:n1=\"urn:partner.soap.sforce.com\">\
<n1:username>" + username + "</n1:username>\
<n1:password>"+ password + token + "</n1:password>\
</n1:login>\
</env:Body>\
</env:Envelope>";
var options = {
'headers': {
'SOAPAction': 'login',
'charset': 'UTF-8'
},
'contentType': 'text/xml',
'payload': login_soap_request_body
}
var response = UrlFetchApp.fetch(soap_url, options);
var soap = XmlService.getNamespace("http://schemas.xmlsoap.org/soap/envelope/");
var document = XmlService.parse(response.getContentText());
var root = document.getRootElement();
var result = root.getChild('Body', soap).getChildren()[0].getChildren()[0]
var sforce = result.getNamespace();
var serverUrl = result.getChild('serverUrl', sforce).getValue();
var instanceUrl = 'https://' + serverUrl.split('/')[2] + "/";
var sessionID = result.getChild('sessionId', sforce).getValue();
return {
'instanceUrl': instanceUrl,
'sessionID': sessionID
}
}
function querySalesforce(soql) {
var scriptProps = PropertiesService.getScriptProperties();
var instanceUrl = "https://na88.lightning.force.com/";
var queryUrl = instanceUrl + "/services/data/v42.0/query?q=" + encodeURIComponent(soql);
var sfAuthToken = scriptProps.getProperty('sfAuthToken')
var options = {
headers: {
Authorization: 'Bearer ' + sfAuthToken
}
}
var response = UrlFetchApp.fetch(queryUrl, options);
var queryResult = Utilities.jsonParse(response.getContentText());
return queryResult;
}
function postSalesforceContact(contactId, contact, sflogin) {
var scriptProps = PropertiesService.getScriptProperties();
var queryUrl = sflogin.instanceUrl + "/services/data/v42.0/sobjects/Contact/" + contactId;
var options = {
"contentType": "application/json",
"method": "patch",
headers: {
"Authorization": 'Bearer ' + sflogin.sessionID,
"Content-Type": "application/json"
},
"payload": JSON.stringify(contact),
}
var response = UrlFetchApp.fetch(queryUrl, options);
var queryResult = Utilities.jsonParse(response.getContentText());
return queryResult;
}
function update_salesforce_contact_info() {
var scriptProps = PropertiesService.getScriptProperties();
var updateContactsForm = create_update_contacts_form()
// grab form responses
var responseID = updateContactsForm.getDestinationId();
var responsess = SpreadsheetApp.openById(responseID);
var headers = createHeaderIfNotFound_('UpdateStatus', responsess);
var sheet = responsess.getActiveSheet();
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(1, 1, 1, lastColumn);
var rangeValues = searchRange.getValues();
var sflogin = SalesforceLogin();
var columnDict = {}
for (i = 0; i < lastColumn; i++) {
columnDict[rangeValues[0][i]] = i;
}
data = rangeData.getValues();
// iterate through responses; if UpdateStatus == Done, skip
// else push info to Salesforce
for (i = 1; i < lastRow; i++) {
status = data[i][columnDict['UpdateStatus']]
if (status == "") {
email = data[i][columnDict['Email Address']]
Logger.log(email)
var user = AdminDirectory.Users.get(email);
var phones = []
var mobile_num = data[i][columnDict['Mobile']]
if (mobile_num != "") {
phones.push({
"type": "mobile",
"value": mobile_num,
"primary": true
})
}
var land_num = data[i][columnDict['Phone']]
if (land_num != "") {
phones.push({
"type": "home",
"value": land_num,
"primary": false
})
}
emails = user.getEmails()
for (var email in emails) {
if (emails[email].type == 'home') {
Logger.log(emails[email]);
emails[email].value = data[i][columnDict['Email']]
}
}
user_update = {
"emails": emails,
"phones": phones
}
// TODO: update name, if name change add new name as alias
Logger.log(user)
// update contact info in GSuites
AdminDirectory.Users.update(user_update, user.id);
externalIDs = user.getExternalIds()
var salesforceID = null;
for (var extID in externalIDs) {
if (externalIDs[extID].type == 'account') {
var salesforceID = externalIDs[extID].value
contactdata = {
'FirstName': data[i][columnDict['First Name']],
'LastName': data[i][columnDict['Last Name']],
'MailingStreet': data[i][columnDict['Mailing Street']],
'MailingCity': data[i][columnDict['Mailing City']],
'MailingPostalCode': data[i][columnDict['Mailing Zip/Postal Code']],
'Phone': data[i][columnDict['Phone']],
'MobilePhone': data[i][columnDict['Mobile']],
'Email': data[i][columnDict['Email']],
'Employer__c': data[i][columnDict['Employer']],
'Title': data[i][columnDict['Title']]
}
// push contact info to Salesforce
try {
postSalesforceContact(salesforceID, contactdata, sflogin)
sheet.getRange(i + 1, lastColumn).setValue("Done").clearFormat().setComment(new Date());;
}
catch (err) {
err_msg = "Unable to push contact to Salesforce: " + data[i][columnDict['First Name']] + " " + data[i][columnDict['Last Name']] + " " + data[i][columnDict['Mobile']]
Logger.log(err_msg);
}
}
}
if (salesforceID == null) {
Logger.log('No salesforce ID for ' + email)
}
}
}
}
function send_mail_merge_emails() {
var mailmerge_sheetID = PropertiesService.getScriptProperties().getProperty('contactMailMergeSheetID');
var draftID = PropertiesService.getScriptProperties().getProperty('AccountUpdateDraftID');
var mailmerge_ss = SpreadsheetApp.openById(mailmerge_sheetID);
run_mail_merge(mailmerge_ss, draftID, 'email');
}