-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample2.html
150 lines (130 loc) · 5.61 KB
/
sample2.html
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
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div style="padding-top: 20px;">1. Use developer tools and watch the console.log.</div>
<div style="padding-top: 20px;">2. Then login.</div>
<button onclick="javascript:jsforce.browser.login();">Login</button>
<div style="padding-top: 20px;">3. Then type the contact name and the account to set the contact to.</div>
<div>
<label for="account">Account:</label>
<input type="text" id="account" placeholder="Acme Corp">
</div>
<div>
<label for="contact">Contact:</label>
<input type="text" id="contact" placeholder="Willie Coyote">
</div>
<button onclick="javascript:createAccountContact();">Set</button>
<div style="padding-top: 20px;">4. See the result in console.log.</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsforce/1.7.0/jsforce.min.js"></script>
<script>
var jsForceConn;
jsforce.browser.init({
clientId: "3MVG9uudbyLbNPZPdIz.uqzF0wyw8jpFz_Tc7l.za7mCCK3UIu3PDncF6_5j98u8p9Xeo89S83jDB_jUcsuaR",
redirectUri: "http://localhost:8080/callback.html",
proxyUrl: "https://node-salesforce-proxy.herokuapp.com/proxy/"
});
jsforce.browser.on('connect', function(conn) {
console.log('Successfully connected to Salesforce');
jsForceConn = conn;
});
function createAccountContact() {
var account = document.getElementById('account').value;
var contact = document.getElementById('contact').value;
console.log('-----------------------------')
console.log('Starting...')
// Make sure jsForceConn is set and that we're logged in.
try {
if (jsforce.browser.isLoggedIn() == false || jsForceConn == null) {
throw 'Not logged in.'
}
} catch (error) {
console.log('Not logged in. Log in first.');
return;
}
// Fetch or create the account.
var getAccountPromise = new Promise((resolve, reject) => {
console.log('Getting account: ' + account);
getAccount(resolve, jsForceConn, account);
});
// Fetch or create the contact
var getContactPromise = new Promise((resolve, reject) => {
console.log('Getting contact: ' + contact);
getContact(resolve, jsForceConn, contact);
});
// When both API calls have finished...
Promise.all([getAccountPromise, getContactPromise]).then(values => {
console.log('All promises returned. Updating contact.');
var accountId = values[0];
var contactId = values[1];
// Then set the Contractor for the Project
jsForceConn.sobject("Contact").update({
Id: contactId,
AccountId: accountId
}, function(err, ret) {
if (err || !ret.success) {
return console.error(err, ret);
}
console.log('Account for Contact successfully updated!');
});
});
}
// Return the ID for the Account. If it doesn't exist, create it.
function getAccount(resolve, conn, account) {
conn.query("SELECT Id FROM Account WHERE Name = '" + account + "'", function(err, res) {
if (err) {
console.error(err);
}
if (res['totalSize'] == 0) {
console.log("Account doesn't exist, creating it.");
createAccount(resolve, conn, account);
} else {
console.log('Account already exists, returning ID: ' + res['records'][0]['Id']);
resolve(res['records'][0]['Id']);
}
});
}
// Create the account then return the ID.
function createAccount(resolve, conn, account) {
conn.sobject("Account").create({
Name: account
}, function(err, ret) {
if (err || !ret.success) {
return console.error(err, ret);
}
console.log('Created new account: ' + ret.id);
resolve(ret.id);
});
}
function getContact(resolve, conn, contact) {
var formattedContact = name.replace(/'/g, "\\'");
conn.query("SELECT Id, Name FROM Contact WHERE Name = '" + formattedContact + "'", function(err, res) {
if (err) {
return console.error(err);
}
if (res['totalSize'] == 0) {
console.log("Contact doesn't exist, creating it..");
createContact(resolve, conn, contact);
} else {
console.log('Contact already exists, returning ID: ' + res['records'][0]['Id']);
resolve(res['records'][0]['Id']);
}
});
}
function createContact(resolve, conn, contact) {
var contactNames = contact.split(" ");
conn.sobject("Contact").create({
FirstName: contactNames[0],
LastName: contactNames[1]
}, function(err, ret) {
if (err || !ret.success) {
return console.error(err, ret);
}
console.log('Created new contact: ' + ret.id);
resolve(ret.id);
});
}
</script>
</body>
</html>