5

Rather disappointingly for Blobs this assertion fails:

System.assert(Blob.valueOf('abc') == Blob.valueOf('abc'));

though the Apex Expression Operators documentation over ambitiously says that:

Unlike Java, == in Apex compares object value equality, not reference equality, except for user-defined types.

So it seems that to test equality where the blobs have the same size requires the blobs to be put through EncodingUtil.base64Encode which may lead to heap limit problems e.g.:

private static Boolean eq(Blob b1, Blob b2) {
    if (b1.size() == b2.size()) {
        String s1 = EncodingUtil.base64Encode(b1);
        String s2 = EncodingUtil.base64Encode(b2);
        return s1 == s2;
    } else {
        return false;
    }
}

I there a better way to go?

2
  • I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
    – sfdcfox
    Commented Oct 18, 2018 at 12:38
  • @sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
    – Keith C
    Commented Oct 18, 2018 at 12:59

1 Answer 1

5

The equals operator == does work as expected with Blob, so there's no reason to jump through extra hoops, such as your eq method. However, one possibility is that you may have had a different case in your code; since Blobs are binary data, they are case-sensitive in respect to equality:

Blob a = blob.valueof('hello world'),
    b = blob.valueof('Hello world');
system.assertequals(a, b); // Assertion fails

This is in contrast to normal String comparison, which is case-insensitive.

1
  • The problem was API version; the class with the comparison was set to version 29.0 and changing it to 43.0 fixed it. Thought I already had made that change but when double checking I had not.
    – Keith C
    Commented Oct 18, 2018 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .