Salesforce & LinkedIn Developer API: 401 – Unauthorized

I’m not going to get into the boilerplate code you’ll need in order to get OAuth up and running as it’s pretty straight forward, but I did run into a peculiar issue which took me some time to narrow down. To reproduce these steps you’ll need to: Correctly setup OAuth between the Force.com Platform and LinkedIn Authorise access to your LinkedIn account Attempt to fetch a resource using an API endpoint such as: http://api.linkedin.com/v1/people/~/network/updates?scope=self When attempting to make the call out in step 3 you’ll get a “401 – Unauthorized” error. So to be clear OAuth is working perfectly but a call like this one will still result in the error. After some troubleshooting I noticed that some API endpoints didn’t result in the error e.g. http://api.linkedin.com/v1/people/id=abcdefg/network/updates?scope=self. At this point I realised the issue was probably the tilde (‘~’) character in the URI. Grasping at straws I manually replaced the tilde with …

Read more

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.

Rule one of packaging – Finish your bolognese first!

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:

  1. Get the person that installs the package to call you, and you can do it post-installation.
  2. Get the person that installs the package to create sample data manually post-installation.
  3. 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 more

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: 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…

Now this one goes right about... here!

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 more

Salesforce: System.Assert vs System.AssertEquals

For a time I’d wondered what the difference between the System.Assert and System.AssertEquals methods might be (System.AssertNotEquals to a lesser degree). To be honest this tip isn’t going to help much with World Peace or Curing Internetlessness, but it’s certainly gonna save you some time. Looking at the documentation there are some obvious differences. Pretty standard stuff, and can all be learnt simply by Reading The Manual. Now testing best practices state that you should assert some condition but also output a “console” message that shows the values of each of the operands. This certainly helps you debug unit tests when asserts are failing, but man I hate typing extra code just to check out some variable values! So the time saving trick that isn’t obvious is that when omitting the optional third parameter for System.AssertEquals and System.AssertNotEquals the compiler automagically outputs the values of the operands to the console …

Read more

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 …

Read more

Salesforce: Using basic email templates from Apex code

A few weeks ago I noticed a number of questions in the forums around how to use email templates from Apex classes. I Googled a few keywords and come up with very little. I then trawled the documentation but came up empty-handed. Eventually it was Eclipse that provided the knowledge required, and I thought I’d share it with the good ol’ developer community.

One part of the process is discovering that Salesforce stores all sorts of items as records in objects; some of them being email templates, user information and even Apex class bodies (scandalous). All you have to do is query them. The other major part is finding that the method you need is missing from the Apex documentation.

Read more

Salesforce API Integration Using SOAP-based Web Services


Too Much Documentation (TMD) - The leading cause of baldness amongst men.

There are several tomes & tools to help you integrate with the platform; this article will concentrate on WSDL defined web services. Some help texts are specific to certain languages, others favour certain approaches but I’ve found there’s no short-and-sweet guide. As someone who’s nearly drowned in the documentation (including forums, tweets and blogs) I thought I’d try to save – at least some of you – the white-squall that is ‘Learning the basics of SFDC WSDL-based integration’.

I’ll be the first to admit that summarising such a broad topic can be difficult, so if I do miss anything out, y’all out there in the community just let me know.

Read more

Salesforce Unit Tests & Code Coverage

Unit testing *sigh*. Oh how they vex me. If they weren’t so important (and required) I’d just skip the lot, but they are and so we – champions of software development – must press on in the face of dreary complexity; we will not back down, we will not surrender, we will look that CRT/LCD screen in the pixels and say, “Untested units, you will not defeat me!”.

Diving into the thick of things, the most common question seems to be, “Why can’t I get code coverage for my entire class?!”. The trick here is to think like a runtime engine, and consider how you might journey through all possible testing paths. Now I never said it’s easy, but with a bit of practice (and 8 truck loads of patience) you’ll get there. Let’s look at some common cases.

Read more

Salesforce Unit Tests & Code Coverage

Unit testing *sigh*. Oh how they vex me. If they weren’t so important (and required) I’d just skip the lot, but they are and so we – champions of software development – must press on in the face of dreary complexity; we will not back down, we will not surrender, we will look that CRT/LCD screen in the pixels and say, “Untested units, you will not defeat me!”.

Diving into the thick of things, the most common question seems to be, “Why can’t I get code coverage for my entire class?!”. The trick here is to think like a runtime engine, and consider how you might journey through all possible testing paths. Now I never said it’s easy, but with a bit of practice (and 8 truck loads of patience) you’ll get there. Let’s look at some common cases.

Read more