How to check Blob objects for equality?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
4
down vote
favorite
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?
apex blob
add a comment |Â
up vote
4
down vote
favorite
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?
apex blob
I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
â sfdcfox
2 hours ago
@sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
â Keith C
2 hours ago
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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?
apex blob
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?
apex blob
apex blob
edited 2 hours ago
asked 2 hours ago
Keith C
91.7k1086191
91.7k1086191
I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
â sfdcfox
2 hours ago
@sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
â Keith C
2 hours ago
add a comment |Â
I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
â sfdcfox
2 hours ago
@sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
â Keith C
2 hours ago
I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
â sfdcfox
2 hours ago
I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
â sfdcfox
2 hours ago
@sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
â Keith C
2 hours ago
@sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
â Keith C
2 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
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.
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
2 hours ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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.
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
2 hours ago
add a comment |Â
up vote
3
down vote
accepted
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.
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
2 hours ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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.
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.
answered 2 hours ago
sfdcfox
233k10179395
233k10179395
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
2 hours ago
add a comment |Â
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
2 hours ago
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
2 hours ago
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
2 hours ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f236490%2fhow-to-check-blob-objects-for-equality%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
â sfdcfox
2 hours ago
@sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
â Keith C
2 hours ago