You need to sign in to do that
Don't have an account?
Class or trigger - newbie question. Please help.
Hi there,
Currently struggling with a problem relating to new caess being created.
When new cases come through the API I want to take a certain piece of information within that case, actually a URL and pass it through Bitly to shorten it.
I found this code on the forum and I have added in the field that I want on there which I want it to shorten - the question is. How do I invoke this class - do I need to set it as a trigger on the Case?
I have tried doing that but it doesn't like the code below due to the "static" elements in there.
Effectively what I am asking is, I have the code (which when tested in the class page does not return errors) it's just how do I get it to do it's stuff?
Thanks in advance
/*
Class to connect to bit.ly API to shorten a long URL
*/
global class bitly {
private static final String APIKEY = 'R_b56894694361ab39ab856f69893b3911';
private static final String LOGIN = 'mujowax';
global static String shorten(String url) {
Http h = new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint('http://api.bit.ly/shorten?version=2.0.1&format=xml&login='+LOGIN+'&apiKey='+APIKEY+'&longUrl='+'Geohash__c');
request.setMethod('GET');
try {
HttpResponse response = h.send(request);
if (response.getStatusCode() == 200) {
XMLStreamReader reader = response.getXmlStreamReader();
String shortUrl = parseResponse(reader);
return shortUrl;
}
} catch (System.CalloutException e) {
System.debug('\n**** bit.ly CalloutException: '+e);
}
// if the callout was unsuccessful for any reason
return null;
}
private static String parseResponse(XmlStreamReader reader) {
String shortUrl;
while (reader.hasNext()) {
if (reader.getEventType() == XmlTag.START_ELEMENT && reader.getLocalName() == 'shortUrl') {
reader.next();
shortUrl = reader.getText();
break;
}
if (reader.getEventType() == XmlTag.START_ELEMENT && reader.getLocalName() == 'errorMessage') {
reader.next();
if (reader.getEventType() != XmlTag.END_ELEMENT) {
String errorMessage = reader.getText();
System.debug('\n* Error message from bit.ly: '+errorMessage);
return null;
}
}
reader.next();
}
return shortUrl;
}
private static testMethod void testBitly0() {
bitly.shorten('http://www.salesforce.com/');
}
private static testMethod void testBitly1() {
String responseXml = '<bitly><errorCode>0</errorCode><errorMessage></errorMessage><results><nodeKeyVal><userHash>15pXjJ</userHash><shortKeywordUrl></shortKeywordUrl><hash>M2FqH</hash><nodeKey><![CDATA[http://www.salesforce.com]]></nodeKey><shortUrl>http://bit.ly/15pXjJ</shortUrl></nodeKeyVal></results><statusCode>OK</statusCode></bitly>';
XmlStreamReader reader = new XmlStreamReader(responseXml);
Test.startTest();
String shortUrl = parseResponse(reader);
System.assertNotEquals(null, shortUrl);
}
private static testMethod void testBitly2() {
String responseXml = '<bitly><errorCode>203</errorCode><errorMessage>You must be authenticated to access shorten</errorMessage><statusCode>ERROR</statusCode></bitly>';
XmlStreamReader reader = new XmlStreamReader(responseXml);
Test.startTest();
String shortUrl = parseResponse(reader);
System.assertEquals(null, shortUrl);
}
}
Have you tried creating a trigger that calls the methods in this class?