0

Can anyone explain what is Pattern idPattern and Matcher in the flowing class?

I've seen in many classes with different patterns like

Pattern idPattern = Pattern.compile('006[A-Za-z0-9]{12}');
 and
 Pattern idPattern = Pattern.compile('001[A-Za-z0-9]{16}');

Here I want to know what is 006 and [A-Za-z0-9] and {12} individually and what Matcher class will do at idPattern.matcher(subject); and matcher.find();

 global class AccountUpdateEmailHandler implements 
 Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(
Messaging.InboundEmail email,
 Messaging.InboundEnvelope envelope)
 {
String subject = email.subject;
Pattern idPattern = Pattern.compile('001[A-Za-z0-9]{12}');
Matcher matcher = idPattern.matcher(subject);
if (!matcher.find()) System.assert(false, 'No Account Id in subject!');

Account account = [SELECT Name FROM Account WHERE Id = :matcher.group(0)];
account.Name = email.plainTextBody;
update account;

Messaging.InboundEmailresult result = new Messaging.InboundEmailResult();
result.message = 'Account Name is now ' + account.Name;
return result;
  }
 }
3

006 is id key prefix for Opportunity. We know that SF id is 15 digit Case-Sensitive and 18 Digit Case Insensitive.

All Opportunity ID will start with 006 and last 12 or 16 digits will be alphanumeric based on type.

Your regex is trying to find if the ID mentioned is proper or not.

006- Opportunity

001- Account

003- Contact

005-Case

500- User.

So your code is trying to find valid Opportunity ID or AccountID in subject of the email received.

You can read more about SF id's here. What are Salesforce ID's composed of?

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.