Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created October 31, 2013 01:02
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/7242912 to your computer and use it in GitHub Desktop.
Save tstachl/7242912 to your computer and use it in GitHub Desktop.
This gist shows how you can add additional information (in this case we fetch the labels) from desk.com with the Sealsforce integration.
public class DeskLabelService
{
private static String OAUTH_KEY = 'your_consumer_key';
private static String OAUTH_SECRET = 'your_consumer_secret';
private static String ACCESS_TOKEN = 'your_access_token';
private static String ACCESS_TOKEN_SECRET = 'your_access_token_secret';
public static String DESK_DOMAIN = 'yourdomain.desk.com';
@future(callout=true)
public static void process(Set<Id> caseIdSet)
{
// get the list of cases we process
List<Deskcom__Case__c> cases = [SELECT Id, Deskcom__display_id__c FROM Deskcom__Case__c WHERE Id IN :caseIdSet];
// create a map of all the existing labels
Map<String, Id> labelMap = new Map<String, Id>();
for (Label__c label : [SELECT Id, Name FROM Label__c]) {
labelMap.put(label.Name, label.Id);
}
// fetch the label for each case
Map<Id, List<String>> case2Labels = new Map<Id, List<String>>();
for (Deskcom__Case__c c : cases) {
case2Labels.put(c.Id, DeskLabelService.fetchLabels(String.valueOf(c.Deskcom__display_id__c)));
}
// get all the associations with current labels
List<LabelCaseAssociation__c> assoc = [SELECT Id, Case__c, Label__c FROM LabelCaseAssociation__c WHERE Case__c IN :caseIdSet];
delete(assoc);
// create labels that don't exist yet
List<Label__c> createLabelsList = new List<Label__c>();
for (Id caseId : case2Labels.keySet()) {
for (String label : case2Labels.get(caseId)) {
if (!labelMap.keySet().contains(label)) {
createLabelsList.add(new Label__c(Name = label));
}
}
}
insert(createLabelsList);
// add the new labels to the labelMap
for (Label__c label : createLabelsList) {
labelMap.put(label.Name, label.Id);
}
// create the new associations
assoc = new List<LabelCaseAssociation__c>();
for (Deskcom__Case__c c : cases) {
// get the labels for this case
for (String label : case2Labels.get(c.Id)) {
assoc.add(new LabelCaseAssociation__c(Case__c = c.Id, Label__c = labelMap.get(label)));
}
}
insert(assoc);
}
public static List<String> fetchLabels(String caseNumber)
{
List<String> labels;
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://' + DeskLabelService.DESK_DOMAIN + '/api/v2/cases/' + caseNumber);
// we need to sign the request
req = DeskLabelService.signRequest(req);
Http client = new Http();
try {
// execute the web service call
HttpResponse response = client.send(req);
// debug messages
System.debug(response.getBody());
System.debug('STATUS: ' + response.getStatus());
System.debug('STATUS CODE: ' + response.getStatusCode());
// process the response
JSONParser parser = JSON.createParser(response.getBody());
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'labels')) {
// get the value
parser.nextToken();
labels = (List<String>)parser.readValueAs(List<String>.class);
System.debug(labels);
}
}
} catch(System.CalloutException e) {
System.debug('EXCEPTION THROWN: ' + e.getMessage());
}
return labels;
}
private static Map<String, String> getParams(String paramString)
{
Map<String, String> params = new Map<String, String>();
if (paramString == null || paramString == '') return params;
for(String s : paramString.split('&')) {
String[] sl = s.split('=');
if (sl.size() == 2) {
params.put(sl[0], sl[1]);
}
}
return params;
}
private static HttpRequest signRequest(HttpRequest req)
{
Map<String, String> params = new Map<String, String>{
'oauth_consumer_key' => DeskLabelService.OAUTH_KEY,
'oauth_nonce' => String.valueOf(Crypto.getRandomLong()),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => String.valueOf(DateTime.now().getTime()/1000),
'oauth_token' => DeskLabelService.ACCESS_TOKEN,
'oauth_version' => '1.0'
};
String[] host = req.getEndpoint().split('\\?');
// parse get parameters
if (host.size() == 2) {
params.putAll(DeskLabelService.getParams(host[1]));
}
// parse body parameters
if (req.getBody() != null && req.getBody() != '') {
params.putAll(DeskLabelService.getParams(req.getBody()));
}
// create the base string
String baseString = '';
List<String> keyList = new List<String>(params.keySet());
keyList.sort();
for (String key : keyList) {
baseString += key + '=' + params.get(key) + '&';
}
baseString = req.getMethod().toUpperCase() + '&' +
EncodingUtil.urlEncode(host[0], 'UTF-8') + '&' +
EncodingUtil.urlEncode(baseString.substringBeforeLast('&'), 'UTF-8');
System.debug('BASE STRING: ' + baseString);
// create the signature
Blob sig = Crypto.generateMac('HmacSHA1', Blob.valueOf(baseString), Blob.valueOf(
DeskLabelService.OAUTH_SECRET + '&' + DeskLabelService.ACCESS_TOKEN_SECRET
));
String signature = EncodingUtil.urlEncode(EncodingUtil.base64encode(sig), 'UTF-8');
// create the header
String header = 'OAuth ';
for (String key : params.keySet()) {
header += key + '="' + params.get(key) + '", ';
}
header += 'oauth_signature="' + signature + '"';
// sign the request
System.debug('Authorization: ' + header);
req.setHeader('Authorization', header);
return req;
}
}
trigger FetchCaseLabels on Deskcom__Case__c (after insert, after update)
{
Set<Id> caseIdSet = new Set<Id>();
for (Deskcom__Case__c c : Trigger.new) {
caseIdSet.add(c.Id);
}
DeskLabelService.process(caseIdSet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment