Wednesday, March 23, 2011

Salesforce.com and validation rules

Salesforce.com supports validation rules that are evaluated when DML is performed on a record. These rules can be defined using formulae (which means you can get far more sophisticated than "required field") and the error messages can be presented in user-friendly language.

This is a far better experience than specifying required="true" for each apex:inputField, which can be too simplistic to meet business rules and will return field labels that may not necessarily be user-friendly (e.g. "Date Customer Last Contacted for Product Category A" is not suitable for customers completing a satisfaction survey).

Unfortunately these validation rules are not evaluated simultaneously - they are worked through one by one until there is an error or no rules are left. This means that for a page with 20 validation rules, the end user may have to fix 'n' click 20 times before they can save the page contents.

The only way I'm aware of at present to perform simultaneous evaluation AND present user-friendly error messages is to codify the validation rules in the controller class - bleagh!

Any better suggestions?

Friday, March 4, 2011

Forcing numeric data type in Excel

Excel data types are particularly significant in the following situations:
  • Formulae that perform comparisons such as Vlookup
  • Using Excel as a data source in a join query when you have poor control over type casts e.g. while using Microsoft Query
You can set the formats of cells, but this won't update until you actually edit the cells. If you have a lot of cells it's not feasible to edit them all individually.

I find the quickest way to force the conversion of text to number is to do the following:
  1. Enter 1 in a cell
  2. Copy the cell
  3. Paste special into the range of cells you want to convert
  4. Specify Value and Multiply
The cells you selected will be converted to numeric type.

Numeric is the easiest as Paste Special has number-specific options. For other data types I think you have to create a copy of the cells using a casting formula and use the calculated values instead of the originals. If I find any faster ways for non-numbers I will update this post.