0

I am trying to get the id of the record from email subject.

This is the subject of my email and ID a5bK0000000HSWA

Subject: ABSI for approval. ref: a5bK0000000HSWA

Now I am getting the id using apex

Id recordId = subject.substring((subject.indexOf('ref:')) + 4, subject.length());
String dealPrefix = Early_Termination__c.SObjectType.getDescribe().getKeyPrefix();
if (String.valueOf(recordId).startsWith(dealPrefix)) {
}

But I am getting the error

System.StringException: Invalid id: a5bK0000000HSWA

Could anyone please help me to resolve this error?

3

Problem is that you have leading space in concrete Id value. You can check it by means of changing recordId type to string:

String subject = 'ABSI for approval. ref: a5bK0000000HSWA';
String recordId = subject.substring((subject.indexOf('ref:')) + 4, subject.length());
System.debug(recordId);

DEBUG|space goes herea5bK0000000HSWA


subject.indexOf('ref:') returns index of r symbol. to start getting concrete id, you need to skip 5 more symbols, because length of ref: is 5 symbols including space.

so change 4 to 5

Id recordId = subject.substring((subject.indexOf('ref:')) + 5, subject.length());
String dealPrefix = Early_Termination__c.SObjectType.getDescribe().getKeyPrefix();
if (String.valueOf(recordId).startsWith(dealPrefix)) {
}

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.