Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created June 7, 2012 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tstachl/2888905 to your computer and use it in GitHub Desktop.
Save tstachl/2888905 to your computer and use it in GitHub Desktop.
Desk.com Salesforce integration. This snippet shows you how to create a autocomplete field on the contacts template containing all your contacts of your Salesforce organization.
<div class="agent_customer_gravatar">
<span class="a-user-icon">
{{thumb}} {{social_profile}}
</span>
</div>
<div class="agent_customer_section short" >
{{first_name}}
</div>
<div class="agent_customer_section short" >
{{last_name}}
</div>
<div class="agent_customer_section salesforce" >
{{custom_salesforce_id}}
</div>
<div class="agent_customer_section">
{{company}}
</div>
<div class="agent_customer_section" >
{{title}}
</div>
<div class="agent_customer_section">
{{language}}
</div>
<div class="agent_customer_section" >
{{twitter_users}}
</div>
<div class="agent_customer_section" >
{{facebook_users}}
</div>
<div class="agent_customer_section" >
{{emails}}
</div>
<div class="agent_customer_section" >
{{phones}}
</div>
<div class="agent_customer_section" >
{{addresses}}
</div>
<div class="agent_customer_section" >
{{about}}
</div>
<style>
.ui-autocomplete-category {
font-weight: bold;
padding: .2em .4em;
margin: .8em 0 .2em;
line-height: 1.5;
}
</style>
<script>
$.widget('custom.catcomplete', $.ui.autocomplete, {
_create: function() {
this._loading = $('<img src="https://d3jyn100am7dxp.cloudfront.net/images/web_site_copy/loading.gif?1338961935">').insertBefore(this.element);
// Reassigning the original element
this.originalElement = this.element;
this.element = this.originalElement.hide().clone().insertBefore(this.originalElement).attr({ id: '', name: '' }).val('');
var originalSelect = this.options.select, that = this;
this.options.select = function(event, ui) {
if (ui.item && ui.item.id) that.originalElement.val(ui.item.id);
return originalSelect(event, ui);
};
$.ui.autocomplete.prototype._create.call(this);
},
_renderMenu: function(ul, items) {
var self = this,
currentCategory = '';
$.each(items, function(index, item) {
if (item.industry != currentCategory) {
ul.append('<li class="ui-autocomplete-category">' + item.industry + '</li>');
currentCategory = item.industry;
}
self._renderItem(ul, item);
});
},
el: function() {
return this.element;
},
loading: function() {
return this._loading;
},
oEl: function() {
return this.originalElement;
}
});
var ar = [],
source = function(request, response) {
response($.ui.autocomplete.filter(ar, request.term));
},
el = $('div.salesforce input').catcomplete({
source: source,
select: function(ev, ui) {
if (ui.item) {
for (var key in ui.item) {
if ($('#customer_' + key).size() > 0) $('#customer_' + key).val(ui.item[key]);
}
}
}
}),
callback = {
onSuccess: function(queryResult) {
if (queryResult.size > 0) {
ar = queryResult.getArray('records').map(function(item) {
if (el.val() == item.Id) {
el.catcomplete('el').val(item.FirstName + ' ' + item.LastName + ', ' + item.Account.Name);
}
return {
value: item.FirstName + ' ' + item.LastName + ', ' + item.Account.Name,
first_name: item.FirstName,
last_name: item.LastName,
company: item.Account.Name,
title: item.Title,
industry: item.Account.Industry || 'None',
id: item.Id
};
});
}
el.catcomplete('loading').hide();
el.catcomplete('el').show();
},
onFailure: function(error) {
alert("An error has occurred: " + error);
$('div.salesforce').hide();
}
},
run = function() {
sforce.connection.login("api@example.com", "YourPassword");
sforce.connection.query("Select Id, FirstName, LastName, Title, Account.Name, Account.Industry From Contact Order By Account.Industry", callback);
};
window.sfAsyncInit = run;
(function(d) {
var js, id = 'salesforce-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return run();}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = '//nodeproxy.herokuapp.com/sfconn.js';
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment