Hi i will get 18 digit recordId in batch apex..But i have 15 digit Id.When id tried to compare both ids getting null value.How to get 15 digit id from 18 digit id.

String id = 'a0P4E0000030HzNXAU';
i need only 'a0P4E0000030HzN'

2 Answers 2


The following should work:

String id ='a0P4E0000030HzNXAU';

String Str= id.substring(0, 15);


public String substring(Integer startIndex, Integer endIndex)

Returns a new String that begins with the character at the specified zero-based startIndex and extends to the character at endIndex - 1.


  • startIndex Type: Integer

  • endIndex Type: Integer

  • Return Value Type: String


You should convert your string values to ID values; then it does not matter if you use the 15- or 18-character version:

Id a = [SELECT Id FROM Account LIMIT 1].Id;
String b = ((String)a).left(15);
// If either side is type Id, comparison is done as Id
System.debug(a == b);


(18624874)|USER_DEBUG|[3]|DEBUG|00150000019w4KXAAY 09:03:52.1

(18697954)|USER_DEBUG|[4]|DEBUG|00150000019w4KX 09:03:52.1

(18894321)|USER_DEBUG|[6]|DEBUG|true 09:03:52.1

It is far more convenient to use the correct data type than manually performing "conversions" by stripping off the last 3 characters. Also, string comparisons in Salesforce are NOT case sensitive, so you risk getting false positives:

System.assert('00150000019w4KX' == '00150000019w4Kx');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .