The Silver Lining

Lessons & Learnings from a salesforce certified technical architect.

Salesforce: Dynamically determining the field type of a dynamically determined sObject

with 2 comments

This solution is quite difficult to find.

Call me crazy but I need to do this from time to time, and every time I do I can’t remember how I did it before! So I then trudge through the API and the Apex docs until I find the answer and that’s no mean feat in this specific case. Well, no more my friends because I’m putting it right here on this very blog!

In short the code below will return (as a String) the type of field that we’re working with. Neither the name of the object or the name of the field need to be known in advance.

    public static String getFieldType(String fieldName){
    	// Assume that "sObjectName" is populated elsewhere
	Schema.SObjectType t = Schema.getGlobalDescribe().get(sObjectName);

	Schema.DescribeSObjectResult r = t.getDescribe();
	Schema.DescribeFieldResult f = r.fields.getMap().get(fieldName).getDescribe();

	if (f.getType() == Schema.DisplayType.String){
		return 'String';
	} // .... else if

	return null;
    }

Written by Wes

February 1, 2012 at 9:33 pm

jQuery Org Chart – a plugin for visualising data in a tree-like structure

with 187 comments

jQuery OrgChart is a plugin that allows you to render structures with nested elements in a easy-to-read tree structure. To build the tree all you need is to make a single line call to the plugin and supply the HTML element Id for a nested unordered list element that is representative of the data you’d like to display. Features include:

  • Very easy to use given a nested unordered list element.
  • Drag-and-drop reorganisation of elements.
  • Showing/hiding a particular branch of the tree by clicking on the respective node.
  • Nodes can contain any amount of HTML except <li> and <ul>.
  • Easy to style.

jQuery OrgChart


Expected Markup & Example Usage

All documentation can be found on github.


Demo

You can view a demo of this here.


Sourcecode

Source code with an example is available here.

Written by Wes

December 1, 2011 at 9:34 pm

Salesforce: Different percentage unit test code coverage in different environments

with 3 comments

Spot the difference.

Many people are finding that their tests are reporting different degrees of test-coverage in different environments. There are a few things to check if you are getting inconsistent results but there’s a new bug in the wild. Before you assume you have the bug make sure that you’ve:

  1. ‘Run All Tests’ in each environment. This will tell you a few things viz.
    • Perhaps there are tests failing that are bringing coverage in that environment down.
    • There are some tests that only fail when run in the browser e.g. MIXED_DML_EXCEPTION will not rear it’s head through the IDE.
  2. Click the ‘Compile all classes’ link above the Setup > Develop > Apex Classes view. I’m not sure when this lil’ bugger first appeared but it’s darn useful. Essentially it makes sure that all the dependencies in your code are fulfilled e.g. if page A uses controller B that in turn refers to utility class C it’ll make sure each of those pieces exist and work (as far as compilation goes at least).
  3. Double-check your test classes to make sure they’re not data dependent. If they are and you have different types/amounts of data in your respective environments it’s 100% feasible that the test coverage will be different.

Now if you’ve checked all of the above you might have been afflicted by a new bug which, for some reason, counts braces, whitespace (and more!) as being uncovered by your tests. This is preposterous of course and to fix it simply remove all test history from the deranged environment. Re-running the test and/or deploying them should now be back to normal!

Written by Wes

November 30, 2011 at 11:01 pm

Salesforce: Stop email being sent on user creation or password reset or …

leave a comment »

I’ve had to do this a few times but infrequently enough for me to forget how to do it each time. Forgetting things isn’t usually an issue because of our Google Overlords and their mighty The Google but it’s quite a journey down the rabbit hole to find this specific information.

The reasons it’s tricky to find is because the setting that controls whether an email is sent to the user on creation is not directly associated with users but with DML. Long story short you need to set a particular Database.DMLOption e.g.


User u = new User();
// Add some details here ...

// Set the DML options
Database.DMLOptions dlo = new Database.DMLOptions();
dlo.EmailHeader.triggerUserEmail = false;

Database.insert(u,dlo);

Hopefully this information will now be easier to find next time I forget 🙂

Written by Wes

October 30, 2011 at 1:12 pm

Posted in Apex, SalesForce

Tagged with , , , , ,

A gem to help you document your Rails ActiveRecord model

with 2 comments

I’ve been using the “annotate” gem for a while and it’s simple whilst incredibly useful. Essentially once run it documents each resource in your database within the appropriate file. Here’s how to get it working.

Step 1

Install the gem. There are a few ways to do this but I usually include the following line in my Gemfile:

gem 'annotate'

Then run “bundle install” from the command line.

Step 2

Generate the schema annotations by running:


annotate

in the root of your rails project directory and it will inject the schema details for each “table” into the respective “model.rb” file e.g. “user.rb” might contain:

# == Schema Information
#
# Table name: users
#
#  id          :integer         not null, primary key
#  screen_name :string(255)
#  name        :string(255)
#  created_at  :datetime
#  updated_at  :datetime
#

Written by Wes

October 21, 2011 at 5:50 pm

Knowledge Tree for Salesforce User Roles

with 3 comments

UX that makes you say things like, "Gee Whizz" and "Cowabunga"

Many moons ago @ratherGeeky was searching for an AppExchange app that would neatly display user roles so that she could be the best admin she could be. I had a poke around and there were a few nice apps out there but nothing with a simple, neat display of user roles.

Since then CloudSpokes was created, and a competition to that effect was released using the concept and tools that I was going to use (@jeffdonthemic smells like turnips btw). The outcome of the competition was a few very cool apps, but nothing that was package-ready so I pressed ahead anyway.

I’m happy to announce that you can now get Knowledge Tree for Roles on the AppExchange for free. It’s definitely a point solution, very specific in the problem it’s trying to solve but I think it does that well. I have a roadmap for the product and will [ever so slowly] be releasing new features e.g. drag and drop will be killer.

If you install the app and like it then please, please, please (please (please)) review it as it helps exposure and will hopefully lead to me getting more time to work on it. If you don’t like it let me know why via email (wes@cloud-corporation.com) and I’ll do my best to turn your frown upside down.

Written by Wes

October 14, 2011 at 11:23 am

Across the Pond with Shannon Hale

leave a comment »

Shannon is Senior Product Manager for Declarative Apps at Salesforce.com

@abhinavguptas and I were curious as to the identity of the creator of the SetupScripter, which is now incorporated into the salesforce.com Org setup menu. I dug around a bit and managed to uncover her real identity – community please meet Shannon Hale, Shannon Hale this is the community. She didn’t just stop with that wonderful piece of UX but has moved onto bigger and better things, but I’ll let her tell you about those.

If you’d like to learn more about the genius that is Shannon or just have a chat with her you can get her on twitter at @shannonsans or @bathtubdreamer. You can also check out her online presence at shannonsansserif.com and bathtubdreamer.com.

Onto the Q&A!

Who is Shannon Hale? How did you get into software development and UX design?

I started out as a writer, but in a different field — I wrote and edited for some independent Canadian music and culture magazines. I started technical writing to help pay the bills, and from there wandered through a series of tech positions: technical training, systems analysis and design, and software development. In 2001 I became obsessed with why a product I was coding was difficult to use, and began to independently study interaction design and user experience.

When I’m not being a complete geek — which I am even at home, I always have personal and volunteer web projects going on — I’m sewing, knitting, or binding books. I’m one of those people who always needs to be doing something with their hands.

Read the rest of this entry »

Written by Wes

October 14, 2011 at 10:49 am

“Dropping” a mongohq database on Heroku

leave a comment »

I’ve used inverted commas around the word ‘dropping’ because it doesn’t look like you can drop a mongohq database – what you can do however is drop all the collections in the database. I was doing this through the add-on interface in heroku but that got tired quickly, and after some googling it looked like the solution was to write a custom rake task and drop it into lib/tasks/; the file can be named anything with a ‘.rb’ extension. The code follows:

namespace :mdb do
    desc 'Drops all the collections for the database for the current Rails.env'
    task :drop => :environment do
      if ENV['MONGOHQ_URL']
        uri = URI.parse(ENV['MONGOHQ_URL'])
        conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
        DB = conn.db(uri.path.gsub(/^\//, ''))
        DB.collections.each do |collection|
          begin
            collection.drop
          rescue
            puts "Can't drop: " + collection.name
          end
        end

      end
    end
end

Note that the exception handler isn’t perfect and essentially all I’m doing is skipping the system collections that can’t be dropped.
Once you’ve deployed this code to the heroku servers it’s simple to execute:

heroku rake mdb:drop

Written by Wes

October 2, 2011 at 5:05 pm

Posted in Ruby

Tagged with , , , , ,

London Force.com Meetup – 20 October 2011

leave a comment »

Yip it’s that time again and boy do we have some very cool speakers this time around. The gig is at Skillsmatter at 6.30pm on 20 October and is summarised in the info below. Please don’t forget to RSVP. Of course there will the be usual beers and pizza 😉

Hope to see you there!

AMJAD KHAN ON FORCE.COM ERD USING SCHEMASPY

Amjad Khan will give a talk to the SafesForce user group on How to Install and run SchemaSpy on any Salesforce Org to generate an ER diagram. More details…

SIMON GOODYEAR ON MAKING BETTER USE OF INTERFACES ON THE FORCE.COM PLATFORM

Simon Goodyear’s talk for the SalesForce user group will give a quick overview on what interfaces are, how you can make better use of them in APEX, and what we gain from doing so. More details…

TESTING ON THE FORCE.COM PLATFORM

Keir Bowden gives a talk for the SalesForce User Group on Testing on the force.com platform, covering TDD, unit testing, continuous integration and test scripting. More details…

EFFECTIVE B2C MANAGEMENT

Stony Grunow gives a talk for the SalesForce User Group on Effective B2C management for companies using the “Contacts and Organisations” package. More details…

TURNING YOUR ORG INTO A FULLY OPERATIONAL BATTLE STATION USING RUBY AND SELENIUM

Bruce Durling will give a talk for the SalesForce User Group on turning your org into a fully operational battle Station using ruby and selenium More details…

Written by Wes

September 28, 2011 at 5:03 pm

%d bloggers like this: