Last active
November 21, 2016 19:43
-
-
Save tstachl/a5d53d3228ae3c2a36e3 to your computer and use it in GitHub Desktop.
Fetch data trigger.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DeskTriggerHelper | |
{ | |
private static DeskClient CLIENT = new DeskClient(new Map<String, String>{ | |
'token' => 'TOKEN', | |
'tokenSecret' => 'TOKEN_SECRET', | |
'consumerKey' => 'CONSUMER_KEY', | |
'consumerSecret' => 'CONSUMER_SECRET', | |
'endpoint' => 'https://example.desk.com' | |
}); | |
public static void fetchData(List<Deskcom__Case__c> cases) | |
{ | |
for (Deskcom__Case__c c : cases) { | |
DeskTriggerHelper.fetchData(c.Id); | |
} | |
} | |
@future(callout=true) | |
public static void fetchData(Id cId) | |
{ | |
// get the case object | |
Deskcom__Case__c c = [Select Deskcom__display_id__c From Deskcom__Case__c Where Id = :cId]; | |
// fetch the latest changes | |
DeskResource res = CLIENT.getResource('cases').find(Integer.valueOf(c.Deskcom__display_id__c)); | |
// get the custom fields | |
Map<String, Object> custom_fields = (Map<String, Object>)res.get('custom_fields'); | |
// get a timestamp as ISO8601 string | |
String firstResponseAt = String.valueOf(custom_fields.get('first_response_at')); | |
// make sure we don't just have a blank string | |
if (String.isNotBlank(firstResponseAt)) { | |
// convert the ISO8601 string to an actual DateTime object | |
c.FirstResponseAt__c = DeskTriggerHelper.convertISO8601(firstResponseAt); | |
} | |
String severity = String.valueOf(custom_fields.get('severity')); | |
if (String.isNotBlank(severity)) { | |
c.Severity__c = severity; | |
} | |
// update the case | |
update(c); | |
} | |
public static DateTime convertISO8601(String iso8601_ts) | |
{ | |
// use JSON.deserialize method to get a DateTime object | |
DateTime dt = (DateTime)JSON.deserialize('"' + iso8601_ts + '"', DateTime.class); | |
// Bug in JSONParser or DateTime object results in a malformed DateTime, | |
// so convert to Long and back to DateTime. Without the conversion, | |
// methods that access timeGmt() and its components will actually get | |
// local time instead of GMT. | |
return DateTime.newInstance(dt.getTime()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger FetchData on Deskcom__Case__c (after update) | |
{ | |
List<Deskcom__Case__c> cases = new List<Deskcom__Case__c>(); | |
for (Deskcom__Case__c c : Trigger.new) { | |
if (c.Deskcom__updated_at__c != Trigger.oldMap.get(c.Id).Deskcom__updated_at__c) { | |
cases.add(c); | |
} | |
} | |
// this has to be a future method to run the API calls | |
DeskTriggerHelper.fetchData(cases); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment