You need to sign in to do that
Don't have an account?
Force.com
Bitly (Url Shortening) Integration with Salesforce
I want to embed javascript coding in visualforce. The javascript coding is for integrating 'Bitly' platform with salesforce.
My visualforce code is:
<apex:form > <apex:inputText value="{!Candidate__c.Long_Url__c}" id="theText" onclick="javascript:go('{!$Component.theText}')" ></apex:inputText> <script type="text/javascript" src="/js/functions.js"></script> <script src="/soap/ajax/11.1/connection.js"></script> <script language="javascript"> function go(f) { var theText = document.getElementById(f).value; alert(theText); } var xhr = new XMLHttpRequest(); xhr.open("GET", "http://api.bitly.com//v3/shorten?login=tseth&apiKey=R_948fa681da46221f969e83b2ba52d31e&longUrl="+theText); xhr.onreadystatechange = function(){ alert('test'); if(xhr.readyState == 4) { if(xhr.status==200) { var jsonResponse = JSON.parse(xhr.responseText); var bitlyUrl = jsonResponse.data.url; myFunc(bitlyUrl); var t = xhr.send(); } } } </script> </apex:form> <apex:form > <apex:actionFunction name="myFunc" action="{!field}"> <apex:param name="param1" value=""/> </apex:actionFunction> </apex:form> </apex:page>
Bitly is used for URL shortening. I want the shortened url to be passed to the controller through <apex:actionfunction>.
Please help. Its really urgent. I am completely stuck.
Thanks
Not sure how to approach this via javascript, but this code works if you create a class, set up the remote site, and call it using the code at the bottom - so you could pass a URL into this class, and get back the string in the controller...
I would probably rework the XML read method to use the DOM functionality, and I typically store the login/api credentials somewhere else (e.g. a custom setting) but I tried this in my dev org, and it works fine...
All Answers
Not sure how to approach this via javascript, but this code works if you create a class, set up the remote site, and call it using the code at the bottom - so you could pass a URL into this class, and get back the string in the controller...
I would probably rework the XML read method to use the DOM functionality, and I typically store the login/api credentials somewhere else (e.g. a custom setting) but I tried this in my dev org, and it works fine...
Thanks a lot. I can now call the Bitly platform through salesforce.
Thanks
Im working on same functionality.
Using the same code what u have posted.
im getting this error:
Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://api.bit.ly/shorten?version=2.0.1&format=xml&history=1&longUrl=http://flourish.sandbox1.cs7.force.com&login=tseth&apiKey=R_948fa681da46221f969e83b2ba52d31e
An unexpected error has occurred. Your development organization has been notified.
what to be changed for endpoint, in bitly class??
You'll need to add the Bitly URL to your Remote Sites...
Go to Security Controls, Remote Site Settings and add http://api.bit.ly as an authorized site...
I have added its working fine.
Just out of curiosity, what would be the advantage of changing the APEX code above to using DOM parsing?
I was running the code in a batch process (i.e. no UI) - so I needed the values in an Apex format that I could then append to records.
"Error: Compile Error: line 32:118 no viable alternative at character '"' at line 32 column 118"
Below is Line 32. I attempted removing the " symbol, but it only led to more errors.
Any suggestions?