The post Use jQuery + Plugins with Visualforce Pages appeared first on Justin Silver.
]]>The jQuery library makes it much easier to create cross-browser functionality on any web page, and Salesforce Visualforce pages are no exception. Going one step further and include jQuery plugins in your pages as well for even more functionality and sparkles. But what do you do when you want to use a more recent version of jQuery, and your plugin is being reported as an undefined function in the console? To the rescue comes jQuery.noConflict(true);
.
First you will want to upload both jQuery and your plugin as Static Resources, or from a CDN if you prefer. For our purposes we will assume they are Static Resources called jQuery
and jQueryPlugin
. As soon as the javascript sources is includes – jQuery first followed by plugins – we need to make a call to noConflict()
to free up the $
resources. Using jQuery
as an alias is easy and logical.
Next we want to tell our code to run after the DOM of the page has loaded, so we wrap our code inside a function that is called by jQuery.ready()
. A neat trick here is to pass jQuery back into the callback function as $
, so your code will know the version of jQuery
you loaded and it’s attached plugins as the standard $
.
<!-- include static resources --> <apex:includeScript value="{!$Resource.jQuery}" /> <apex:includeScript value="{!$Resource.jQueryPlugin}" /> <script type="text/javascript"> // Alias to jQuery and free $ jQuery = $.noConflict(true); // Call jQuery.ready(), and alias jQuery back to $ by passing as an argument jQuery(document).ready(function($){ // use $.plugin() $('#container').plugin(); // attach events to DOM elements $('.button').on('click', function(){ alert('clicked!'); }); }); </script>
The post Use jQuery + Plugins with Visualforce Pages appeared first on Justin Silver.
]]>The post Loggly.com Logging with Salesforce.com Apex appeared first on Justin Silver.
]]>If you want to send log messages to Loggly.com you will need to implement their HTTP API interface. All that is required to send data to this interface is an HTTP POST to a url containing your API token with the log message in the body. Tags can be applied either in the URL after your token or using theĀ X-LOGGLY-TAG
. Setting the Content-Type
header is also required with value of text/plain
.
Worth noting that you should keep your Apex Limits in mind in regards to number of callouts and possibly implement a different scheme depending on your volume. Don’t forget to add “https://logs-01.loggly.com” to your Salesforce.com>Setup>Security Controls>Remote Site Settings.
This class uses theĀ @future (callout=true)
annotation to make an HTTP POST to Loggly.com
public abstract class LogglyService { // Use a future method to make a callout @future (callout=true) public static void log(String data, String[] tags) { // this should come from config somewhere final String api_key = 'GET YOUR API KEY FROM SOMEWHERE'; // objects to make the request HttpRequest request = new HttpRequest(); HttpResponse response = new HttpResponse(); Http http = new Http(); // setup the request request.setEndpoint('https://logs-01.loggly.com/inputs/'+api_key); request.setMethod('POST'); request.setHeader('Content-Type', 'text/plain'); // add tags in the header request.setHeader('X-LOGGLY-TAG', String.join(tags, ',')); // put the log message in the body request.setBody(data); try { // send the request to Loggly response = http.send(request); } catch(System.CalloutException e) { System.debug('Loggly error: ' + e); } System.debug(response.toString()); } }
Post a sample message to Loggly.com using this anonymous apex.
List<String> tags = new List<String>{'testTag'}; LogglyService.log('Test Log Message', tags);
The post Loggly.com Logging with Salesforce.com Apex appeared first on Justin Silver.
]]>