Wednesday 16 November 2011

VF Woes

Very quick blog this week and it's around an issue I had allocating dynamic Ids to Apex Input fields.

I had a recent requirement that needed me to change the values in some input fields in a targetted way on a VF page. My first thought was to assign a dynamic ID to the <apex:inputfield> only to find that you need concrete values in here.

Found a clever way round this that allows you to find fields by their <apex:inputfield styleclass="{!variable}"

As the JavaScript function getByElementClass is not support in all browsers I needed to write something quick and functional (read dirty) that did the job without using any external libraries or Frameworks i.e. jQuery Here is a very simple JS function that will retrieve all the elements of a certain classname to allow you to 'do stuff' to them.
function getElementByClassName(cl) {
    var elements = [];
    var elem = document.getElementsByTagName('input');
    for (var i = 0; i < elem.length; i++) {
        if (elem[i].className.indexOf(cl)!=-1) elements.push(elem[i]); 
    }
    return elements;
}; 
Not rocket science but you can extend it to use regular expressions etc... and all you need to do it pass it the classname you are looking for. Won't solve a lot of problems but it definitely solved one for me today :)

Thursday 3 November 2011

What did that chap just say?

Business and IT, two bodies working for the same company on the same project separated by a common language. How many times in your working life have you as a business user come out of a meeting or workshop and thought,

"What on Earth were those techy guys going on about? A workflow with a field update and an outbound call to a web service to integrate with the outlying systems via the middleware tier, I have no idea what that means"

You are not alone in this, many's the time I have left a workshop and had a developer ask what the business users meant by:

"We need a solution which will enable our back office staff to get all the information necessary for fulfilment without having to go back to the front office B2B reps to get extra detail."

Neither of these statements are complicated per se but, one of my jobs as a consultant is to make sure that when communications happen between technical resources and business users that they communicate in a common language and my role is interpreter.

Luckily this is not a difficult problem to overcome. All you need to do is invest a little time in the technical team prepping them. Here is a short list of what I like to do to better faciliate the conversation
  1. Get a list of all the issues that the technical team need to discuss up front, find out what the developer needs to know to complete the work. This way, you will be having the conversation.
  2. Coach/Mentor your technical team. Have a pre-meet with the team to make sure there are some simple ground rules laid down:
    1. Under no circumstances should the developer talk about code or offer to show it
    2. Business users do not want to hear about the details, Workflows, Classes, Triggers, Unit tests. None of these should be mentioned
    3. Where appropriate, try and ask the question to your own consultants. It’s their job is to translate between Business and IT
Communications between Business and IT need not be a stressful ordeal. With a little preparation then it can become a productive and stress-free experience.

Monday 17 October 2011

The Lazy Unit Tester

My take on unit tests? Essential!

Never mind that SalesForce.com forces 75% code coverage on any code written. Even if this mechanism wasn't there, I would still insist on them being present on any solution implemented by myself or my team.

A unit test is, at its most basic level, code that tests code! It's not a replacement for smoke, functional or system testing; it's a mechanism which helps improve the quality of code written by developers.

If you’re building a car, it’s the sum of all its parts. The parts are manufactured individually, then put together and at the end of it you have a working car! Quite a simple process, like a jigsaw puzzle, but a lot can go wrong in this process.

Let's take the wheels on the car. For the car to run you need four wheels, each with tyres. They all need to be the correct size, shape and inflated to the correct pressure to be useful. If one of those wheels is square, the wrong size or is flat then this will affect the operation of the car and make it less than desirable to drive or own.

When buying a new car, you hope the manufacturer tests the individual components (in this example the wheels), as well as the overall function of the car.

Now, onto the geekery, let's take this code example

public class Wheel {
    public static String defaultBrandName = 'Default Tyres Inc';
    public static int defaultSize = 14;
    public static double defaultPressure = 33;

    public String brandName;
    public int size;
    public double pressure;
 

    public Wheel() {
        this.brandName = defaultBrandName;
        this.size = defaultSize; 
        this.pressure = defaultPressure;
    }

    public Wheel(String brandName, int size, double pressure) {
       this.brandName = brandName;
       this.size = size;
       this.pressure = pressure; 
    } 

    // Some Accessors and Mutators for the instance variables
}

public class WheelFactory {
    public static Wheel getWheel() {
        return new Wheel();
    }

    public static Wheel getCustomWheel(String brandName, int size, double pressure) {
        return new Wheel(brandName, size, pressure);
    } 

}
It's quite clear how this now works. We go to the wheel factory and either ask for a default tyre or we specify what kind of tyre we want, as the meerkat says 'Simples'. but what would be the best way to test this to enable me to deploy it to a SDFC production org? Not much I hear you say, but here is an example of the type of thing I have seen.
@isTest
public Class WheelFactoryTest {

    public static testMethod void myUnitTest() {
        Wheel w = WheelFactory.getWheel();
        Wheel w1 = WheelFactory.getWheel('Pirelli', 20, 33);
    }
}
And there is it, as far as Force.com is concerned, a perfectly valid unit test! It makes the 75% test coverage requirement and took about 30 seconds to write. Job done! Let me ask you this though, what is my unit 'TEST' actually testing? The simple answer is that it's testing that the WheelFactory and Wheel classes compile and can be called. It's quite frankly a waste of 30 seconds and this test is useless! Is a Wheel being returned? or is it returning a null? What brand is it? What size? What pressure? We just don't know from this test! Below is what I would consider a better test of this:
@isTest
public Class WheelFactoryTest {

    public static testMethod void wheelFactoryGetWheelNoParamsTest() {
        Wheel w = WheelFactory.getWheel();
        System.assert(w != null);
        System.assert(w.getBrandName() == Wheel.defaultBrandName);
        System.assert(w.getSize() == Wheel.defaultSize);
        System.assert(w.getPressure() == Wheel.defaultPressure);
    }

    public static testMethod void wheelFactoryGetWheelWithParamsTest() {
        String brand = "My Brand";
        int size = 17;
        double pressure = 35;
        Wheel w = WheelFactory.getCustomWheel(brand, size, pressure);
        System.assert(w.getBrandName() == brand);
        System.assert(w.getSize() == size);
        System.assert(w.getPressure() == pressure);
        
}
Hopefully you can see the improvements here?
  1. We are testing each method in a clearly separate way so if one method fails we know instantly which one is broken
  2. We are using asserts to guarantee that we are getting the correct results! When you see a unit test with no asserts you need to change that! 95% of the time there is no excuse for a unit test with 0 assertions this is testing more than just coverage!
  3. We are protected from change! Because we are using the static default variables from the WheelFactory class in our tests, if those defaults change our unit test does not need modifying, this is basic coding 101, don't use magic numbers!
  4. I am actually testing that the tyres are the correct size, pressure and brand! Wouldn't you do that in real life if you were buying tyres for your own car?
Hopefully people will find this useful and when they get given a unit test to write will do the right thing and do it properly and understand the importance of a well constructed unit test!

Friday 7 October 2011

Who Needs a new Server

My current project has me again working on my platform of choice Force.com solving business problems for a massive multi-national enterprise company. The problem the client has is that they have developed an excellent suite of 'sweet' sales tools to assist their sales people out in the field. The technologies are many and various and all have one slight weakness from a business perspective:

The Tools that they use all capture client information and details of sales orders and proposals. Again, as a toolkit given to their Sales representatives it's an excellent package, videos demonstrating their products and simple forms to allow them to capture all the information to both identify pain points for the prospective customer and also identify appropriate solutions that they can offer to solve them, this is where the problems begin for the business.

The tools all reside locally on the Sales representatives own laptop and there is currently no way for the business to extract this information and get critical data out to view in a federated way. At first impression the long rendundant IBM consultant within me started thinking of the servers that would be needed to support this, a big expensive middleware infrastructure, an appliance such as DataPower, or software (MQ, and ESB) deployed to a 6 figure cost server that would need to be housed in an even more expensive data centre. This "essential" piece of middleware would then connect to a big central database (DB2, Oracle etc...) and then we can look at deploying Business Objects or Crystal Reports to allow management to get the MIS they require out of the system. Ouch, a big expensive implementation with 5+ consultants and a lead time as much as 6 months before you even start to think about the OpEx of maintaing such a mammoth implementation!!!
Luckily for me and the customer, the year 2011 came knocking and told me that I was living in the past and that those old fashioned architectures are just expensive to deploy in a resilient fashion, unreliable unless you deploy in a resilient fashion and require specialist infrastructure and application teams just to keep the lights on so a more modern, agile and rapid solution was required.

I then lifted the lid on SalesForce after a few hours it became quite obvious that the data they were capturing was essentially Accounts, Contacts, Opportunities and some ancilliary data to support each of those objects! So we had it, a clean mapping between the core SalesForce.com CRM offering and the toolkits developed, now all we needed was a mechanism to get the data from the remote silos into the SalesForce.com org and start reporting on it. The answer was so simple and that was to make use of the excellent WebServices API that you get from the Force.com platform. After an initial workshop, an interface with core services was defined, the applications, which already have the functionality to operate in an off-line capacity were compatible with the delivery mechanism, http/https, which was considered rightly, to be a reliable and resilient mechansim and therefore removed the need for some IBM or BEA/Oracle bloatware (middleware to the layman) and away we all went.

Now to repeat the problem, business critical data was residing in unreachable, remote silos and needed bringing together to provide senior business leaded with mission critical data and reports to allow for forecast and pipeline analysis. How long do you think it would have take? I couldn't even begin to estimate the cost, lapse time for going the 'old' way all the unknowns, server procurement, highly paid product specific consultants, licensing costs, server costs, data centre costs, security considerations, the list goes on and on and on, as does the delivery date and the zeros on the end of the purchase order the customer would need to raise. Luckily for them, the modern Cloud / SalesForce.com route was chosen, and here we are, 12 days of consultant effort later, ready to plug the solution in. The platform is in place, it's resilient and secure, it doesn't cost us to maintain and only one person was needed for all the configuration and coding of the SFDC solution.

If you are reading this, have a think about your own company! How many servers do you have? How many people do you employ exclusively to keep the lights on? How much does one server cost you as a company? How much does it cost doing failover and DR simulations?

Just keep in mind that SalesForce frees you from:

  1. Data Centre Considerations
    1. Rack Space
    2. Power Consumption
    3. Air Conditioning
    4. Networks
    5. Firewalls
    6. Bandwidth
  2. Server Considerations
    1. Security Patches
    2. OS upgrades
  3. Hardware Considerations
    1. Maintenance Contracts
    2. Component failure
    3. Redundant infrastructure (Warm, Hot, Cold Stand-bys)
  4. Skills Considerations
    1. System Administrators
    2. Networks and Firewalls teams
    3. Security specialists
All of this is included in your SaaS licensing model, one user, one license and all of the above comes for free!

With this in mind, when you consider your next implementation, think about the costs, the lead times, the procurement processes, the on-going maintenance cost, the legion of expensive consultants you will need and then think again, why should I do/pay for all of this when the "True Cloud" frees your hands and give you all of this in the price?

Wednesday 28 September 2011

Consultancy and the Word "No"

I was enjoying the company of an ex-colleague, rail travel critic and long time friend Ben Baumguertel this morning on the 06:14 train into London this morning and the conversation roamed onto a topic that provoked strong professional reactions for both of us. Whilst discussing passed projects and clients we stumbled onto the topic of what actually makes a 'good' consultant as opposed to a green-horn consultant on their first client engagement and began telling each other of some parallels we had each encountered in our careers.  

The life of a consultant is often stressful, bouncing from industry to industry, each time being required to learn the political landscape, the stakeholders, the processes and procedures, governance, regulatory requirements, the list goes on and on. In each case however, one core principle seemed to be the same, in all client engagements, all industries, all projects, one simple core principle seems to be present. The ability to say 'No' in a constructive fashion.

We've all been there, you are sitting in a workshop, you've gauged the lay of the land and you are listening intently to both hear about the project as a whole and the particular component(s) you are responsible for when the dreaded line comes from nowhere "What we need to do is X". This instantly puts you in a bind, you need to be able to understand what has been said, think your way through X and apply as may use cases as the workshop has uncovered and also apply the benefit of experience to consider your response. Often you may have the luxury of stating something non-committal such as "let's take that off-line" or "I don't think we know enough to make that decision now" but, more often than not, the response in your head is like something off of lost in space "Danger Will Robinson, Danger".

It's at this point that the subtle skill of consultancy comes to the fore, the art of taking your time, I have learnt the hard way that shooting from the hip is a terrible idea in these situations. The idea has been made for valid reasons by a member of the team you will be working with for the foreseeable future, you simply cannot afford to sit there and simply state "No, that is not a good idea". It creates a perception of yourself as negative and blocking, you run the risk of alienating the team member who made the proposal and you are there to be constructive and helpful. You may be 100% right but the delivery is everything in situations like this.

I'm not going to be prescriptive in this as I don't actually know the answer and anyone that claims so it stretching the truth somewhat but what works for me is to share my relevant thought on the matter. Phases such as
"Will that work for all the use cases we have uncovered today?"
"I think that suggestion has merit but we need to fully explore that before we go too far down the path"
"That works for what we have but what happens when A, B or C occur? Will it handle them?"

Most of the time, my role has not been to dictate an entire solution to the customer but to work collaboratively with Business and IT to produce a solution that benefits all. Don't get me wrong, as the external consultant it's my job to spot things which will cause problems down the line and to provide the benefits of my experience to the project team as a whole and to make sure that the project will be a success but, in order to do this, you must learn the right way to say that simple two letter word that you've known from infancy

"No"

Tuesday 27 September 2011

I Could Murder a Cuppa

During our annual meeting, we were set a relatively simple challenge, we divided into equal teams, and we were all set a simple process to document and present to the group. This sounded quite simple but it was very interesting to see different peoples' takes on the requirement.

Here is the exercise I'll be covering in this Blog. Making a cup of Tea:

  1. Fetch the kettle
  2. Open the lid on the kettle
  3. Turn on the cold tap
  4. Fill up the kettle to the max line
  5. Turn off the cold tap
  6. Put the lid back on the kettle
  7. Put the kettle back on it's base
  8. Plug the kettle in to the electrical socket
  9. Turn the kettle on at the electrical socket
  10. Switch the kettle on
  11. Open the cupboard door
  12. Get a mug
  13. Put the mug on the worktop
  14. Go back to the cupboard and get a tea bag
  15. Put the tea bag in the mug
  16. Wait for the kettle to finish boiling
  17. Pick up the kettle
  18. Pour water into the mug to the desired level
  19. Leave for 3 minutes
  20. Go to the drawer and get a spoon
  21. Stir the tea and remove the teabag
  22. Open the bin
  23. Put the used teabag in the bin
  24. Close the bin
  25. Put the spoon down next to the mug
  26. Go to the fridge and open it
  27. Get the milk out
  28. Open the milk
  29. Add some milk to the mug with tea in it
  30. Close the milk
  31. Put the milk back in the fridge
  32. Close the fridge
  33. Pick up the spoon from next to the mug
  34. Use the spoon to stir the tea
  35. Drink
Now this simple 35 point process all comes from the simple requirement, make a cup of tea!

Now this is quitovee straightforward, any first year university student could have a reasonable stab at this with a high success rate but let's take it a step further. Let's say that you were meeting a customer for a discovery workshop and they passed you a requirement to make a cup of tea. You decompose it down into a series of managable tasks and race to the implementing of it but I've made a common and fatal mistake in the process list above. It's blindling obvious but has caused no end of problems in projects since the dawn of IT. I've potentially made some fatal assumptions: Do we have a kettle? Do we have Teabags? Does the customer want their tea without Sugar? The list goes on and on...

The point of this is that don't be in a rush to implement, think about the requirements,
  • List all your assumptions, get them verified by the customer. 
  • Think about your happy path and get this in place first
  • Think about all your exception flows and hang them off the happy path flow
  • Thnk about how you are going to test it?
    • What are the pre-conditions?
    • What are the post-conditions?
This is just as valid in Agile as it ever was with Waterfall approaches, admittedly in Agile you may proceed without knowing the full requirement but you should still attempt to flag the unknowns to be bottomed out later. Making sure that your requirements are fully thought out is the only sure fire way I've seen to prevent the 'ball of mud' coding pattern from becoming the predominent pattern on an implementation. An hour spent upfront clearing up your requirement and thinking your way through a problem will invariably prevent a throwing code at problems to make them go away.

This may be a very simplistic way of demostrating my point but it's a simple thing that if done incorrectly can have serious consequences? We could potentially have gotten to step 27 before we realised that we hadn't actually got any milk!



Finally, I suppose I should give the answer to my previous blog, this is quite simple once you start to dry run the principles in your head and you have ever got a divide by zero exception :). The answer is simply that the equations don't work as if X == Y then X-Y will always equal 0... Therefore, to divide by X - Y is to divide by 0 which cannot be done

Thursday 22 September 2011

Spotting a True Developer

I was thumbing through some old Maths homework set for me in my university days and I came across a practical joke that a lecturer set us that I think would be a marvellous addition to any technical interview when assessing would be developers.

The principle stems from a Mathematical fallacy which can be proven to the naked eye but, when you start to dig into it a developer should easily spot the anomoly as it can only be disproved by 'dry running' the concept in your head. Remember dry running you old school guys? A skill that has sadly seemed to have diminished as debuggers have gotten better and better. The good news is with SalesForce.com and it's Apex programming language the debugger is pretty basic and I personally am starting to see a slow resurgence of this lost, essential art!

On to the meat of this:

If we start with a basic principle X = Y it is safe to assume we can expand this as long as whatever we do to one side of the equation we do to the other side to keep it balanced so here we go.

1. Multiply both sides by X

    X^2 =X^2

2. Subtract Y^2

X^2- Y^2 = XY - Y^2

3. Now let's factor both sides and break it down into an easier to read format

(X - Y) (X + Y) = Y (X - Y)

4. Now we can divide both sides by X - Y to clean it up a bit

X + Y = Y

5. Now if we revisit our original principle that X = Y we can now express this as

Y + Y = Y or 2Y = Y

6. Now we simply divide by Y

2 = 1

And there you have it, we have now proven that 2 = 1 but can you spot the problem with this. I maintain my assertion that a true developer capable of dry running this will spot it in a few moments.


This is a common Maths 'joke' so have a go at it first before googling for the answer and I'll release the answer in my next blog but you shouldn't really need it as long as you maintain the basic principle X = Y

Until next time,

Chris