Stat Tracker

Tuesday, January 18, 2011

Using Visual Flow in Salesforce Spring 11

NOTE: As of Spring 12 all Orgs will have Visual Flow for FREE! SWEET! Also, this post is a little old, there is already an update with the new Cloud Flow Designer.

You know what can be tedious to work with? Wizards. And I’m not talking about wizards like Gandolf who can shoot lighting and carry swords with a +10 mana regeneration rate.  I’m talking about wizards that navigate a user through a flow or decision making process. Many programming languages and platforms provide setup wizards in their IDE’s that allow you to create a wizard framework quickly. This frees the developer from manually creating all the framework code to build one. 

As of Salesforce Winter 11, this was something that was lacking in Salesforce and Force.com platform.

You’d have to manually create a bunch of Visualforce Pages and Apex Controller and flow the user through them manually, which was a pain!

In Spring 11 however, we are getting Visual  Flows GA (Generally Available). This was previously available in Pilot for Winter 11 release under the name “Visual Process Manager”. This allows you to build business processes quickly by drag, dropping, and connecting items in the Flow Designer tool. In my Spring 11 Release org I’ve been playing around with it and it’s pretty sweet. You can even invoke Apex methods from the flows for reusable / complex logic.

Use Case: New Account Wizard

This use case will create a new account using the Visual Flow tool. To create a Visual Flow, we need to do a few things first. 
  1. Gain access to a Salesforce Pre-Release Spring 11 Org. https://www.salesforce.com/form/signup/prerelease-spring11.jsp
  2. Enable the user as a “Force.com Flow user”. You can access this in Setup -> Manage Users -> Edit A user. Check the “Force.com Flow User” box.
  3. Download the Flow Designer tool from Developer Tools and install. You can access this in Setup -> Create -> Workflows & Approvals - > Flows. The download link is on the screen.
Once we have our environments setup and our user configured as a Flow user, we can create a flow. I recommend following the User Manual PDF provided in the Flow Designer tool to learn how to create a beginning flow (Help -> User Manual). It contains a pretty decent quick tutorial, but it’s a little lightweight. For example, the flow tutorial only provides a flow which does a calculation. It does not utilize the Lookup or Update elements which are critical to a lot of workflows. But it does introduce you to the concepts.

After following the tutorial, I updated my flow to use the Data Update element and the Send Email element to perform an Account insert as well as email me a confirmation email that the account was created. I am including my test flow file so you can use it in your org. 

You can grab the complete test flow file here: Google Code - TestFlow File.

When I finished, my flow looked like this in the Flow Editor:


To test this flow, I created a homepage component that linked to the flow allowing the user to execute it from their homepage. As you can see, the flow will execute in a new window and the user can go through the generated screens and create a new Account.

1. User clicks the Homepage Component to execute the wizard:
 2. Wizard starts up and user goes through the prompts to create the record.


And finally we can see the account record created in the Accounts Detail Page:

You can also embed these flows inside Visualforce Mashups so maybe I’ll play around with that next.

Overall, this is pretty exciting for developers and even administrators of Salesforce. I can see a lot of efficiency by building all sorts of custom wizards quickly using this tool. This will probably be heavily utilized in all those call center applications. 

Friday, January 14, 2011

Generating a Barcode in a Visualforce Page using Javascript

You can generate a barcode in a Visualforce Page using Javascript pretty easily. Some folks forget that you can execute Javascript in a Visualforce page. This opens you up to a lot of the goodies you can do with the client browser.

The trick is to find the right Javascript library which provides the type of barcode you want to generate, or you could always write your own.

At a highlevel, the steps you need to do are:

1. Upload a JS Library (someJSLib.js) to your Static Resources in Salesforce.
2. Reference the JS Library in your Visualforce page by using the <apex:includeScript> tag.
3. Execute the Javascript functions to generate the barcode content and display in your page.

For this example, I am using a Barcode 39 Javascript library provided by http://www.codeproject.com/KB/HTML/Code-39-Barcode.aspx.

1. First I upload the JS Library to my Static Resources:
  

2. After I have uploaded the library, I embed the JS file in my VF Page using the <apex:includeScript> tag:
<apex:includeScript value="{!$Resource.BarcodeScript}"/>

3. I execute javascript in the Visualforce page to generate the barcode (Full VF Page Code):
<apex:page standardController="Position__c">
    <apex:includeScript value="{!$Resource.BarcodeScript}"/>
    <apex:detail relatedList="false"></apex:detail>
    <br/>
    <br/>
    <div id="inputdata">{!Position__c.Name}</div>
    <div id="externalbox" style="width:5in"></div>
    <script type="text/javascript">
    /* <![CDATA[ */
      function get_object(id) {
      alert('Executed');
       var object = null;
       if (document.layers) {
        object = document.layers[id];
       } else if (document.all) {
        object = document.all[id];
       } else if (document.getElementById) {
        object = document.getElementById(id);
       }
       return object;
      }
      get_object("inputdata").innerHTML=DrawCode39Barcode(get_object("inputdata").innerHTML,1);
     /* ]]> */
    </script>
</apex:page>


4. When I view this page it will take the Name field from my Position__c object and encode it in the Barcode:

That's all it takes!

The only challenging part really is finding JS Libraries that are opensource and support your barcode format.

Here are some websites that have JS Libraries available:

EAN-13: http://www.parkscomputing.com/barcode.html
Barcode 39: http://www.codeproject.com/KB/HTML/Code-39-Barcode.aspx
Barcode 39: http://www.atalasoft.com/cs/blogs/loufranco/archive/2008/04/25/writing-code-39-barcodes-with-javascript.aspx

Monday, January 10, 2011

Salesforce Spring 11 Release Update - Governor Limits

I just finished reading the Salesforce Spring 11 Release Notes and I am floored by the new Force.com updates. Some of my most frustrating headaches are getting alleviated. Not completely eliminated, but alleviated by a huge amount.

When I first started working on the Force.com Platform, coming from a J2EE background, I was instantly frustrated with the governor limits enforced on Apex code. Only 1000 records in all my SOQL queries in a Trigger context? Only 20 DML statements? It has been the most frustrating part of an otherwise simple development platform.

Well, in Spring 11, we are getting some huge updates!

First, all Apex code will now execute in a single context. Previously we had two context (Trigger, and Anonymous/Controller/Everything Else). These two context had their own governor limits, with the Trigger context being the most restrictive. Well, not anymore! All Apex code will now execute in one context! WOW.

And if thats not all, there's more!

Secondly, the total number of records in a context's SOQL queries has increased to 50,000! Yeah, 50,000! Previously in a Trigger you could only get 1,000. Thats 50 times the number of records!

Oh, and almost forgot. Full REST API is generally available!

Man, what an awesome release for developers.

Check out the full release notes here: Salesforce Spring 11 Release Notes