How to Convert 18 char Id to 15 char Id using substring in Apex trigger

I have a trigger where i am assigning ID to a cusotm field like below –

D.customfield__c = acc.Id;

The ID is coming as 00P28000000EwpjEA0

But i need only 00P28000000Ewpj 15 char. Is there is a way to use substring in apex trigger. I can’t use formula field.

Answer

All you need to do is convert the Id to a String and you can call substring(startIndex, endIndex) on it.

The following should work:

D.customfield__c = String.valueOf(acc.Id).substring(0, 15);

Attribution
Source : Link , Question Author : hellraizer , Answer Author : Alex Tennant

Leave a Comment