+ Start a Discussion
pcave_jrpcave_jr 

Regular expression to validate Salesforce IDs

Does anyone have a RegEx to check for valid Salesforce Ids? I'm working on an integration project and need to validate values going into a reference field.

 

Thanks,

Phillip

prageethprageeth

Hello pcave.jar;

Hello pcave_jr;

I don't know whether your problem could be solved using RegEx s. But if you want to check whether a given string is a valid SalesForce id, and if you are using Apex, you can do something like this.


Create an "Id" type variable and assign your "String" to that variable. If the string is not a valid id then it throws an exception.

Following method returns "true" if the string passed to it is a valid Id and otherwise returns false.

 

public Boolean isValidId(String s) {

Id validId;

try {

validId = s;

return true;

} catch (Exception ex) {

return false;

}

}