Search
Twitter
Thursday
Sep302010

Base-64 Encoding in Apex

The other day someone came to me and asked me how to Base64 encode some parameters in an Apex class. Base64 is often used when you need to encode binary data into characters. Base64 is a good way of taking binary data and turning it into text so that it can easily be transmitted in things like HTML form data and email. Salesforce.com makes it pretty easy to perform Base-64 encoding in Apex via their EncodingUtil class. Below is an Apex code snippet with a very simple example of the base-64 encode/decode.

    string before = 'Testing base 64 encode';
    
    // create a blob from our parameter value before we send it as part of the url
    Blob beforeblob = Blob.valueOf(before);
   
    // base64 encode the blob that contains our url param value
    string paramvalue = EncodingUtil.base64Encode(beforeblob);
   
    // print out the encoded value to the debug log so we can see it before/after base64 encode
    System.debug(before + ' is now encoded as: ' + paramvalue);
       
    // take the base64 encoded parameter and create base64 decoded Blob from it
    Blob afterblob = EncodingUtil.base64Decode(paramvalue);
   
    // Convert the blob back to a string and print it in the debug log
    System.debug(paramvalue + 'is now decoded as: ' + afterblob.toString());

You can refer to EncodingUtil documentation here for more information. In addition, you can take a look at the documentation for the primiatve data type 'blob' here.

 

Wednesday
Sep222010

Salesforce.com Unveils Chatter 2

In less than a year, Salesforce.com has gone from almost no social media type functionality to a fully loaded social platform. Chatter 2 brings more new tools to the table in hopes that more users will begin to use it. The current adoption rates are incredible though, so for most of us - this is all bonus functionality. Here is a list of some of the new features to take advantage of:

  • Files - you can now drag-and-drop files from your desktop directly into Chatter. In my view, this is a big one. I can't tell you how much time this will save.
  • Email digest - great for those of us who don't have time to sit and watch the latest posts and feeds. You can now setup daily or weekly recaps.
  • Topics - just like the has tags we're all familiar with in Twitter.
  • Chatter filters
  • Analytics - as a system admin this is great. I can now quickly generate a report and email it off to the department heads.
  • Desktop integration through Adobe Air. This allows you to see the latest feed without keeping a browser window open. Screen realestate is a precious commodity - this a nice add-on.
  • Search, recommendations, and more...

 

Monday
Sep202010

Regular Expressions in Apex Code

Every once in a while it's very helpful to use regular expressions in your code. The problem is that resources and examples are often scattered. I wanted to give two simple examples of how you can write and test regular expressions in your Apex code inside Salesforce.com.

Return all the numeric characters in a string via regular expression

For the sake of quickly writing and testing this, I've just created a new Lead trigger that runs before insert. I've then set a test input string "input" and I'm using a Pattern object to return the matches in a new string "test". I then print a system debug statement so that I can look in the debug logs to see my results.

trigger myLeadTrigger on Lead (before insert) {

    string input = 'a3f45qq456';

    for (Lead l : Trigger.new)
    {
        //instantiate new Pattern object        
        Pattern p = Pattern.compile('[^0-9]');
        

        //return a string that contains only numeric values from 0-9 from my original input
        String test = p.matcher(input).replaceAll('');
        

        //print a debug statement with my test results
        System.debug(test);

    }

}

Ouput from the debug log shows us that this is working:

16:5:8.981|USER_DEBUG|[12,9]|DEBUG|345456

As you can see, "345456" are the alpha chars from the original string "a3f45qq456".

 

Return all the alphabetical characters in a string via regular expression

For the sake of quickly writing and testing this, I've just created a new Lead trigger that runs before insert. I've then set a test input string "input" and I'm using a Pattern object to return the matches in a new string "test". I then print a system debug statement so that I can look in the debug logs to see my results.

 

trigger myLeadTrigger on Lead (before insert) {

    string input = 'a3f45qq456';

    for (Lead l : Trigger.new)
    {
        //instantiate new Pattern object        
        Pattern p = Pattern.compile('[^a-zA-Z]');
        

        //return a string that contains only numeric values from 0-9 from my original input
        String test = p.matcher(input).replaceAll('');
        

        //print a debug statement with my test results
        System.debug(test);

    }

}

 

Ouput from the debug log shows us that this is working:

6:46:21.762|USER_DEBUG|[14,9]|DEBUG|afqq

As you can see, "afqq" are the alpha chars from the original string "a3f45qq456".

 

 

Friday
Sep172010

Winter '10 Release 

Code Scheduler & Batch Code Processor

I'm just now getting the chance to sit down and read the release notes for the Salesforce.com Winter '10 release. There are some cool things going in from a developer standpoint (not to mention as an end user).

Salesforce.com Code Scheduler

The code scheduler being introduced is awesome. It's a cron-like mechanism that allows you to configure when your processes run so that you don't have to kick them off manually. It sounds simple, but this is a really nice thing to have. You can no create execution schedules and kick off Apex code. The Salesforce.com code scheduler also allows you to monitor/edit schedules programmatically (as well as through the UI).

Salesforce.com Batch Code Processor

With the batch code processor, you can now batch/deploy asynchronous processes. This allows you to perform operations on an entire set of data within a single batch process. I can think of about 101 uses for this. The example they give in the release notes is perfect - building a process to validate all your account addresses and have it run in the background.

Read the full Salesforce.com Winter '10 release notes here.

Wednesday
Sep152010

Salesforce Chatter Going Mobile

Earlier this week, Salesforce.com announced that they are taking Chatter to the smart phone. While we've expected this for some time now - it is exciting news. Mobile devices are becoming the new enterprise desktop. IDC research is placing the number at more than 50 percent of the workforce.

"The combination of devices like the iPad or the new iPod touch with mobile apps like Chatter that push information to you in real-time are making the desktop obsolete."

-  Marc Benioff, chairman and CEO of Salesforce.com.

What excites me about Chatter going mobile is the clear commitment by Salesforce.com to bringing business and social media together. Chatter was officially launched back in June. They are reporting that nearly 20,000 companies have deployed the platform. I've been in a number of user group meetings over the summer and the topic always comes up by someone in the group, "have you guys implemented Chatter for your users?" As the summer has progressed I've been hearing less and less people say, "not yet." What seems to be a clear indicator of a successful Chatter adoption by end users is they buy in from key executives. When this would be raised in the group, at least one admin would always chime in, "..but our CTO is so tied up in meetings and trips he doesn't have time." By rolling out Chatter Mobile, this is no longer a roadblock.

Chatter Mobile apps for iPhone, Blackberry and iPad will be in the market later in 2010. Support for Chatter Mobile on Andriod devices should be available next year. 

Salesforce Chatter is available at no additional charge to app paying subscribers of Salesforce CRM and Force.com.

 

 

Page 1 ... 29 30 31 32 33