0

I have installed Salesforce for Social media app from app exchange. Not I am using it to reply to twitter to the users. In that reply I am asking them to fill-up a form, whose link is given in the reply itself. The salesforce reply character limit is 140 words. SO I want to shorten the url. Is there any way to do it via salesforce itself. I have googled it and found an app for the same named "LinkForce". But I don't want to use that app for that. Is there any standard feature in salesforce to shorten the URL?

1
  • Can URL Rewriter class help me to achieve the same?
    – happy
    Oct 5, 2015 at 13:41

1 Answer 1

2

You can use Bitly (Url Shortening) API for this. Create a a Apex class as below to call the service. Here is a developer forum link on this I came accross. Hope this helps.

How to use

bitly b = new bitly();
b.mode = 'live';
b.sUrl = '<a href="http://www.bridgefarmconsulting.com';" target="_blank" rel="nofollow">http://www.bridgefarmconsulting.com';</a>

system.debug('Check BitLy' + b.getbitly() );

Bitly Class

global class bitly {

    Public String mode;
    Public String sUrl;

    public String getbitly () {
        String shorten;
        XmlStreamReader reader;
        HttpResponse res;

        //First, build the http request
        Http h = new Http();
        HttpRequest req = buildWebServiceRequest(sURL);

        //Second, invoke web service call 
        if (mode=='live') {
            res = invokeWebService(h, req);
        }
        if (mode=='live') {
            reader = res.getXmlStreamReader();
        }else{
            String str = '<bitly><results shortUrl="<a href="http://bit.ly/QqHEm" target="_blank" rel="nofollow">http://bit.ly/QqHEm</a>">Foo bar</results></bitly>';
            reader = new XmlStreamReader(str);
        }
        return readXMLResponse(reader,'shortUrl');
    }

    public static HttpRequest buildWebServiceRequest(String purl){
        String endpoint;
        HttpRequest req = new HttpRequest();
        endpoint = '<a href="http://api.bit.ly/shorten?version=2.0.1&format=xml&history=1&longUrl='" target="_blank" rel="nofollow">http://api.bit.ly/shorten?version=2.0.1&format=xml&history=1&longUrl='</a> + purl + '&login=tseth&apiKey=R_948fa681da46221f969e83b2ba52d31e';
        req.setEndpoint(endpoint); 
        req.setMethod('GET');
        return req;
    }

    public static HttpResponse invokeWebService(Http h, HttpRequest req){
     //Invoke Web Service
     HttpResponse res = h.send(req);
     return res;
    }

    public static String readXMLResponse(XmlStreamReader reader, String sxmltag){
        string retValue;
        // Read through the XML
        system.debug(reader.toString());
        while(reader.hasNext()) {
            if (reader.getEventType() == XmlTag.START_ELEMENT) {
                if (reader.getLocalName() == sxmltag) {
                    reader.next();
                    if (reader.getEventType() == XmlTag.characters) {
                        retValue = reader.getText();    
                    }
                }
            }
            reader.next();
        }
        return retValue;
    }      
}
2
  • In it, where do I have to post my URL? And when I tried to save this class, I am getting an error saying "Compile Error: line 32:103 no viable alternative at character '"' at line 32 column 103 ' " It is coming in the code where we are setting the ENDPOINT. Suppose my URL is "developer.salesforce.com/forums/?id=906F00000008z1jIAA". Where do I mention this in my code to shorten it?
    – happy
    Oct 6, 2015 at 5:06
  • How can I write the test class for this. Please can you guide?
    – happy
    Oct 6, 2015 at 8:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .