1

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'
1

2 Answers 2

1

The following should work:

String id ='a0P4E0000030HzNXAU';

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

Signature

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.

Parameters

  • startIndex Type: Integer

  • endIndex Type: Integer

  • Return Value Type: String

0

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);
System.debug(a);
System.debug(b);
// If either side is type Id, comparison is done as Id
System.debug(a == b);

Output:

(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');
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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