Skip to content

Instantly share code, notes, and snippets.

@nategrieb
Created December 12, 2013 19:14
Show Gist options
  • Save nategrieb/7933694 to your computer and use it in GitHub Desktop.
Save nategrieb/7933694 to your computer and use it in GitHub Desktop.
public class VOTimeLinePOSTClass{
// Create boolean to be used in test class
public static boolean isApexTest = false;
// Create boolean that differentiates between Opening and Closing a case
public static boolean caseIsClosed = true;
@future(callout=true)
//Set string of variables you want to appear in VictorOps Timeline
public static void caseToVictorTimeLine(boolean isRecovery, LIST THE REST OF THE VARIABLES YOU WISH TO USE){
//Customize what you want to appear in the MSG in VictorOps Timeline
//For example this will read "New SalesForce Case from: 'Name or Email', Case Subject: 'Case Reason or Email Subject' "
String AtMessage='from: ';
String IssueMessage=', Case Subject: ';
String critical='critical';
String recovery='recovery';
String message='New SalesForce Case '+AtMessage + ifnull(name,email) + IssueMessage + ifnull(reason,subject);
//Set up HttpRequest
HttpRequest req = new HttpRequest();
Http http = new Http();
//Set Http Endoint - Make sure to include your Org's API Key and the correct Routing Key
req.setEndpoint('http://alert.victorops.com/integrations/generic/20131114/alert/API KEY GOES IN HERE/ROUTING KEY GOES IN HERE');
//Set request method to 'POST'
req.setMethod('POST');
//Map out the strings note that message_type is requried and Entity ID should be unique for incident
Map<String, String> data = new Map<String, String>();
data.put('message_type', ifRecover(isRecovery, recovery,critical));
data.put('entity_id', casenumber);
data.put('entity_display_name', 'SalesForce Case Number: ' +casenumber);
data.put('monitoring_tool', 'SalesForce');
data.put('state_message', message);
//
String body = JSON.serialize(data);
System.debug(body);
req.setBody(body);
req.setHeader('Content-Type','application/json');
// Create a new http object to send the request object
// A response object is generated as a result of the request
if (!isApexTest){
HTTPResponse res = http.send(req);
System.debug(res.getBody());
}
}
//Use the following to return the users Name if it is provided if not default to their Email
private static String ifnull(String maybeValue, String defaultValue) {
if(maybeValue != null) {
return maybeValue;
} else {
return defaultValue;
}
}
//Use the following to to set up different triggers that fire depending on the case being opened or closed.
private static String ifRecover(boolean isRecovery, String recovery, string critical){
if (isRecovery){
return recovery;
} else{
return critical;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment