Is there a way to validate a Salesforce ID, maybe using RegEx? They are normally 15 chars or 18 chars but do they follow a pattern that we can use to check that it's a valid id.
-
do you know at least what characters are to be considered valid>? – Bogdan Emil Mariesan Mar 16 '12 at 18:56
-
@BogdanEmilMariesan I don't understand your question. The chars are different for different object records, which is why it's not a straightforward regex I think. – Anup Mar 16 '12 at 19:24
8 Answers
There are two levels of validating salesforce id:
- check format using regular expression
[a-zA-Z0-9]{15}|[a-zA-Z0-9]{18}
- for 18-characted ids you can check the the 3-character checksum:
Code samples provided in comments:
![](../../users/profiles/836941.webp)
- 8,415
- 3
- 32
- 57
-
2
-
2I created a gist based on the info above and it check(sums) out. I'll see myself out. https://gist.github.com/jeriley/36b29f7c46527af4532aaf092c90dd56 – jeriley Feb 28 '17 at 16:58
-
2And here's a Go version: https://gist.github.com/maxmcd/a32a35c0eebcfb77cd005f9dd8958815 – maxm Aug 25 '19 at 19:11
-
1Thanks - I edited the response and linked your gits. @alf your link seems not working (404). – zacheusz Sep 09 '19 at 20:45
-
Oops, here's the updated link to the Ruby version: https://gist.github.com/alfius/3bd51d4bc0796185a9d1f7a7841507d0 – alf Sep 20 '19 at 23:07
Something like this should work:
[a-zA-Z0-9]{15,18}
It was suggested that this may be more correct because it prevents Ids with lengths of 16 and 17 characters to be rejected, also we try to match against 18 char length first with 15 length as a fallback:
[a-zA-Z0-9]{18}|[a-zA-Z0-9]{15}
![](../../users/profiles/913223.webp)
- 10,545
- 3
- 61
- 49
![](../../users/profiles/2517.webp)
- 10,718
- 3
- 32
- 32
-
-
2
-
4In most regex implementations, `\w` would include `_`, so the above is not correct. – haridsv Jan 30 '14 at 05:35
-
2This will only work with 15 characters long IDs. The 3 character suffix is computed from the first 15. If you are not sure, that 18 character long ID have the right suffix, you better chop them off. It is the same, but "15" IDs are not sortable in case insensitive way. Basicaly, you can pass as ID anything that matches `[a-zA-Z0-9]{15}` or `[a-zA-Z0-9]{18}` with right suffix. Everything else will fail. – Michal Ingeli Feb 03 '15 at 10:08
-
1Unfortunately it appears this won't work in all cases. It appears that salesforce does additional validation beyond just the format (15 or 18 digit, alphanumeric), and I've run into cases where I get a string exception when trying to assign a id that matches this regex to an id variable ... – Ralph Callaway Feb 19 '15 at 23:05
-
2Won't you need to anchor the Regex pattern to prevent strings greater than length 18 from passing? Like so: `^([a-zA-Z0-9]{15}|[a-zA-Z0-9]{18})$` – dthrasher Nov 04 '15 at 15:58
-
1Please see zacheusz's detailed answer: http://stackoverflow.com/a/29299786/1135138 – aaronbauman Nov 24 '15 at 14:59
Just use instanceOf to check if the string is an instance of Id.
String s = '1234';
if (s instanceOf Id) System.debug('valid id');
else System.debug('invalid id');
![](../../users/profiles/7268877.webp)
- 161
- 1
- 2
-
1Wow, seems so obvious in retrospect, but it never occurred to me to try `instanceof`. SO much better than a try-catch block. – Jason Clark Feb 12 '21 at 21:14
-
1
The easiest way I've come across, is to create a new ID variable and assign a String to it.
ID MyTestID = null;
try {
MyTestID = MyTestString; }
catch(Exception ex) { }
If MyTestID is null after trying to assign it, the ID was invalid.
![](../../users/profiles/549141.webp)
- 6,747
- 5
- 36
- 60
-
Yeah, I've seen this solution in the salesforce developer forums, but was wondering if there was a better way to validate a salesforce id. Thanks though! – Anup Mar 16 '12 at 19:23
-
You're welcome; Jeremy Ross's solution should work well if you were to try validating it with a Regular Expression. – Matt K Mar 16 '12 at 19:52
-
4Is this good practice? I try to avoid try-catch as much as possible (doesn't mean I don't use it well) I always thought try-catches are resource heavy. – Vid L Apr 03 '13 at 12:59
-
4The reason this is bad practice is that you should NEVER flippantly use try/catch. The reason is that Try/Catch is a very complex operation, causing your code to run for many more cycles on the processor. Of course the time complexity varies based on the language and compiler, however I think we can all safely assume that Apex's version is quite probably horrible. – Ben K. Sep 30 '14 at 18:25
-
True, it's costly, but it's also the best way that I could find that works in this case... – Matt K Sep 30 '14 at 18:42
-
1I agree with the two comments questioning if this is good practice, though I think it is totally fine if you are just going to do it on a single user input for example. It might be slow, but because there is only one run, it will not be a something you would notice. – lindon fox Jan 14 '15 at 07:37
-
@LVS not sure there is a better way, i'm running into this with queries from string input, and the only option is to try catch the query, or try catch the string to an id to validate it, the regex still lets invalid ids through since salesforce appears to do some additional validation above 15 or 18 digit alphanumeric – Ralph Callaway Feb 12 '15 at 01:46
-
1@Ralph I just thought of another idea which could be worse - try to delete the record and find if its valid. So yes, this would be the most effective way – Vid L Feb 18 '15 at 20:50
This regex has given me the optimal results so far.
\b[a-z0-9]\w{4}0\w{12}|[a-z0-9]\w{4}0\w{9}\b
![](../../users/profiles/2370483.webp)
- 28,730
- 25
- 78
- 91
![](../../users/profiles/5951790.webp)
- 46
- 3
Javascript: /^(?=.*?\d)(?=.*?[a-z])[a-z\d]{18}$/i
These were the Salesforce Id validation requirements for me.
- 18 characters only
- At least one digit
- At least one alphabet
- Case insensitive
Test cases
Should fail
1
a
1234
abgcde
1234aDcde
12345678901234567*
123456789012345678
abcDefghijabcdefgh
Should pass
1234567890abcDeFgh
1234abcd1234abcd12
abcd1234abcd1234ab
1abcDefhijabcdefgf
abcDefghijabcdefg1
12345678901234567a
a12345678901234567
For understanding the regex, please refer this thread
![](../../users/profiles/7351463.webp)
- 61
- 1
- 7
The regex provided by Daniel Sokolowski works perfectly to verify if the id is in the correct format.
If you want to verify if an id corresponds to an actual record in the database, you'll need to first find the object type from the first three characters (commonly known as prefix) and then query the object type:
boolean isValidAndExists(String key) {
Map<String, Schema.SObjectType> objTypes = Schema.getGlobalDescribe();
for (Schema.SObjectType objType : objTypes.values()) {
Schema.DescribeSObjectResult objDesc = objType.getDescribe();
if (objDesc.getKeyPrefix() == key.substring(0,3)) {
String objName = objDesc.getName();
String query = 'SELECT Id FROM ' + objName + ' WHERE Id = \'' + key + '\'';
SObject[] objs = Database.query(query);
return !objs.isEmpty();
}
}
return false;
}
Be aware that Schema.getGlobalDescribe can be an expensive operation and degrade the performance of your application if you use that often. If you need to check that often, I recommend creating a Custom Setting or Custom Metadata to store the relation between prefixes and object types.
![](../../users/profiles/3230188.webp)
- 136
- 1
- 5
You can also check for 15 chars, and then add an extra 3 chars optional, with an expression similar to:
^[a-z0-9]{15}(?:[a-z0-9]{3})?$
on i
mode, or not:
^[A-Za-z0-9]{15}(?:[A-Za-z0-9]{3})?$
Demo
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
RegEx Circuit
jex.im visualizes regular expressions:
![](../../users/profiles/6553328.webp)
- 1
- 9
- 28
- 53