Posts Tagged ‘force.com’
Salesforce: A better way to work with jQuery selectors and Visualforce Component Ids

Irregular Expressions
I get very sad when discussing this particular topic. There are a variety of ways of get Visualforce component Ids and using them in JavaScript but all of them keep me awake at night. Srsly. A commenter on one of my posts got me thinking about how we can do this better and I’ve come up with a way that I think is great. Hopefully you’ll agree.
This post means that my older posts here and here are now retired in favour of this method.
If the world was on the brink of nuclear war with no clear path to peace what could you count on to save the day? Regular Expressions of course. If a meteor the size of Pluto was about to crash into Earth and Bruce Willis was too old to land on it and blow it up what could we count on to rid us of the troublesome rock. Yes that’s right, Regular Expressions. I think you can guess where I’m going with this.
jQuery has the ability to understand very simple regular expressions in it’s attribute selectors. The full documentation can be found here.
To solve our particular problem however the code is simple:
<apex:page> <head> <style> a,span{ display:block; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script> jQuery(document).ready(function($){ $('#btn').click(function(e){ e.preventDefault(); console.log('The following element was found when looking for an id of \'output1\':'); console.log($('[id$=output1]')); /* Here's where we're grabbing the element. */ }); }); </script> </head> <apex:outputText value="She sells seashells by the seashore." id="output1"/> <apex:outputText value="Peter Piper picked a pack of pickled peppers." id="output2"/> <a href="" id="btn">Click me.</a> </apex:page>
The important part here is the selector $(‘[id$=output1]’) which says, “Find the id value that ends with ‘output1′”. This comes with a warning though! Do not duplicate the Visualforce Id that you give to your elements otherwise this piece of code will find all of them.
When I first wrote this post I used a selector extension library that gives you the full power of JavaScript-based regular expression but Ryan Fritts has rightly shown that the above will deal with 99% of use cases and is simpler. For those of you that need to deal with the extra 1% I’ve implemented a wrapper to regex selector as an example. It does exactly what jQuery is doing above and gives you access to the regex flags as documented here.
Thanks again Ryan!
How many Salesforce Roles do you have in your Org?
I’ve worked with many Org instances in my career with salesforce.com and the Force.com Platform and was stunned by an Org that had a massive number of Roles, way beyond anything I’d ever seen before. This made me curious as to what other people were dealing with and I’m sure the result will be interesting.
Note: If you work with several Orgs and you have a high tolerance for boredom please submit an answer for each 🙂
Across the Pond with Jason Venable aka TehNrd
Today I start a series of posts that’ll appear here and on the Tquila blog. The series will be in the format of Q&A with some of the finest Salesforce.com and Force.com evangelists, admins and developers. I’m starting with Jason Venable aka TehNrd and I’ll let him introduce himself.
Q: Tell me a bit about yourself. How long have you worked with the CRM vs the Force.com Platform? Were you always a developer?
A: My name is Jason Venable. I am 27 years old. I live in Seattle, Washington, USA. Oh, wait, you want something more interesting, got it. I’ve been working with salesforce.com CRM for a little over 4 years. Three of these years have also been working with force.com. All of this time has been administering and developing for a large enterprise salesforce.com deployment at F5 Networks. A lot of what I do is merging the two worlds of salesforce.com and force.com to meet business needs. This includes using all of the features force.com has to offer including, custom objects, validation rules, Apex code triggers, Visualforce pages, and web services to enhance and improve our companies use of salesforce.com.
Have I always been a developer? Heck no! If you told me I’d be doing coding and web app development 4 years ago I would have laughed at you. College classes that had me coding in notepad and some not so great experiences with the now dead s-controls left a bad taste in my mouth when it came to development. Then salesforce.com released Apex code and I saw how it could solve some of the problems we where facing. I taught myself the basics and the rest is history.
I also have a little blog related to all sorts of force.com goodness at tehnrd.com and some people follow around @TehNrd in Twitterland.
Q: What is your favourite type of development on the platform? What piece of work are you most proud of?
A: Databases design, triggers, and workflow are all cool but what I really like is building snazzy web apps. This has become even funner after jumping on the jQuery and jQueryUI bandwagon (disclaimer: I am a major jQuery fanboy). These JavaScript libraries allow you to make rich web apps with animations, drag & drop, and all sorts of other slick effects with minimal code. Pair this with Visualforce and the force.com database and you’ve got yourself a recipe for some great awesomesauce applications.
I think the coolest thing I’ve ever built on force.com was Gameforce. If anyone from salesforce.com reads this please don’t sue me for stealing your naming convention. Gameforce is a site built with force.com where you can play games. There is a single player black jack card game but what I think is even cooler is multiplayer Connect 4 and what I mean by multiplayer is two people on separate computers anywhere in the world. What I’m really proud of is this site is pure force.com. There is no flash, JavaScript, or any other tricks to handle the multiplayer game. You can check it out here.
Q: Where do you think “The Cloud” is headed?
A: I won’t even pretend to be the first person to say or think this, you talked about it here: http://tquilamockingbird.wordpress.com/2011/03/15/salesforce-com-crm-vs-oracle-ondemand/
But I really believe the younger generation will push adoption of the cloud to the next level. The CTOs and CEOs of today pick “the cloud” because it’s easy to manage, cheaper, and scalable. The CTOs and CEOs of tomorrow will choose cloud solutions for these same reasons but also because they know nothing else. Kids today use “the cloud” every day but don’t even realize it. Webmail, google docs, and mobile me to name a few. How many people under 20 use a local web client to check their personal mail, probably 3. How many people under 20 upload every picture they take to Flickr or Facebook and then don’t worry about the local copy, a lot. When it is time for these kids to choose solutions that solve business problems they will look to “the cloud” without even realizing “the cloud” is something new and useful. To them it will be their norm and the way things have always been.
Q: Which of the Spice Girls do you most closely identify with?
A: Of course the one living in UK has to work in a Spice Girls question. A secret fan you are perhaps? I’m not that scary and I’m not a baby. I don’t have red hair and I haven’t played organized sports in over 9 years. So in some strange way I think I just identified myself as relating the closest to Posh spice. Oh boy, I’m not going to be able to live this one down. I’m definitely not snobby or upscale but the other day someone said I had cool shoes so I guess that makes me stylish and poshy? Posh Dev!
Q: What advice do you have for beginners on the Force.com platform?
A: For beginners the Force.com Workbooks are a great resource. http://wiki.developerforce.com/index.php/Forcedotcomworkbook I am super jealous these didn’t exist when I first started. They are clear, concise, and walk you through the steps of building a full blown application. I also hear pretty good things about the Salesforce Handbook. apparently two guys that know a thing or two about salesforce.com and force.com development wrote it. The forums at developer.force.com are also a great place to hang out. When I first started doing force.com development the forums where the only resource available and the community helped me solve problems that ranged from the “simple face palm I can’t believe it was that easy” problems to the “holy smokes there is no way on earth I would have ever figured this out on my own” problems.
Q: Do you by any chance know of a better way to peel an orange?
A: Funny you ask because I actually do know the most superior method in the entire universe on how to consume an orange… http://www.tehnrd.com/the-best-way-to-eat-an-orange/
Salesforce: Force.com Sites and Google Analytics
This is a cross-post from the Tquila blog.

My Favourite Chart Type
Not having analytics built into your public sites is much like having a Q&A site but not allowing people to answer. In this case some of the questions are:
- Where did you come from?
- How long did you stick around for?
- Where did you hang out on your visit?
Now I’m not going to debate which set of analytics is best but I did come across a few quirks when setting Google Analytics (GA) up for wesnolte.com that I suspect are fairly universal.
Build a Site
This of course is quite a big step and I’m going to assume you’re just about done. To get analytics up and running though you’re going to have to do a few extra bits.

Don't hack me pls.
- Sign up for a GA account, create a Website Profile and you’ll receive an Analytics Code. My code has been blocked out in orange in the image alongside, your code should appear in it’s place.
- Insert the standard Google Analytics Visualforce component into your page.
- Enter the same Analytics Code as above on the Force.com Site Detail page – the field is called “Analytics Tracking Code”.
If you go back to your Analytics home page and refresh you’ll see a little warning sign that tells you something is amiss – and it is but it’s difficult to figure out just what that something is.
S.O.S
The problem in this case is that the default robots.txt file for Force.com Sites blocks all bots. This is not a bad idea but it’s not obvious when setting all this up.
Michaelforce and myself seemed to have had these pains at the same time and he posted his findings here. You’ll need to apply step 3 from his post to allow GA to peek at your site.
Now in my particular case I did all of this and GA was still not able to access my site so I threw in the towel and coded the GA JavaScript straight into my page. A few days later however I realised that the robots.txt was probably caching so I ripped out that funky ol’ JavaScript, plugged the Visualforce component back in and BHAM! It worked. Now I know that exactly 5 people visit my site per day, and that 4 of them of from the UK 😛
Some Advice
My analytics are working a charm but I’ve realised there’s a snag. Since salesforce.com doesn’t allow you access to their nameservers you have to point your root domain to your Force.com Site using URL forwarding at the domain registrar’s side i.e. I can use a CNAME to point www.wesnolte.com to my Force.com site but wesnolte.com has to bounce to my registrars forwarding server before it finally hits the real site. What this means is that – to GA – the traffic directly to wesnolte.com looks like it’s all coming from one source, that is the forwarding server. The only way that I know to work around this is to get people to only use the http://www.domain.com form of your URL – not ideal I know.
Salesforce: Programmatically Populating Sample Data Post-Deployment
I’m not sure if this concept is obvious when reading my previous post, so I thought I’d run through it with a specific example.
Let’s say that you’ve created what can only be described as an exemplary application in the form of a Managed Package. Although your user interface is beyond compare, you’d also like to populate some of your core objects with example data. Some options that immediately spring to mind are:
- Get the person that installs the package to call you, and you can do it post-installation.
- Get the person that installs the package to create sample data manually post-installation.
- Give the user a “Start Here” page with a call-to-action – such as a commandButton – that fetches the data from some API and parses into object data.
Option 3 is pretty excellent, especially now that you can package Remote Site Settings but I think we can do one better. And when I say better I mean simpler and with fewer potential points of failure. Read the rest of this entry »
Salesforce: Instantiating an SObject Dynamically at Run-time
I’m sure a lot of you have this documented somewhere but I’ve recently discovered that it’s quite difficult to find an obvious reference to this knowledge on the interwobbles. So how would you create a Generic SObject at run-time? It’s rather easy thankfully:
String sObjectName = 'MyObject__c'; Schema.SObjectType t = Schema.getGlobalDescribe().get(sObjectName); SObject s = t.newSObject();
If you require this type of functionality quite often I’d suggest putting it in a utility class.
Salesforce: Enhanced Custom Settings
Okay book’s done, now where were we? Oh yes software development, right? Programming software engineering application development h4x0R-ing. Oh how I’ve missed getting my mitts dirty so without further ado…
Some time back Custom Settings were introduced on the Force.com Platform and we all star-jumped in the air, w00ting to anyone who would listen. Up till this point – if you’re anything like me – you were using custom objects to hold configuration data, whether this be lists of language-codes, or operational settings such at outbound web service endpoints, usernames, passwords etc. With Custom Settings you finally had a place to put this information – a home if you will – for your lonely, orphaned Control Data.
Quite quickly however I realised there was still a gaping hole that could be filled with Custom Settings but just didn’t feel right. Lists of data (such as currency codes and descriptions) fit really well into this structure but more serious Control Data that you only need to be listed once-off (such as important URLs, flags to active/deactive modules in your application, usernames and passwords) just don’t seem like they really belong with this other crowd. A quick list of reasons highlights this:
- Control Data is typically entered once off and creating an entire Custom Setting for a single line of data feels like a waste.
- Custom Settings are data so they can’t be deployed with code, they must be created after the fact. Control Data should be a little more important than regular data, it needs a smarter vehicle than plain-old data entry.
- If you’re creating packages you want as much autonomy for your clients as possible. If you use custom settings there will have to be that “Create data in Custom Setting X__c” step in each and every deployment. Read the rest of this entry »
Announcing the Salesforce Handbook
Recently, Jeff Douglas and I saw the potential for a beginner’s book – aimed at business owners, analysts and developers, that comprehensively documents Salesforce and Force.com. There is a tonne of documentation out there and we thought “Wouldn’t it be nice to have a handbook that lightly summarised the most important areas of the platforms as well as offering some best practise advice”. We mulled it over for a time, and today we’d like to announce that we’re currently writing:
The Salesforce Handbook
A newcomer’s guide to building applications on Salesforce.com and the Force.com Platform.
Hand-in-hand with the book we’ll be publishing content from the book on a WordPress site. Here you can expect to find excerpts from the book, but also content that supplements the book e.g. areas that’ll serve as best-practice hubs with links to official documentation, blog posts that rock the party, and even superb discussion forum threads.
We’d love to get your feedback on the book as it progresses, but for now you can checkout the announcement and let us know what you think of the idea.