Friday, April 29, 2011

Data three ways from Force.com…




Over the past couple of months there has been a lot of chatter about some new ways to access your Salesforce.com data. The REST API was released earlier this year as well as something called JavaScript remoting that is in developer release. I currently use standard Apex and Visualforce when creating custom apps on the platform so I thought I would take some time to better understand the REST API and JavaScript remoting.

I had a fun night playing with the code samples provided by Salesforce and thought I would share some examples. Below are three different ways you could access your data on the platform. My goal was very simple…query Salesforce for a list of accounts and display them in a table. Not earth shattering at all but hopefully the simple use case will illustrate three different approaches. Each approach will output a data table exactly like this:





Recipe 1: Using standard Visualforce page with Apex class

The first approach I took was to use an Apex class to query the data and then use a Visualforce page to display it. This is pretty standard Force.com development and should look familiar.

Here is the Apex class:
public with sharing class dataTableOne {

 public List getAccounts() {
  
  List accountsForPage = [Select Id,Name,Phone From Account Limit 10];
  return accountsForPage;
  
 }
}


Here is the Visualforce page:

 
Data retrieved using standard APEX/Visualforce
Account Name Account Id


Recipe 2: Using JavaScript Remoting

When I first saw Josh Birk (@joshbirk) demo JavaScript Remoting at the Minneapolis user group meeting my reaction was, “That looks a lot like those old s-controls we use to write before APEX/Visualforce”. After he talked me off the ledge I got a better understanding of what situations you might use remoting (mobile dev, no viewstate, etc). I used his blog post here to build my recipe below.

Here is the Apex class:
global class dataTableTwo {

    @RemoteAction
    global static Account[] findAccounts() {
       
        Account[] accountsForPage = [Select Id,Name,Phone From Account Limit 10];
        return accountsForPage;
    }


}

Here is the Visualforce page:


 
 
Data retrieved via JavaScript Remoting

Data retrieved via JavaScript Remoting


Recipe 3: Using the REST API and JavaScript toolkit

The last recipe uses the REST API to access the data from Salesforce and has no APEX class. I leaned heavily on Pat Patterson’s (@metaDaddy) blog post and the JavaScript toolkit he created. Thanks Pat! You can see below that once you include his toolkit as a static resource it is just a few lines of code to query data.


Here is the Visualforce page:


 
    
    
    
    
Data was retrieved via REST API

Hope this was helpful. Love to hear any questions or feedback.

Monday, April 18, 2011

The ABCs (I need x and y) of Force.com development…









A while ago I saw this blog post, “The ABCs of Web Development” by Siddharth. I enjoyed it so much I thought it might be fun to try to do the same thing with Force.com development. A draft has been sitting in Evernote for a couple weeks now and I only needed X and Y (XML seemed too easy) to complete. So I decided to put out my incomplete list and see if anyone had an X, Y or any other concepts/terms they might stick in here. I will update and give you credit if you send me one.

Below is what I have so far. I'd love to hear your thoughts or additions!


APEX - APEX is a programming language created by Salesforce.com that is hosted and run entirely on the Force.com platform. 

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex

API - API stands for "Application Programming Interface". Think of it as a set of predefined methods you can call to interact with your Salesforce.com instance. 



Batch APEX - What you use to create large, complex, long-running jobs on the platform.


C

Custom Object - A custom object is pretty much the same thing as a database table. They can be defined through the declarative UI to extend your Salesforce data and have a number of field types you can add to them. 



DML - DML stands for "data manipulation language". This is basically all the different things you can do to modify your data in Salesforce (insert, update, delete, etc.).



Database.com (Submitted by @knhornt) - "Database.com is the world’s first enterprise database built for the cloud. It's open, proven and trusted. Database.com is built to power the next generation of Cloud 2 social and mobile enterprise apps." Definition from the Database.com website. This new Salesforce offering was announced at Dreamforce 2010.

http://www.database.com/


Data Loader (Submitted by @ashoknaglikar) - Desktop tool that allows you to easily access your Salesforce data.


http://wiki.developerforce.com/index.php/Apex_Data_Loader


Eclipse - Eclipse is an open source IDE in which you can develop your APEX and Visualforce code.


F

Flex - Flex is an open source framework for developing rich internet applications. Using Salesforce's APIs you can create very slick applications using Flex. There are a number of built-in UI libraries, as well as a powerful charting library that allows you to create powerful dashboards. 


Force.com Explorer (Submitted by @ashoknaglikar) - Direct from the developer.salesfore.com website: "The Force.com Explorer (beta) is a new cross-platform Force.com database explorer built with Adobe Flash Builder for Force.com. It's an AIR app, so you can get started pretty quickly: click here to install."
G

Governor Limits - Since you are developing in a shared environment Salesforce has implemented a number of limits to make sure your code is not taking up too many resources. If you reach a governor limit, your code will throw an error and stop running.


H

HTML - HTML stands for "Hypertext Markup Language" Ultimately all of your Visualforce code gets turned into HTML, so if you have an understanding of HTML you will be okay.



Heroku  (Submitted by @knhornt) - Heroku is a could based platform for developing Ruby applications. Salesforce announced that they would be buying Heroku at Dreamforce 2011.


 http://www.heroku.com/

I

IDE - IDE stands for Integrated Development Environment. The current IDE of choice for Force.com development is Eclipse. However, there a number of cloud based IDEs (BrainEngine) popping up that might give it a run for its money.


J

 jQuery - jQuery is an extremely fast and powerful JavaScript library that is commonly used by Force.com developers. It can be downloaded for free and uploaded to your Salesforce.com org as a Static Resource.


JSON (Submitted by @osamanasir) - JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language,


K

Keywords - A set of reserved words that you cannot use when naming variables, methods, or classes.


L

Log  (Debug Log) – “A debug log records database operations, system processes, and errors that occur when executing a transaction or while running unit tests. The system generates a debug log for a user every time that a user executes a transaction that is included in the filter criteria”.


M

MVC -  MVC stands for Model-view-controller. It is a software design pattern that is used in Force.com development. The idea is to separate the Model (Database or Custom Objects in SFDC) from the View (User interface or Visualforce in SFDC) from the Controller (Business Logic or APEX code in SFDC). 



N

nullPointerException - If you have ever done any APEX coding, you have probably run into this error. It is defined as "Any issue with dereferencing null".  It can be a frustrating little error that is sometimes hard to debug. You can find some examples of it in the APEX manual below.


O

Org - This term refers to a particular instance of Salesforce. 

P

Production - This refers to your "production" instance of Salesforce. All APEX coding must be done in a Sandbox or Developer instance and pushed to production once code coverage is reached.
Q

Query (SOQL and SOSL) - I know this is cheating a little bit since Salesforce's query languages actually start with S and not Q but I had a hard time finding a Q. Anyways, Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL) are two ways that you can query Salesforce for data. SOQL has a syntax that is very similar to SQL and is primarily used to query Salesforce objects for a list of records that match a specific criteria. SOSL is used to search a list of lists of objects that contain a given search term.


R

Rerender attribute - Used in Visualforce to do partial page refreshes and create cool user interfaces. Used in this code sample:


S

standardController - This is the pre-built Visualforce controller that you reference if you create an Extension for a Visualforce page.  



Sandbox - (Submitted by @_siddhesh) - A sandbox is a replication of your production environment in which you do Force.com development before pushing to production. Below is a link that outlines the different types of Force.com environments.


http://wiki.developerforce.com/index.php/An_Introduction_to_Environments

Sites - (Submitted by @_siddhesh) Force.com sites allows you to create public facing web sites and applications that run on the Force.com platform and can leverage data in your Salesforce org.

http://developer.force.com/sites
http://wiki.developerforce.com/index.php/An_Introduction_to_Force.com_Sites

T

Trigger - Triggers are used to invoke APEX based on data changing in your Salesforce org. Triggers can be run on most objects when records are inserted, updated, deleted, merged, upserted and undeleted.


U

Unit Tests - "A unit test is code that exercises a specific portion of your codebase in a particular context." In order to get your code from a development or sandbox environment to a production environment you need to have 75% code coverage. This code coverage is gained by writing unit tests for your code. Basically, it is verifying that your code runs the way you think it should with no errors.


V

Visualforce - Allows you to create any user interface to run on the Force.com platform. 


W

Webservice methods - You can expose your own APEX class methods as a custom web service using the "webservice" keyword. This allows external applications to make calls to your APEX code within Salesforce.


X
Y

YouTube Salesforce Channel  (Submitted by @pbattison) - A collection of videos that are all about Salesforce.com. This site is maintained by @CRMFYI

ZIP archives - This is one of my favorite features of Force.com development. You can upload a zip file as a Static Resource with a bunch of images, JavaScript code, or whatever and then reference it on your Visualforce pages.