Posts in "AnotherBoringUtilClass" category

Convert a Blob to a Hex String in Salesforce

Since setting up my new business Wickedbit I have been spending a lot of time looking at integrating Salesforce with various third party applications. I love integration work which is just as well as one is never like another, despite there being standards for things like authentication and data payloads people still seem to relish the chance to “roll their own”. I shouldn’t complain I guess it keeps me in a job!

One such “example of innovation” that I came across required me to pass a blob via HTTP and to achieve this rather than simply Base64 encoding the Blob I was required to send the Blob as a string of hexadecimal values. An interesting idea for sure and one that after a quick bit of searching I realised wasn’t straight forward to solve.

My solution to the problem has ended up being a little convoluted and I’m sure there are other ways around this but for prosperities sake here is my approach. I decided to use the platform EncodingUtil class to get the Blob into a base64 representation; then using some simple bit shifting and masking I would reverse the base64 encoding and therefore get the individual bytes that make up the blob. From there it’s just a case of doing a decimal to hex conversion. It sounds easy enough and here’s the code; it’s compressed into as few statements as possible to try and keep below governor limits.

This could all be made a lot simpler if there were two little additions to the platform, other than the inclusion of a byte primitive. Firstly access to the bytes that make up the blob. Either let me initialise a list of bytes using the blob or provide a getByteAt(n) method. Secondly, a simple decimal to hex conversion method would clean this up no end and be useful in other circumstances too.

Whilst you might not have an odd integration requirement like I did to fulfil I hope that the idea above will be of some use; if you remove the lookup into the hex map you have a routine that can give you the list of bytes that make up a blob – that’s got to be useful to someone, no? I hope so.

AnotherBoringUtilClass: Extract a Set from a List

Yet another update to the AnotherBoringUtilClass.

This time it’s to extract a Set of fields from a list of objects.  I find myself writing the same loop over and over to do this work – often in a trigger or another bulkification scenario.  The idea is that this method will take a list of objects and return you a set of the values associated with one particular field in that object.  So for example; here’s a list of contacts, return me a set of all of the AccountIds they’re related to. This set can then be used to query the accounts in one nice quick SOQL statement; yes I know I’m teaching you all things you already know – send me your Grandmother’s address if you’re offended.

public static Set<Object> extractFieldValues(List<sObject> input, String fieldName){

    Set<Object> output = new Set<Object>();

    for(sObject obj : input){

    return output;


And you could use it in a trigger like so:

trigger DoSomethingSilly on Contact (after insert){

    Set<Id> accountIds = AnotherBoringUtilClass.<wbr>extractFieldValues(Trigger.<wbr>new, 'AccountId');</wbr></wbr>
    List<Account> accs = [SELECT Id, NumberOfEmployees FROM Account WHERE Id IN :accountIds];

    for(Account acc : accs){
        acc.NumberOfEmployees += 1;

    update accs;


AnotherBoringUtilClass: Day Of The Week

I recently needed to get the day of the week from a Date in SalesForce and I was amazed to find that there’s no weekday() method on the date object; month: yes, day: yes, weekday: no.

Anyway I came up with the following addition to my AnotherBoringUtilClass:

private static integer getWeekday(Date input){
   return input.toStartOfWeek().daysBetween( input );

This will return a zero based day for the date you pass in where zero is the day that you have conifgured as the start of the week.

I have seen this done elsewhere in a slightly different way but I thought I’d share this one liner anyway.

I really should look at those release notes more carefully

I can’t believe that I missed the introduction of the URL class in SalesForce.  Oh joy, at last.

This obviously leads to this code:

public class AnotherBoringUtilClass{

  public static string getUrl( Id Id ){

    return URL.getSalesforceBaseUrl().toExternalForm() + '/' + Id;



Now if only we had extension methods like C# then this would be really cool.