0
for (Account acc : newAcc_lst) {
    System.debug('to15format Test 1-> ' + acc.id);
    Id to15convertString = acc.id;
    System.debug('to15format Test 2-> ' + to15convertString);
    String to15format = to15convertString.to15();
                
    System.debug('to15format Test 3-> ' + to15format);
    acc.id = to15format;
    System.debug('to15format Test 4-> ' + acc.id);
}
        
update newAcc_lst;

DEBUGS:

to15format Test 1-> 0017Y0000testedQAJ
to15format Test 2-> 0017Y0000testedQAJ
to15format Test 3-> 0017Y0000tested
to15format Test 4-> 0017Y0000testedQAJ

So, the Id cannot be updated to new 15 characters Id. Why is the reason?

7
  • 1
    15 and 18 character IDs are entirely equivalent. 15 character variants are case sensitive, whilst the 18 character equivalents are case insensitive (so work even if you lowercase the ID content). Why are you trying to do what you're doing?
    – Phil W
    Commented Apr 22, 2021 at 12:39
  • 2
    Since 15 and 18 are the same, I see no point in testing such conversion. I suggest you actually work with "Id" instances instead of Strings, since these will be agnostic of length.
    – Phil W
    Commented Apr 22, 2021 at 13:03
  • 1
    Any on-platform code that fails to be agnostic between 15 and 18 character Ids is broken and needs to be fixed.
    – David Reed
    Commented Apr 22, 2021 at 13:10
  • 2
    This is an X-Y Problem. If you're having ID problems in Apex, you're doing something wrong. Always use the Id data type instead of String, and always use the 18-character ID values.
    – sfdcfox
    Commented Apr 22, 2021 at 21:33
  • 1
    @CPS it should not matter if the ID field is 15 or 18 format, it's transparent for the developer, although it might be not transparent in a certain custom implementation (but for sure it's not the right design). Please provide a wider context i.e. what is the contract for those unit tests you mentioned, and I think we'll be able to help you then.
    – Marcin Sz.
    Commented Apr 25, 2021 at 20:36

0

You must log in to answer this question.

Browse other questions tagged .