forked from ccoenraets/forcejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
206 lines (181 loc) · 7.34 KB
/
index.html
File metadata and controls
206 lines (181 loc) · 7.34 KB
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
<html>
<head>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">ForceJS</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="javascript:login()">Login</a></li>
<li><a href="javascript:discardToken()">Discard Token</a></li>
<li><a href="javascript:isLoggedIn()">Is Logged In?</a></li>
</ul>
</div>
</div>
</div>
<div class="container" style="padding-top: 60px;">
<div class="row">
<div class="col-xs-12 col-sm-4">
<p><button type="button" class="btn btn-default" onclick="query()"><i class="glyphicon glyphicon-refresh"></i> Get Contacts</button></p>
<ul id="list" class="list-group"></ul>
</div>
<div class="col-xs-12 col-sm-8">
<p><button type="button" class="btn btn-default" onclick="newContact()"><i class="glyphicon glyphicon-plus"></i> New</button></p>
<form role="form">
<div class="form-group">
<label for="contactId">Id</label>
<input type="text" class="form-control" id="contactId" disabled>
</div>
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" class="form-control" id="firstName">
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" class="form-control" id="lastName">
</div>
<button type="button" class="btn btn-default" id="createBtn" onclick="create()" style="display:none">Create</button>
<button type="button" class="btn btn-default" id="updateBtn" onclick="update()" style="display:none">Update</button>
<button type="button" class="btn btn-default" id="deleteBtn" onclick="del()" style="display:none">Delete</button>
</form>
</div>
</div>
</div>
<script src="force.js"></script>
<script>
var contactList = document.getElementById('contact-list'),
idField = document.getElementById('contactId'),
firstNameField = document.getElementById('firstName'),
lastNameField = document.getElementById('lastName'),
createBtn = document.getElementById('createBtn'),
updateBtn = document.getElementById('updateBtn'),
deleteBtn = document.getElementById('deleteBtn');
// ForceJS is built to work out of the box with sensible defaults.
// Uncomment the force.init() function call below to provide specific runtime parameters
// force.init({
// appId: '3MVG9fMtCkV6eLheIEZplMqWfnGlf3Y.BcWdOf1qytXo9zxgbsrUbS.ExHTgUPJeb3jZeT8NYhc.hMyznKU92',
// apiVersion: 'v32.0',
// loginUrl: 'https://login.salesforce.com',
// oauthRedirectURL: 'http://localhost:8200/oauthcallback.html',
// proxyURL: 'http://localhost:8200'
// });
function login() {
force.login(
function() {
console.log('Salesforce login succeeded');
},
function(error) {
console.log(error);
alert('Salesforce login failed');
});
}
function query() {
// Empty list
list.innerHTML = '';
// Retrieve contacts
force.query('select id, firstName, lastName from contact LIMIT 50',
function(response) {
var str = '';
var contacts = response.records;
for (var i=0; i < contacts.length; i++) {
str += '<a href="#' + contacts[i].Id + '" class="list-group-item">' + contacts[i].FirstName + ' '
+ contacts[i].LastName + '</a>';
}
list.innerHTML = str;
},
function(error) {
alert("An error has occurred. See console for details.");
});
}
function request() {
// Empty list
list.innerHTML = '';
// Retrieve contacts
force.request({path: '/services/apexrest/contact',
success: function(response) {
console.log('request');
var str = '';
var contacts = response;
for (var i=0; i < contacts.length; i++) {
str += '<li><a href="#' + contacts[i].Id + '" class="list-group-item">' + contacts[i].Name + '</a></li>';
}
list.innerHTML = str;
},
error: function(error) {
alert("An error has occurred. See console for details.");
}});
}
function create() {
force.create('contact', {FirstName: firstNameField.value, LastName: lastNameField.value},
function(response) {
console.log(response);
},
function(error) {
alert("An error has occurred. See console for details.");
});
}
function update() {
force.update('contact', {Id: idField.value, FirstName: firstNameField.value, LastName: lastNameField.value},
function(response) {
console.log(response);
},
function(error) {
alert("An error has occurred. See console for details.");
});
}
function del() {
force.del('contact', idField.value,
function(response) {
console.log(response);
},
function(error) {
alert("An error has occurred. See console for details.");
});
}
function retrieve(id) {
force.retrieve('contact', id, null,
function(contact) {
console.log(contact);
idField.value = contact.Id;
firstNameField.value = contact.FirstName;
lastNameField.value = contact.LastName;
createBtn.style.display = 'none';
updateBtn.style.display = 'inline';
deleteBtn.style.display = 'inline';
},
function(error) {
alert("An error has occurred. See console for details.");
});
}
function discardToken() {
force.discardToken();
alert('Token discarded');
}
function isLoggedIn() {
alert(force.isLoggedIn());
}
function newContact() {
idField.value = "";
firstNameField.value = "";
lastNameField.value = "";
createBtn.style.display = 'inline';
updateBtn.style.display = 'none';
deleteBtn.style.display = 'none';
}
window.onhashchange = function () {
var id = window.location.hash.substr(1);
retrieve(id);
}
</script>
</body>
</html>