I have an object that has a string field that I store object id's in. When my apex code runs it looks at the object id in the string field and queries the apprpriate object unfortunately the where clause is not performing a case sensitive search so and id like '000000000000abc' matches id's like '000000000000abC', '000000000000aBc', '000000000000ABC' etc. and I need it to perform and exact match in the where clause. How do I do this?

Note the parentid__c in my custom object is a 18 character text field

CustomObject[] co = [select parentid__c from CustomObject__c where id='xxxx'];
string s = co.parentid__c;
Case[] c = [select id, name from case where id=:s];

returns more than the one record I'm looking for


If you have two 18 digit Ids which are properly formed, they would never be equal regardless of their casing. That's because the last three digits encode the casing of the first fifteen. So when filtering on the 18 digit Id, those last three digits actually do serve the purpose of making your query "case-sensitive". For more information on case sensitivity and how queries are constructed, take a look at Converting uppercased 18-digit Id to valid Id. If you have these two account Ids, only one is valid:

Id value1 = Id.valueOf('00129000007Kbn7AAC'); // valid
Id value2 = Id.valueOf('00129000007KBN7AAC'); // invalid
//                                   ^ this character is improperly cased
  • Hmmm. I was pulling the id out of the URL in some circumstances and that's why I was writing 15 character id's to the string. Simply setting an id variable to the 15 character value adds the 3 character checksum to the end. This sounds like a solution to my problem. I will recode some and let you know. Thanks Adrian – Ohmicron May 23 '17 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.