Start saving time with snippets

February 26th, 2009 2 Comments »

One powerful feature of Eclipse and the Force.com IDE that I’ve recently begun using is snippets. When you find yourself writing the same piece of code repeatedly, create a snippet.  I wrote a snippet today to map objects to lists of child objects. This is a task I find myself doing quite often, so it makes sense to have a snippet for it.

To create a snippet in eclipse,  highlight the block of code that you want to create your snippet from and right click it. In the context menu select ‘Add to Snippets..’.  You will then be prompted to select a category for your snippet or create a new one.  After that you are brought into the snippet editor with your chunk of code populated in the ‘Template Pattern’ section. From here you can create variables and put them in your template. After you’ve finished your template click ‘ok’ and your snippet will now be available for resuse.

 To use your snippet you need to have the snippet view on your Eclipse layout. To open the snippet view go to Window > show view > snippets. With the snippet view open, double click on the snippet that you want to use, enter in values for the variables you set and hit insert.  That’s it.

Here is the snippet I wrote today for mapping objects to lists of child objects:


List<${childObjectType}> ${listName} = new List<${childObjectType}>();
Map<Id,List<${childObjectType}>> ${mapName} =
                        new Map<Id,List<${childObjectType}>>();

//TODO: populate ${listName}

for(${childObjectType} childObj : ${listName})
{
	if(${mapName}.containsKey(childObj.${lookupField}))
	{
		List<${childObjectType}> childObjs =
                           ${mapName}.get(childObj.${lookupField});
		childObjs.add(childObj);
		${mapName}.put(childObj.${lookupField}, childObjs);
	}
	else
		${mapName}.put(childObj.${lookupField},
                      new ${childObjectType}[]{childObj});
}

A Rose By Any Other Name…

February 3rd, 2009 No Comments »

One of the great things about developing for salesforce.com is that there are any number of options when it comes to language support- toolkits are available for pretty much every flavor of programming language a modern application might utilize, so you’re free to pick and choose as the situation and your skillset may dictate.

Earlier, we looked at some different tools that are useful for developing on the Force.com platform- these were really aimed at supporting and enhancing your ability to use salesforce.com’s native languages, Apex and VisualForce, as well as perform data manipulation tasks.  Today, we’ll be taking a look at just a few of the various libraries that exist today that make developing for salesforce.com so versatile.

Ruby on Rails

Ruby is my latest kick, and there are a couple ’staples’ when it comes to integrating with salesforce.com from a Ruby or Rails application. 

RForce is what I would consider a ‘low-level’ gem, that creates a direct binding to the salesforce API.  It requires a little bit of knowledge to setup and use properly.  The project documentation has a simple example of creating and using the RForce binding to connect and use some basic API calls, but I have found it’s mostly trial and error when it comes to actually using it in a project.

ActiveSalesforce is another gem that actually utilizes the RForce low-level binding, but wraps it in an easier-to-use package.  It allows you to set a salesforce.com org as a datasource in your database.yml config and interact with it just as you would any other database- create a model class to represent your Account object, for instance, and you can use all the built-in Rails functionality to search through it:

accts = Account.find(:all)

Both of these gems are pretty easy to use, once you’ve seen a few code samples.  Examples may be a bit hard to come by, but I plan on posting some samples here from time to time.

One of my current pet projects is a gem wrapper around the Metadata API for salesforce.com- it’s in an early stage, but the possibilities are exciting.

JavaScript/AJAX

Salesforce has written a JavaScript wrapper around their Web Services API, known as the AJAX Toolkit.  It’s hosted and available on every salesforce.com server that has API access enabled- you can simply add the code below to an S-Control to include the library on your page.

<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>

S-controls, and the AJAX toolkit, are beginning to go the way of the dinosaur with the advent of VisualForce and Apex, however, they are still a viable solution for many situations facing developers today.

Next in this series:  A rose by any other name… Part 2

Handy Tools for Force.com Developers

January 28th, 2009 1 Comment »

Let’s face it, salesforce.com’s UI for creating or editing Apex, VisualForce, or other code is pretty sparse. There’s really no support for development from within the standard UI… Luckily, there are a plethora of tools out there to make development easier.

Here are some of the tools we’ve found useful:

Force.com IDE - Built on Eclipse

A full-fledged IDE based on the open source Eclipse platform, the Force.com IDE is generally the choice for developers working in Apex, VisualForce, or on S-Controls. It has many great features, including the ability to deploy not just code, but also things like page layouts, workflows, and reports between orgs.

It could use a bit more development, as the syntax highlighting and code auto-complete features are scant at best, but overall is a great tool for anyone working with salesforce.com on a more technical level.

Data Loader

The Apex Data Loader is a tool for mass manipulation (creation, updating, deletion) of records in salesforce.com. While things like the PHP Workbench (see below) can also be used for data manipulation, the Data Loader has the advantage of creating success and error files for each operation, allowing you to more easily keep track of the records you are dealing with.

The Data Loader is not actually available from the page above, but must be downloaded from within an org. From the Setup menu, go to Administration Setup -> Data Management -> Data Loader. This tool is only available for Unlimited, Enterprise, or Developer orgs.

PHP Workbench

The PHP Workbench is an invaluable tool for quickly investigating the schema of an org, running once-off queries on data, or executing anonymous Apex blocks without the hassle of creating a new Eclipse project for an org.

There is functionality for Insert/Update/Delete of records, but I would be a bit hesitant to do so for anything important. The logging features (success and error files) of the Data Loader are a bit more robust than the Workbench.

Next in this series: we explore some of the various toolkits used to interact with salesforce.com