Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Is there a way(regex or something) to find out if a given string is a Salesforce Id or not? I'm looking for some helper class in Java.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Regex is okay: [A-Za-z0-9]{15}

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class IdUtil {
  private static final String INPUT = "00a000000000012";
  public static void main(String[] args) {
    Pattern id = Pattern.compile('[A-Za-z0-9]{15}');
    Matcher matcher = id.matcher(INPUT);
    if (!matcher.find()) System.out.println('Not an Id!');
  }
}

If you use [A-Za-z0-9]{18} to find 18-character Ids (case-insensitive), best to run the checksum as well. You could port this implementation of the algorithm to Java.

share|improve this answer
add comment

Apex Id data type does validate what is set into it, In apex, you could assign a string to an Id type variable. If you put that in a try block and catch StringException the catch block will indicate failure.

String anIdString = 'a05E0000002Ug8U'; //also works with a05E0000002Ug8UIAS
String aString = 'whatever';

Id anId = anIdString;

System.debug(anId);

try {
    anId = aString;
} catch (StringException e) {
    System.debug('tried to assign invalid string to Id and failed');
}
System.debug(anId);

EDIT: If you wanted to access that logic from outside of SFDC, you could write the logic into a custom webservice method. Even though this is a very clean way to do Id datatype checking, it has the downside of consuming a webservice call for very little gain. But it could be scaled perhaps to do the same with an array of strings:

global with sharing class DemoWebService {
webService static Boolean isStringId(String idTestStr) {
    Boolean isId = true;
    Id anId;
    try {
        anId = idTestStr;
    } catch (StringException e) {
        isId = false;
    }
    return isId;
}
}
share|improve this answer
1  
Shoot...didn't read the Java bit. –  Peter Feb 5 '13 at 11:10
1  
You could still do sth like that in Java (pass something to Id field, send a DML, watch fireworks). It should fail on XML composition phase before reaching SF; if I recall correctly XSD has validation of 15/18 chars, alphanumeric only. Not too sophisticated but it's something, not sure how many cycles OP wishes to spend on essentially checking it twice... –  eyescream Feb 5 '13 at 11:17
1  
It would be the ultimate in API-call-wasting to take a trip to the server just to validate an ID is an ID. But you could take the code above and wrap it in a webservice method...I'll take a shot. –  Peter Feb 5 '13 at 11:30
1  
haha, at least this will work if salesforce ever decides to change the ID structure. –  Greg Grinberg Feb 5 '13 at 13:58
1  
Not likely. :-) –  Peter Feb 6 '13 at 12:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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